Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/api-tests/OAuthFlowTests.cs
blob: a900e5ddbcef5f4a241d320ba93f465a4ca36c0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1

using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;

namespace Progmasoft.Progmaweb.Api.Tests;

public sealed class OAuthFlowTests
{
    private static CancellationToken Cancel => TestContext.Current.CancellationToken;

    [Fact]
    public async Task AuthorizationCodeWithPkceIssuesTokensThatCarryOnlyTheGrantedClaims()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder1", "holder@example.com");
        (string clientId, string? secret) = await browser.RegisterApplicationAsync();
        (string verifier, string challenge) = OAuthTestClient.Pkce();

        Uri redirect = await browser.AuthorizeAsync(clientId, "openid profile email offline_access viget.packages.read",
            challenge);
        Assert.StartsWith(OAuthTestClient.RedirectUri, redirect.ToString(), StringComparison.Ordinal);
        Assert.Equal("state-123", OAuthTestClient.Query(redirect, "state"));
        string code = OAuthTestClient.Query(redirect, "code")!;

        (HttpStatusCode status, JsonElement tokens) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "authorization_code",
            ["code"] = code,
            ["redirect_uri"] = OAuthTestClient.RedirectUri,
            ["code_verifier"] = verifier,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });
        Assert.True(status == HttpStatusCode.OK, tokens.ToString());

        JsonElement identity = OAuthTestClient.Payload(tokens.GetProperty("id_token").GetString()!);
        Assert.Equal($"https://{AccountApiFactory.OAuthHost}/", identity.GetProperty("iss").GetString());
        Assert.Equal(clientId, identity.GetProperty("aud").GetString());
        Assert.Equal("OAuthHolder1", identity.GetProperty("preferred_username").GetString());
        Assert.Equal("holder@example.com", identity.GetProperty("email").GetString());
        Assert.False(identity.GetProperty("email_verified").GetBoolean());
        Assert.Equal("nonce-456", identity.GetProperty("nonce").GetString());

        using HttpClient client = factory.CreateOAuthClient();
        client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", tokens.GetProperty("access_token").GetString());
        JsonElement user = await client.GetFromJsonAsync<JsonElement>("/connect/userinfo", Cancel);
        Assert.Equal(identity.GetProperty("sub").GetString(), user.GetProperty("sub").GetString());
        Assert.Equal("OAuthHolder1", user.GetProperty("name").GetString());
        Assert.Equal("holder@example.com", user.GetProperty("email").GetString());

        // The code is single use.
        (HttpStatusCode reused, _) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "authorization_code",
            ["code"] = code,
            ["redirect_uri"] = OAuthTestClient.RedirectUri,
            ["code_verifier"] = verifier,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });
        Assert.Equal(HttpStatusCode.BadRequest, reused);
    }

    [Fact]
    public async Task ScopesThatWereNotGrantedAreNotInTheTokens()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder2", "holder2@example.com");
        (string clientId, string? secret) = await browser.RegisterApplicationAsync();
        (string verifier, string challenge) = OAuthTestClient.Pkce();

        Uri redirect = await browser.AuthorizeAsync(clientId, "openid", challenge);
        (_, JsonElement tokens) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "authorization_code",
            ["code"] = OAuthTestClient.Query(redirect, "code")!,
            ["redirect_uri"] = OAuthTestClient.RedirectUri,
            ["code_verifier"] = verifier,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });
        JsonElement identity = OAuthTestClient.Payload(tokens.GetProperty("id_token").GetString()!);
        Assert.False(identity.TryGetProperty("email", out _));
        Assert.False(identity.TryGetProperty("preferred_username", out _));
        Assert.False(tokens.TryGetProperty("refresh_token", out _));
    }

    [Fact]
    public async Task RefreshTokensRotateAndAReusedOneIsRefused()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder3", "holder3@example.com");
        (string clientId, string? secret) = await browser.RegisterApplicationAsync();
        (string verifier, string challenge) = OAuthTestClient.Pkce();
        Uri redirect = await browser.AuthorizeAsync(clientId, "openid offline_access", challenge);
        (_, JsonElement first) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "authorization_code",
            ["code"] = OAuthTestClient.Query(redirect, "code")!,
            ["redirect_uri"] = OAuthTestClient.RedirectUri,
            ["code_verifier"] = verifier,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });
        string refresh = first.GetProperty("refresh_token").GetString()!;

        Dictionary<string, string> refreshRequest = new()
        {
            ["grant_type"] = "refresh_token",
            ["refresh_token"] = refresh,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        };
        (HttpStatusCode renewed, JsonElement second) = await browser.TokenAsync(refreshRequest);
        Assert.Equal(HttpStatusCode.OK, renewed);
        Assert.NotEqual(refresh, second.GetProperty("refresh_token").GetString());

        (HttpStatusCode reused, JsonElement error) = await browser.TokenAsync(refreshRequest);
        Assert.Equal(HttpStatusCode.BadRequest, reused);
        Assert.Equal("invalid_grant", error.GetProperty("error").GetString());
    }

    [Fact]
    public async Task AnAllowedApplicationSkipsTheConsentAndWithdrawingEndsItsTokens()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder4", "holder4@example.com");
        (string clientId, string? secret) = await browser.RegisterApplicationAsync();
        (string verifier, string challenge) = OAuthTestClient.Pkce();
        Uri redirect = await browser.AuthorizeAsync(clientId, "openid offline_access", challenge);
        (_, JsonElement tokens) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "authorization_code",
            ["code"] = OAuthTestClient.Query(redirect, "code")!,
            ["redirect_uri"] = OAuthTestClient.RedirectUri,
            ["code_verifier"] = verifier,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });

        HttpResponseMessage again = await browser.Browser.GetAsync(
            OAuthTestClient.AuthorizePath(clientId, "openid offline_access", OAuthTestClient.Pkce().Challenge), Cancel);
        Assert.Equal(HttpStatusCode.Redirect, again.StatusCode);
        Assert.NotNull(OAuthTestClient.Query(again.Headers.Location!, "code"));

        JsonElement allowed = await (await browser.SendAsync(HttpMethod.Get, "/api/v1/oauth/allowed"))
            .Content.ReadFromJsonAsync<JsonElement>(Cancel);
        JsonElement entry = Assert.Single(allowed.EnumerateArray());
        Assert.Equal("Example Client", entry.GetProperty("displayName").GetString());
        string authorizationId = entry.GetProperty("authorizationId").GetString()!;
        HttpResponseMessage foreign = await browser.SendAsync(HttpMethod.Delete,
            $"/api/v1/oauth/allowed/{authorizationId}", origin: "https://attacker.example");
        Assert.Equal(HttpStatusCode.Forbidden, foreign.StatusCode);
        OAuthTestClient stranger = new(factory);
        await stranger.RegisterAndSignInAsync("OAuthStranger4", "stranger4@example.com");
        Assert.Equal(HttpStatusCode.NotFound, (await stranger.SendAsync(HttpMethod.Delete,
            $"/api/v1/oauth/allowed/{authorizationId}")).StatusCode);
        HttpResponseMessage withdrawn = await browser.SendAsync(HttpMethod.Delete,
            $"/api/v1/oauth/allowed/{authorizationId}");
        Assert.Equal(HttpStatusCode.NoContent, withdrawn.StatusCode);

        (HttpStatusCode refreshed, _) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "refresh_token",
            ["refresh_token"] = tokens.GetProperty("refresh_token").GetString()!,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });
        Assert.Equal(HttpStatusCode.BadRequest, refreshed);
    }

    [Fact]
    public async Task DenyingTheConsentReturnsAccessDenied()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder5", "holder5@example.com");
        (string clientId, _) = await browser.RegisterApplicationAsync();

        Uri redirect = await browser.AuthorizeAsync(clientId, "openid email", OAuthTestClient.Pkce().Challenge, "deny");
        Assert.Equal("access_denied", OAuthTestClient.Query(redirect, "error"));
        Assert.Null(OAuthTestClient.Query(redirect, "code"));
    }

    [Fact]
    public async Task TheConsentPageListsEachRequestedGrant()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder6", "holder6@example.com");
        (string clientId, _) = await browser.RegisterApplicationAsync(
            "profile email viget.packages.read viget.packages.publish viget.packages.yank");

        HttpResponseMessage authorize = await browser.Browser.GetAsync(OAuthTestClient.AuthorizePath(clientId,
            "openid profile email viget.packages.read viget.packages.publish viget.packages.yank",
            OAuthTestClient.Pkce().Challenge), Cancel);
        Uri consentPage = authorize.Headers.Location!;
        Assert.True(OAuthTestClient.IsConsentPage(consentPage), consentPage.OriginalString);

        JsonElement consent = await browser.Browser.GetFromJsonAsync<JsonElement>(
            "/api/v1/oauth/consent" + consentPage.OriginalString["/consent".Length..], Cancel);
        Assert.Equal(["profile", "email", "viget.packages.read", "viget.packages.publish", "viget.packages.yank"],
            consent.GetProperty("scopes").EnumerateArray().Select(scope => scope.GetString()));
        Assert.Equal("Example Client", consent.GetProperty("displayName").GetString());
        Assert.Equal("OAuthHolder6", consent.GetProperty("owner").GetString());
        Assert.Equal("OAuthHolder6", consent.GetProperty("accountName").GetString());
        Assert.Equal("https://client.example", consent.GetProperty("returnTo").GetString());
        Assert.Equal("https://client.example", consent.GetProperty("homepage").GetString());
    }

    [Fact]
    public async Task TheConsentPageDescribesOnlyRequestsTheApplicationCouldMake()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder10", "holder10@example.com");
        (string clientId, _) = await browser.RegisterApplicationAsync("profile");

        string Query(string redirectUri, string scope) =>
            $"/api/v1/oauth/consent?client_id={clientId}&redirect_uri={Uri.EscapeDataString(redirectUri)}" +
            $"&scope={Uri.EscapeDataString(scope)}";
        Assert.Equal(HttpStatusCode.OK,
            (await browser.Browser.GetAsync(Query(OAuthTestClient.RedirectUri, "openid profile"), Cancel)).StatusCode);
        Assert.Equal(HttpStatusCode.BadRequest,
            (await browser.Browser.GetAsync(Query("https://attacker.example/callback", "openid"), Cancel)).StatusCode);
        Assert.Equal(HttpStatusCode.BadRequest,
            (await browser.Browser.GetAsync(Query(OAuthTestClient.RedirectUri, "openid email"), Cancel)).StatusCode);
        Assert.Equal(HttpStatusCode.BadRequest, (await browser.Browser.GetAsync(
            "/api/v1/oauth/consent?client_id=pmo_missing&redirect_uri=https%3A%2F%2Fclient.example%2Fcallback",
            Cancel)).StatusCode);
    }

    [Fact]
    public async Task AConsentDecisionFromAnotherSiteIsRefused()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder11", "holder11@example.com");
        (string clientId, _) = await browser.RegisterApplicationAsync("profile");

        HttpResponseMessage authorize = await browser.Browser.GetAsync(
            OAuthTestClient.AuthorizePath(clientId, "openid profile", OAuthTestClient.Pkce().Challenge), Cancel);
        Uri consentPage = authorize.Headers.Location!;
        HttpResponseMessage forged = await browser.DecideAsync(consentPage, "allow", "https://attacker.example");
        Assert.Equal(HttpStatusCode.Forbidden, forged.StatusCode);
        HttpResponseMessage blind = await browser.DecideAsync(consentPage, "allow", origin: null);
        Assert.Equal(HttpStatusCode.Forbidden, blind.StatusCode);
    }

    [Fact]
    public async Task ViGetScopesGiveAccessTokensForViGet()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder7", "holder7@example.com");
        (string clientId, string? secret) = await browser.RegisterApplicationAsync("viget.packages.publish");
        (string verifier, string challenge) = OAuthTestClient.Pkce();
        Uri redirect = await browser.AuthorizeAsync(clientId, "openid viget.packages.publish", challenge);
        (HttpStatusCode status, JsonElement tokens) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "authorization_code",
            ["code"] = OAuthTestClient.Query(redirect, "code")!,
            ["redirect_uri"] = OAuthTestClient.RedirectUri,
            ["code_verifier"] = verifier,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });
        Assert.Equal(HttpStatusCode.OK, status);
        Assert.Equal("openid viget.packages.publish", tokens.GetProperty("scope").GetString());

        // The access token is encrypted for the resource servers; introspection shows its audience.
        using HttpClient client = factory.CreateOAuthClient();
        HttpResponseMessage response = await client.PostAsync("/connect/introspect", new FormUrlEncodedContent(
            new Dictionary<string, string>
            {
                ["token"] = tokens.GetProperty("access_token").GetString()!,
                ["client_id"] = clientId,
                ["client_secret"] = secret!
            }), Cancel);
        JsonElement introspection = await response.Content.ReadFromJsonAsync<JsonElement>(Cancel);
        Assert.True(introspection.GetProperty("active").GetBoolean(), introspection.ToString());
        Assert.Contains("viget", introspection.GetProperty("aud").ToString(), StringComparison.Ordinal);
    }

    [Fact]
    public async Task APublicClientUsesPkceWithoutASecret()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder8", "holder8@example.com");
        (string clientId, string? secret) = await browser.RegisterApplicationAsync("profile", kind: "desktop",
            redirectUris: "com.example.app:/callback");
        Assert.Null(secret);
        (string verifier, string challenge) = OAuthTestClient.Pkce();

        Uri redirect = await browser.AuthorizeAsync(clientId, "openid profile", challenge,
            redirectUri: "com.example.app:/callback");
        Assert.Equal("com.example.app", redirect.Scheme);

        (HttpStatusCode status, _) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "authorization_code",
            ["code"] = OAuthTestClient.Query(redirect, "code")!,
            ["redirect_uri"] = "com.example.app:/callback",
            ["code_verifier"] = verifier,
            ["client_id"] = clientId
        });
        Assert.Equal(HttpStatusCode.OK, status);
    }

    [Fact]
    public async Task OnPostgreSqlTheTablesAreCreatedAndAReusedRefreshTokenRevokesItsChain()
    {
        await using PostgresTestDatabase database = await PostgresTestDatabase.CreateAsync();
        await using AccountApiFactory factory = new(accountDatabase: database.ConnectionString);
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("PostgresHolder1", "postgres1@example.com");
        (string clientId, string? secret) = await browser.RegisterApplicationAsync();
        (string verifier, string challenge) = OAuthTestClient.Pkce();
        Uri redirect = await browser.AuthorizeAsync(clientId, "openid profile offline_access", challenge);
        (_, JsonElement first) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "authorization_code",
            ["code"] = OAuthTestClient.Query(redirect, "code")!,
            ["redirect_uri"] = OAuthTestClient.RedirectUri,
            ["code_verifier"] = verifier,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });
        Dictionary<string, string> Refresh(string token) => new()
        {
            ["grant_type"] = "refresh_token",
            ["refresh_token"] = token,
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        };

        (HttpStatusCode renewed, JsonElement second) =
            await browser.TokenAsync(Refresh(first.GetProperty("refresh_token").GetString()!));
        Assert.Equal(HttpStatusCode.OK, renewed);

        // Reusing the first refresh token is taken for theft: the token issued from it stops working as well.
        (HttpStatusCode reused, _) = await browser.TokenAsync(Refresh(first.GetProperty("refresh_token").GetString()!));
        (HttpStatusCode chained, _) = await browser.TokenAsync(Refresh(second.GetProperty("refresh_token").GetString()!));
        Assert.Equal(HttpStatusCode.BadRequest, reused);
        Assert.Equal(HttpStatusCode.BadRequest, chained);

        await using Npgsql.NpgsqlDataSource dataSource = database.OpenDataSource();
        await using Npgsql.NpgsqlCommand version =
            dataSource.CreateCommand("SELECT max(version) FROM oauth_schema_versions");
        Assert.Equal(Progmasoft.OAuth.Storage.OAuthSchema.CurrentVersion,
            (int)(await version.ExecuteScalarAsync(Cancel))!);
    }

    [Fact]
    public async Task ClientCredentialsActAsTheApplicationAndTakeNoAccountScope()
    {
        await using AccountApiFactory factory = new();
        OAuthTestClient browser = new(factory);
        await browser.RegisterAndSignInAsync("OAuthHolder9", "holder9@example.com");
        (string clientId, string? secret) = await browser.RegisterServiceAsync();

        (HttpStatusCode status, JsonElement token) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "client_credentials",
            ["client_id"] = clientId,
            ["client_secret"] = secret!
        });
        Assert.True(status == HttpStatusCode.OK, token.ToString());

        (HttpStatusCode scoped, JsonElement error) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "client_credentials",
            ["client_id"] = clientId,
            ["client_secret"] = secret!,
            ["scope"] = "profile"
        });
        // A service has no scope permission at all, so OpenIddict refuses the scope before the handler sees it.
        Assert.Equal(HttpStatusCode.BadRequest, scoped);
        Assert.Equal("invalid_request", error.GetProperty("error").GetString());
        Assert.Contains("scope", error.GetProperty("error_description").GetString(), StringComparison.Ordinal);

        (HttpStatusCode wrongSecret, _) = await browser.TokenAsync(new Dictionary<string, string>
        {
            ["grant_type"] = "client_credentials",
            ["client_id"] = clientId,
            ["client_secret"] = "not-the-secret"
        });
        Assert.Equal(HttpStatusCode.Unauthorized, wrongSecret);
    }
}