Skip to content

Commit e6c3585

Browse files
committed
(#100) Swagger Authentication
1 parent c9a7e50 commit e6c3585

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

src/ModularMonolith/ClassifiedAds.Modules.Auth/IdServerPersistenceExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ public static void MigrateIdServerDb(this IApplicationBuilder app)
5757
context.Database.Migrate();
5858

5959
var clients = new List<Client>();
60+
61+
if (!context.Clients.Any(x => x.ClientId == "Swagger"))
62+
{
63+
clients.Add(new Client
64+
{
65+
ClientId = "Swagger",
66+
ClientName = "Swagger",
67+
AllowedGrantTypes = GrantTypes.Code,
68+
RequirePkce = true,
69+
RedirectUris =
70+
{
71+
"https://localhost:44312/oauth2-redirect.html",
72+
"http://host.docker.internal:9002/oauth2-redirect.html",
73+
},
74+
AllowedScopes =
75+
{
76+
IdentityServerConstants.StandardScopes.OpenId,
77+
IdentityServerConstants.StandardScopes.Profile,
78+
"ClassifiedAds.WebAPI",
79+
},
80+
ClientSecrets =
81+
{
82+
new Secret("secret".Sha256()),
83+
},
84+
RequireConsent = false,
85+
});
86+
}
87+
6088
if (!context.Clients.Any(x => x.ClientId == "ClassifiedAds.WebMVC"))
6189
{
6290
clients.Add(new Client

src/ModularMonolith/ClassifiedAds.WebAPI/Startup.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,33 @@ public void ConfigureServices(IServiceCollection services)
169169
},
170170
});
171171

172-
setupAction.AddSecurityDefinition("bearer", new OpenApiSecurityScheme()
172+
setupAction.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
173173
{
174174
Type = SecuritySchemeType.Http,
175-
Scheme = "bearer",
175+
Scheme = "Bearer",
176176
BearerFormat = "JWT",
177177
Description = "Input your Bearer token to access this API",
178178
});
179179

180+
setupAction.AddSecurityDefinition("Oidc", new OpenApiSecurityScheme
181+
{
182+
Type = SecuritySchemeType.OAuth2,
183+
Flows = new OpenApiOAuthFlows
184+
{
185+
AuthorizationCode = new OpenApiOAuthFlow
186+
{
187+
TokenUrl = new Uri(AppSettings.IdentityServerAuthentication.Authority + "/connect/token", UriKind.Absolute),
188+
AuthorizationUrl = new Uri(AppSettings.IdentityServerAuthentication.Authority + "/connect/authorize", UriKind.Absolute),
189+
Scopes = new Dictionary<string, string>
190+
{
191+
{ "openid", "OpenId" },
192+
{ "profile", "Profile" },
193+
{ "ClassifiedAds.WebAPI", "ClassifiedAds WebAPI" },
194+
},
195+
},
196+
},
197+
});
198+
180199
setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
181200
{
182201
{
@@ -185,7 +204,17 @@ public void ConfigureServices(IServiceCollection services)
185204
Reference = new OpenApiReference
186205
{
187206
Type = ReferenceType.SecurityScheme,
188-
Id = "bearer",
207+
Id = "Oidc",
208+
},
209+
}, new List<string>()
210+
},
211+
{
212+
new OpenApiSecurityScheme
213+
{
214+
Reference = new OpenApiReference
215+
{
216+
Type = ReferenceType.SecurityScheme,
217+
Id = "Bearer",
189218
},
190219
}, new List<string>()
191220
},
@@ -214,6 +243,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
214243

215244
app.UseSwaggerUI(setupAction =>
216245
{
246+
setupAction.OAuthClientId("Swagger");
247+
setupAction.OAuthClientSecret("secret");
248+
setupAction.OAuthUsePkce();
249+
217250
setupAction.SwaggerEndpoint(
218251
"/swagger/ClassifiedAds/swagger.json",
219252
"ClassifiedAds API");

src/Monolith/ClassifiedAds.Persistence/IdServerPersistenceExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public static void MigrateIdServerDb(this IApplicationBuilder app)
8181
{
8282
new Secret("secret".Sha256()),
8383
},
84-
AllowOfflineAccess = true,
8584
RequireConsent = false,
8685
});
8786
}

src/Monolith/ClassifiedAds.WebAPI/Startup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ public void ConfigureServices(IServiceCollection services)
136136
{ "openid", "OpenId" },
137137
{ "profile", "Profile" },
138138
{ "ClassifiedAds.WebAPI", "ClassifiedAds WebAPI" },
139-
{ "offline_access", "Offline Access" },
140139
},
141140
},
142141
},

0 commit comments

Comments
 (0)