|
|
|
@ -1,9 +1,10 @@
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
@ -31,6 +32,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 +54,40 @@ 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 = RSA.Create();
|
|
|
|
|
const string jwtKeyPath = ".aspnet/jwt-key";
|
|
|
|
|
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
|
|
|
|
string fullPath = Path.Combine(home, jwtKeyPath);
|
|
|
|
|
if (File.Exists(fullPath))
|
|
|
|
|
{
|
|
|
|
|
rsaKey.ImportRSAPrivateKey(File.ReadAllBytes(fullPath), out _);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string? dirName = Path.GetDirectoryName(fullPath);
|
|
|
|
|
if (!string.IsNullOrEmpty(dirName))
|
|
|
|
|
Directory.CreateDirectory(dirName);
|
|
|
|
|
|
|
|
|
|
var privateKey = rsaKey.ExportRSAPrivateKey();
|
|
|
|
|
File.WriteAllBytes(fullPath, privateKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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,7 +147,7 @@ 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")
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|