|
|
|
@ -1,11 +1,13 @@
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using OAuthServer;
|
|
|
|
|
using OAuthServer.Services;
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
@ -31,6 +33,20 @@ builder.Services.AddSwaggerGen(options =>
|
|
|
|
|
Id = "Bearer"
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
|
{
|
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
|
Id = "Bearer"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
new string[] { }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddDbContext<AppDbContext>(options => { options.UseSqlite("DataSource=db.sqlite3"); });
|
|
|
|
@ -39,24 +55,24 @@ builder.Services.AddIdentity<IdentityUser, IdentityRole>(options => { options.St
|
|
|
|
|
.AddEntityFrameworkStores<AppDbContext>()
|
|
|
|
|
.AddDefaultTokenProviders();
|
|
|
|
|
|
|
|
|
|
// Load the signing key from a file if it exists or create a new one
|
|
|
|
|
var rsaKey = JwtService.GetSigningKey();
|
|
|
|
|
|
|
|
|
|
// Add the JWT authentication method
|
|
|
|
|
builder.Services.AddAuthentication().AddJwtBearer("OAuthToken", options =>
|
|
|
|
|
{
|
|
|
|
|
// options.RequireHttpsMetadata = false;
|
|
|
|
|
// options.SaveToken = true;
|
|
|
|
|
// options.TokenValidationParameters = new TokenValidationParameters()
|
|
|
|
|
// {
|
|
|
|
|
// ValidateIssuer = true,
|
|
|
|
|
// ValidateAudience = true,
|
|
|
|
|
// RequireSignedTokens = true,
|
|
|
|
|
// ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|
|
|
|
// ValidAudience = builder.Configuration["Jwt:Audience"],
|
|
|
|
|
// IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("JWT_KEY")))
|
|
|
|
|
// };
|
|
|
|
|
options.SaveToken = false;
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters()
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = false,
|
|
|
|
|
ValidateAudience = false,
|
|
|
|
|
RequireSignedTokens = true,
|
|
|
|
|
IssuerSigningKey = new RsaSecurityKey(rsaKey)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.Configure<IdentityOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// SignIn settings.
|
|
|
|
|
options.SignIn.RequireConfirmedAccount = false;
|
|
|
|
|
options.SignIn.RequireConfirmedEmail = false;
|
|
|
|
@ -116,10 +132,12 @@ builder.Services.AddAuthorization(options =>
|
|
|
|
|
// Require the External role to authenticate with a different authentication method
|
|
|
|
|
options.AddPolicy("External", policy => policy
|
|
|
|
|
.RequireRole("External")
|
|
|
|
|
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddAuthenticationSchemes("OAuthToken")
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<JwtService>();
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
|