fix: switch JWT Bearer auth from symmetric key to OIDC discovery in 5 microservices
Replace manual SymmetricSecurityKey validation with Authority-based OIDC discovery so tokens are validated against RSA keys published by the IAM IdentityServer's discovery endpoint. Services updated: - CatalogService.API - OrderService.API - InventoryService.API - FnbEngine.API - BookingService.API Co-authored-by: Velik <hongochai10@users.noreply.github.com>
This commit is contained in:
@@ -85,22 +85,19 @@ try
|
||||
name: "postgresql",
|
||||
tags: ["db", "postgresql"]);
|
||||
|
||||
// EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication
|
||||
// EN: Add JWT Bearer authentication via IAM IdentityServer OIDC discovery
|
||||
// VI: Thêm JWT Bearer authentication qua IAM IdentityServer OIDC discovery
|
||||
var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001";
|
||||
var jwtSecret = builder.Configuration["Jwt:Secret"] ?? "";
|
||||
builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.Authority = jwtAuthority;
|
||||
options.RequireHttpsMetadata = false;
|
||||
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret),
|
||||
IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret)
|
||||
? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret))
|
||||
: null,
|
||||
};
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
@@ -85,22 +85,19 @@ try
|
||||
name: "postgresql",
|
||||
tags: ["db", "postgresql"]);
|
||||
|
||||
// EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication
|
||||
// EN: Add JWT Bearer authentication via IAM IdentityServer OIDC discovery
|
||||
// VI: Thêm JWT Bearer authentication qua IAM IdentityServer OIDC discovery
|
||||
var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001";
|
||||
var jwtSecret = builder.Configuration["Jwt:Secret"] ?? "";
|
||||
builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.Authority = jwtAuthority;
|
||||
options.RequireHttpsMetadata = false;
|
||||
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret),
|
||||
IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret)
|
||||
? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret))
|
||||
: null,
|
||||
};
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
@@ -85,22 +85,19 @@ try
|
||||
name: "postgresql",
|
||||
tags: ["db", "postgresql"]);
|
||||
|
||||
// EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication
|
||||
// EN: Add JWT Bearer authentication via IAM IdentityServer OIDC discovery
|
||||
// VI: Thêm JWT Bearer authentication qua IAM IdentityServer OIDC discovery
|
||||
var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001";
|
||||
var jwtSecret = builder.Configuration["Jwt:Secret"] ?? "";
|
||||
builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.Authority = jwtAuthority;
|
||||
options.RequireHttpsMetadata = false;
|
||||
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret),
|
||||
IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret)
|
||||
? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret))
|
||||
: null,
|
||||
};
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
@@ -88,22 +88,19 @@ try
|
||||
name: "postgresql",
|
||||
tags: ["db", "postgresql"]);
|
||||
|
||||
// EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication
|
||||
// EN: Add JWT Bearer authentication via IAM IdentityServer OIDC discovery
|
||||
// VI: Thêm JWT Bearer authentication qua IAM IdentityServer OIDC discovery
|
||||
var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001";
|
||||
var jwtSecret = builder.Configuration["Jwt:Secret"] ?? "";
|
||||
builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.Authority = jwtAuthority;
|
||||
options.RequireHttpsMetadata = false;
|
||||
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret),
|
||||
IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret)
|
||||
? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret))
|
||||
: null,
|
||||
};
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
@@ -148,22 +148,19 @@ try
|
||||
name: "postgresql",
|
||||
tags: ["db", "postgresql"]);
|
||||
|
||||
// EN: Add JWT Bearer authentication / VI: Thêm JWT Bearer authentication
|
||||
// EN: Add JWT Bearer authentication via IAM IdentityServer OIDC discovery
|
||||
// VI: Thêm JWT Bearer authentication qua IAM IdentityServer OIDC discovery
|
||||
var jwtAuthority = builder.Configuration["Jwt:Authority"] ?? "http://localhost:5001";
|
||||
var jwtSecret = builder.Configuration["Jwt:Secret"] ?? "";
|
||||
builder.Services.AddAuthentication(Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.Authority = jwtAuthority;
|
||||
options.RequireHttpsMetadata = false;
|
||||
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = !string.IsNullOrEmpty(jwtSecret),
|
||||
IssuerSigningKey = !string.IsNullOrEmpty(jwtSecret)
|
||||
? new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(jwtSecret))
|
||||
: null,
|
||||
};
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
Reference in New Issue
Block a user