Skip to content

Commit 31fb3e3

Browse files
Merge pull request #1405 from lurumad/feature/pkce
Add support to PKCE for SwaggerUI & update OAuth2Integration sample #999
2 parents f987e36 + f84a077 commit 31fb3e3

File tree

7 files changed

+51
-32
lines changed

7 files changed

+51
-32
lines changed

package-lock.json

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,10 @@ public class OAuthConfigObject
200200
/// (Authorization header with Basic base64encode(client_id + client_secret))
201201
/// </summary>
202202
public bool UseBasicAuthenticationWithAccessCodeGrant { get; set; } = false;
203+
204+
/// <summary>
205+
/// Enabled to use PKCE with the Autorization Code flow.
206+
/// </summary>
207+
public bool UsePkceWithAuthorizationCodeGrant { get; set; } = false;
203208
}
204209
}

src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIOptionsExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,14 @@ public static void OAuthUseBasicAuthenticationWithAccessCodeGrant(this SwaggerUI
257257
{
258258
options.OAuthConfigObject.UseBasicAuthenticationWithAccessCodeGrant = true;
259259
}
260+
261+
/// <summary>
262+
/// Enabled to use PKCE with the Autorization Code flow.
263+
/// </summary>
264+
/// <param name="options"></param>
265+
public static void OAuthUsePkce(this SwaggerUIOptions options)
266+
{
267+
options.OAuthConfigObject.UsePkceWithAuthorizationCodeGrant = true;
268+
}
260269
}
261270
}

src/Swashbuckle.AspNetCore.SwaggerUI/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Swashbuckle.AspNetCore.SwaggerUI/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"version": "1.0.0",
44
"private": true,
55
"dependencies": {
6-
"swagger-ui-dist": "3.24.0"
6+
"swagger-ui-dist": "3.24.3"
77
}
88
}

test/WebSites/OAuth2Integration/AuthServer/Config.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,38 @@ internal static IEnumerable<Client> Clients()
1010
{
1111
yield return new Client
1212
{
13-
AllowAccessTokensViaBrowser = true,
14-
AllowedGrantTypes = GrantTypes.Implicit,
15-
AllowedScopes = new[] { "readAccess", "writeAccess" },
1613
ClientId = "test-id",
17-
ClientName = "test-app",
18-
ClientSecrets = new[] { new Secret("test-secret".Sha256()) },
14+
ClientName = "Interactive client (Code with PKCE)",
15+
1916
RedirectUris = new[] {
2017
"http://localhost:55202/resource-server/swagger/oauth2-redirect.html", // IIS Express
2118
"http://localhost:5000/resource-server/swagger/oauth2-redirect.html", // Kestrel
22-
}
19+
},
20+
21+
RequireClientSecret = false,
22+
RequireConsent = true,
23+
24+
AllowedGrantTypes = GrantTypes.Code,
25+
RequirePkce = true,
26+
AllowedScopes = new[] { "readAccess", "writeAccess" },
27+
};
28+
29+
yield return new Client
30+
{
31+
ClientId = "test-id.confidential",
32+
ClientName = "Interactive client (Code with PKCE)",
33+
34+
RedirectUris = new[] {
35+
"http://localhost:55202/resource-server/swagger/oauth2-redirect.html", // IIS Express
36+
"http://localhost:5000/resource-server/swagger/oauth2-redirect.html", // Kestrel
37+
},
38+
39+
ClientSecrets = { new Secret("test-secret".Sha256()) },
40+
RequireConsent = true,
41+
42+
AllowedGrantTypes = GrantTypes.Code,
43+
RequirePkce = true,
44+
AllowedScopes = new[] { "readAccess", "writeAccess" },
2345
};
2446
}
2547

test/WebSites/OAuth2Integration/Startup.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ public void ConfigureServices(IServiceCollection services)
6060
Type = SecuritySchemeType.OAuth2,
6161
Flows = new OpenApiOAuthFlows
6262
{
63-
Implicit = new OpenApiOAuthFlow
63+
AuthorizationCode = new OpenApiOAuthFlow
6464
{
6565
AuthorizationUrl = new Uri("/auth-server/connect/authorize", UriKind.Relative),
66+
TokenUrl = new Uri("/auth-server/connect/token", UriKind.Relative),
6667
Scopes = new Dictionary<string, string>
6768
{
6869
{ "readAccess", "Access read operations" },
@@ -128,15 +129,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
128129
resourceServer.UseSwaggerUI(c =>
129130
{
130131
c.SwaggerEndpoint("/resource-server/swagger/v1/swagger.json", "My API V1");
131-
132132
// Additional OAuth settings (See https://github.com/swagger-api/swagger-ui/blob/v3.10.0/docs/usage/oauth2.md)
133-
c.OAuthClientId("test-id");
133+
c.OAuthClientId("test-id.confidential");
134134
c.OAuthClientSecret("test-secret");
135-
c.OAuthRealm("test-realm");
136135
c.OAuthAppName("test-app");
137136
c.OAuthScopeSeparator(" ");
138-
c.OAuthAdditionalQueryStringParams(new Dictionary<string, string> { { "foo", "bar" }});
139-
c.OAuthUseBasicAuthenticationWithAccessCodeGrant();
137+
c.OAuthUsePkce();
140138
c.ConfigObject.DeepLinking = true;
141139
});
142140
});

0 commit comments

Comments
 (0)