Skip to content

Commit

Permalink
OpenIddict
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Dec 29, 2024
1 parent 8b024c9 commit 8f4be10
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ spec:
spec:
containers:
- env:
- name: IdentityServerAuthentication__Authority
- name: Authentication__IdentityServer__Authority
value: http://{{ .Release.Name}}-identityserver
- name: IdentityServerAuthentication__RequireHttpsMetadata
- name: Authentication__IdentityServer__RequireHttpsMetadata
value: "false"
envFrom:
- configMapRef:
Expand Down
4 changes: 2 additions & 2 deletions src/ModularMonolith/.k8s/webapi.deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ spec:
spec:
containers:
- env:
- name: IdentityServerAuthentication__Authority
- name: Authentication__IdentityServer__Authority
value: http://identityserver
- name: IdentityServerAuthentication__RequireHttpsMetadata
- name: Authentication__IdentityServer__RequireHttpsMetadata
value: "false"
envFrom:
- configMapRef:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AppSettings

public MonitoringOptions Monitoring { get; set; }

public IdentityServerAuthentication IdentityServerAuthentication { get; set; }
public AuthenticationOptions Authentication { get; set; }

public string AllowedHosts { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@

namespace ClassifiedAds.WebAPI.ConfigurationOptions;

public class IdentityServerAuthentication
public class AuthenticationOptions
{
public string Provider { get; set; }

public IdentityServerOptions IdentityServer { get; set; }

public JwtOptions Jwt { get; set; }
}

public class IdentityServerOptions
{
public string Authority { get; set; }

public string ApiName { get; set; }
public string Audience { get; set; }

public bool RequireHttpsMetadata { get; set; }

public OpenIddictOptions OpenIddict { get; set; }
}

public class OpenIddictOptions
public class JwtOptions
{
public string IssuerUri { get; set; }

public string Audience { get; set; }

public CertificateOption TokenDecryptionCertificate { get; set; }

public CertificateOption IssuerSigningCertificate { get; set; }
Expand Down
26 changes: 13 additions & 13 deletions src/ModularMonolith/ClassifiedAds.WebAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,26 @@

services.AddAuthentication(options =>
{
options.DefaultScheme = appSettings.IdentityServerAuthentication.Provider switch
options.DefaultScheme = appSettings.Authentication.Provider switch
{
"OpenIddict" => "OpenIddict",
"Jwt" => "Jwt",
_ => JwtBearerDefaults.AuthenticationScheme
};
})
.AddJwtBearer(options =>
{
options.Authority = appSettings.IdentityServerAuthentication.Authority;
options.Audience = appSettings.IdentityServerAuthentication.ApiName;
options.RequireHttpsMetadata = appSettings.IdentityServerAuthentication.RequireHttpsMetadata;
options.Authority = appSettings.Authentication.IdentityServer.Authority;
options.Audience = appSettings.Authentication.IdentityServer.Audience;
options.RequireHttpsMetadata = appSettings.Authentication.IdentityServer.RequireHttpsMetadata;
})
.AddJwtBearer("OpenIddict", options =>
.AddJwtBearer("Jwt", options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false,
ValidIssuer = appSettings.IdentityServerAuthentication.OpenIddict.IssuerUri,
TokenDecryptionKey = new X509SecurityKey(appSettings.IdentityServerAuthentication.OpenIddict.TokenDecryptionCertificate.FindCertificate()),
IssuerSigningKey = new X509SecurityKey(appSettings.IdentityServerAuthentication.OpenIddict.IssuerSigningCertificate.FindCertificate()),
ValidIssuer = appSettings.Authentication.Jwt.IssuerUri,
ValidAudience = appSettings.Authentication.Jwt.Audience,
TokenDecryptionKey = new X509SecurityKey(appSettings.Authentication.Jwt.TokenDecryptionCertificate.FindCertificate()),
IssuerSigningKey = new X509SecurityKey(appSettings.Authentication.Jwt.IssuerSigningCertificate.FindCertificate()),
};
});

Expand Down Expand Up @@ -161,8 +161,8 @@
{
AuthorizationCode = new OpenApiOAuthFlow
{
TokenUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/token", UriKind.Absolute),
AuthorizationUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/authorize", UriKind.Absolute),
TokenUrl = new Uri(appSettings.Authentication.IdentityServer.Authority + "/connect/token", UriKind.Absolute),
AuthorizationUrl = new Uri(appSettings.Authentication.IdentityServer.Authority + "/connect/authorize", UriKind.Absolute),
Scopes = new Dictionary<string, string>
{
{ "openid", "OpenId" },
Expand All @@ -172,7 +172,7 @@
},
ClientCredentials = new OpenApiOAuthFlow
{
TokenUrl = new Uri(appSettings.IdentityServerAuthentication.Authority + "/connect/token", UriKind.Absolute),
TokenUrl = new Uri(appSettings.Authentication.IdentityServer.Authority + "/connect/token", UriKind.Absolute),
Scopes = new Dictionary<string, string>
{
{ "ClassifiedAds.WebAPI", "ClassifiedAds WebAPI" },
Expand Down
15 changes: 9 additions & 6 deletions src/ModularMonolith/ClassifiedAds.WebAPI/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"IdentityServerAuthentication": {
"Provider": "IdentityServer4",
"Authority": "https://localhost:44367",
"ApiName": "ClassifiedAds.WebAPI",
"RequireHttpsMetadata": "true",
"OpenIddict": {
"Authentication": {
"Provider": "IdentityServer",
"IdentityServer": {
"Authority": "https://localhost:44367",
"Audience": "ClassifiedAds.WebAPI",
"RequireHttpsMetadata": "true"
},
"Jwt": {
"IssuerUri": "https://localhost:44367/",
"Audience": "ClassifiedAds.WebAPI",
"TokenDecryptionCertificate": {
"Thumbprint": null,
"Path": "Certs/classifiedads.identityserver.pfx",
Expand Down
4 changes: 2 additions & 2 deletions src/ModularMonolith/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ services:
- rabbitmq
environment:
ASPNETCORE_ENVIRONMENT: ${ASPNETCORE_ENVIRONMENT}
IdentityServerAuthentication__Authority: "http://host.docker.internal:9000"
IdentityServerAuthentication__RequireHttpsMetadata: "false"
Authentication__IdentityServer__Authority: "http://host.docker.internal:9000"
Authentication__IdentityServer__RequireHttpsMetadata: "false"
Modules__AuditLog__ConnectionStrings__Default: ${Modules__AuditLog__ConnectionStrings__Default}
Modules__Configuration__ConnectionStrings__Default: ${Modules__Configuration__ConnectionStrings__Default}
Modules__Identity__ConnectionStrings__Default: ${Modules__Identity__ConnectionStrings__Default}
Expand Down

0 comments on commit 8f4be10

Please sign in to comment.