From 19a3ba6f87801b7a08c995cc4e2012b6055d6d19 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Thu, 10 Jul 2025 09:16:58 +0100 Subject: [PATCH] config the issuer url --- src/Yavsc/Extensions/HostingExtensions.cs | 203 +++++-------------- src/Yavsc/Helpers/GoogleOAuthHelpers.cs | 4 +- src/sampleWebAsWebApiClient/Program.cs | 5 +- src/sampleWebAsWebApiClient/appsettings.json | 1 + 4 files changed, 62 insertions(+), 151 deletions(-) diff --git a/src/Yavsc/Extensions/HostingExtensions.cs b/src/Yavsc/Extensions/HostingExtensions.cs index df035487..abac3b0c 100644 --- a/src/Yavsc/Extensions/HostingExtensions.cs +++ b/src/Yavsc/Extensions/HostingExtensions.cs @@ -43,57 +43,6 @@ namespace Yavsc.Extensions; public static class HostingExtensions { - public static IApplicationBuilder ConfigureFileServerApp(this IApplicationBuilder app, - bool enableDirectoryBrowsing = false) - { - - var userFilesDirInfo = new DirectoryInfo(Config.SiteSetup.Blog); - AbstractFileSystemHelpers.UserFilesDirName = userFilesDirInfo.FullName; - - if (!userFilesDirInfo.Exists) userFilesDirInfo.Create(); - - Config.UserFilesOptions = new FileServerOptions() - { - FileProvider = new PhysicalFileProvider(AbstractFileSystemHelpers.UserFilesDirName), - RequestPath = PathString.FromUriComponent(Constants.UserFilesPath), - EnableDirectoryBrowsing = enableDirectoryBrowsing, - }; - Config.UserFilesOptions.EnableDefaultFiles = true; - Config.UserFilesOptions.StaticFileOptions.ServeUnknownFileTypes = true; - - var avatarsDirInfo = new DirectoryInfo(Config.SiteSetup.Avatars); - if (!avatarsDirInfo.Exists) avatarsDirInfo.Create(); - Config.AvatarsDirName = avatarsDirInfo.FullName; - - Config.AvatarsOptions = new FileServerOptions() - { - FileProvider = new PhysicalFileProvider(Config.AvatarsDirName), - RequestPath = PathString.FromUriComponent(Constants.AvatarsPath), - EnableDirectoryBrowsing = enableDirectoryBrowsing - }; - - - var gitdirinfo = new DirectoryInfo(Config.SiteSetup.GitRepository); - Config.GitDirName = gitdirinfo.FullName; - if (!gitdirinfo.Exists) gitdirinfo.Create(); - Config.GitOptions = new FileServerOptions() - { - FileProvider = new PhysicalFileProvider(Config.GitDirName), - RequestPath = PathString.FromUriComponent(Constants.GitPath), - EnableDirectoryBrowsing = enableDirectoryBrowsing, - }; - Config.GitOptions.DefaultFilesOptions.DefaultFileNames.Add("index.md"); - Config.GitOptions.StaticFileOptions.ServeUnknownFileTypes = true; - - app.UseFileServer(Config.UserFilesOptions); - - app.UseFileServer(Config.AvatarsOptions); - - app.UseFileServer(Config.GitOptions); - app.UseStaticFiles(); - return app; - } - internal static WebApplication ConfigureWebAppServices(this WebApplicationBuilder builder) { IServiceCollection services = LoadConfiguration(builder); @@ -231,23 +180,13 @@ public static class HostingExtensions // OAuth2AppSettings var googleAuthSettings = builder.Configuration.GetSection("Authentication:Google"); - string? googleClientFile = builder.Configuration["Authentication:Google:GoogleWebClientJson"]; - string? googleServiceAccountJsonFile = builder.Configuration["Authentication:Google:GoogleServiceAccountJson"]; - if (googleClientFile != null) - { - Config.GoogleWebClientConfiguration = new ConfigurationBuilder().AddJsonFile(googleClientFile).Build(); - } + //LoadGoogleConfig(builder.Configuration); - if (googleServiceAccountJsonFile != null) - { - FileInfo safile = new FileInfo(googleServiceAccountJsonFile); - Config.GServiceAccount = JsonConvert.DeserializeObject(safile.OpenText().ReadToEnd()); - } + var services = builder.Services; _ = services.AddControllersWithViews() .AddNewtonsoftJson(); - LoadGoogleConfig(builder.Configuration); - + services.Configure(siteSection); services.Configure(smtpSection); services.Configure(paypalSection); @@ -414,87 +353,57 @@ public static class HostingExtensions Config.GServiceAccount = JsonConvert.DeserializeObject(safile.OpenText().ReadToEnd()); } } -} - -public class MyIdentityStore : IUserClaimStore -{ - public Task AddClaimsAsync(IdentityUser user, IEnumerable claims, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task CreateAsync(IdentityUser user, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task DeleteAsync(IdentityUser user, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public void Dispose() - { - throw new NotImplementedException(); - } - - public Task FindByIdAsync(string userId, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task> GetClaimsAsync(IdentityUser user, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task GetNormalizedUserNameAsync(IdentityUser user, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task GetUserIdAsync(IdentityUser user, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task GetUserNameAsync(IdentityUser user, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task> GetUsersForClaimAsync(Claim claim, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task RemoveClaimsAsync(IdentityUser user, IEnumerable claims, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task ReplaceClaimAsync(IdentityUser user, Claim claim, Claim newClaim, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task SetNormalizedUserNameAsync(IdentityUser user, string? normalizedName, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task SetUserNameAsync(IdentityUser user, string? userName, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task UpdateAsync(IdentityUser user, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } + + public static IApplicationBuilder ConfigureFileServerApp(this IApplicationBuilder app, + bool enableDirectoryBrowsing = false) + { + + var userFilesDirInfo = new DirectoryInfo(Config.SiteSetup.Blog); + AbstractFileSystemHelpers.UserFilesDirName = userFilesDirInfo.FullName; + + if (!userFilesDirInfo.Exists) userFilesDirInfo.Create(); + + Config.UserFilesOptions = new FileServerOptions() + { + FileProvider = new PhysicalFileProvider(AbstractFileSystemHelpers.UserFilesDirName), + RequestPath = PathString.FromUriComponent(Constants.UserFilesPath), + EnableDirectoryBrowsing = enableDirectoryBrowsing, + }; + Config.UserFilesOptions.EnableDefaultFiles = true; + Config.UserFilesOptions.StaticFileOptions.ServeUnknownFileTypes = true; + + var avatarsDirInfo = new DirectoryInfo(Config.SiteSetup.Avatars); + if (!avatarsDirInfo.Exists) avatarsDirInfo.Create(); + Config.AvatarsDirName = avatarsDirInfo.FullName; + + Config.AvatarsOptions = new FileServerOptions() + { + FileProvider = new PhysicalFileProvider(Config.AvatarsDirName), + RequestPath = PathString.FromUriComponent(Constants.AvatarsPath), + EnableDirectoryBrowsing = enableDirectoryBrowsing + }; + + + var gitdirinfo = new DirectoryInfo(Config.SiteSetup.GitRepository); + Config.GitDirName = gitdirinfo.FullName; + if (!gitdirinfo.Exists) gitdirinfo.Create(); + Config.GitOptions = new FileServerOptions() + { + FileProvider = new PhysicalFileProvider(Config.GitDirName), + RequestPath = PathString.FromUriComponent(Constants.GitPath), + EnableDirectoryBrowsing = enableDirectoryBrowsing, + }; + Config.GitOptions.DefaultFilesOptions.DefaultFileNames.Add("index.md"); + Config.GitOptions.StaticFileOptions.ServeUnknownFileTypes = true; + + app.UseFileServer(Config.UserFilesOptions); + + app.UseFileServer(Config.AvatarsOptions); + + app.UseFileServer(Config.GitOptions); + app.UseStaticFiles(); + return app; + } + + } diff --git a/src/Yavsc/Helpers/GoogleOAuthHelpers.cs b/src/Yavsc/Helpers/GoogleOAuthHelpers.cs index 1e62b037..c772f1e8 100644 --- a/src/Yavsc/Helpers/GoogleOAuthHelpers.cs +++ b/src/Yavsc/Helpers/GoogleOAuthHelpers.cs @@ -74,7 +74,7 @@ namespace Yavsc.Helpers ); return googleLogin; } - public static async Task GetGoogleCredential(GoogleAuthSettings googleAuthSettings, IDataStore store, string googleUserLoginKey) + public static async Task GetGoogleCredential(GoogleAuthSettings googleAuthSettings, IDataStore store, string googleUserLoginKey) { if (string.IsNullOrEmpty(googleUserLoginKey)) throw new InvalidOperationException("No Google login"); @@ -82,7 +82,7 @@ namespace Yavsc.Helpers var token = await store.GetAsync(googleUserLoginKey); // token != null var c = SystemClock.Default; - if (token.IsExpired(c)) { + if (token.IsStale) { token = await RefreshToken(googleAuthSettings, token); } return new UserCredential(flow, googleUserLoginKey, token); diff --git a/src/sampleWebAsWebApiClient/Program.cs b/src/sampleWebAsWebApiClient/Program.cs index 5cc25c36..82938292 100644 --- a/src/sampleWebAsWebApiClient/Program.cs +++ b/src/sampleWebAsWebApiClient/Program.cs @@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Authentication; JwtSecurityTokenHandler.DefaultMapInboundClaims = false; + var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews(); @@ -28,8 +29,7 @@ builder.Services .AddCookie("Cookies") .AddOpenIdConnect("oidc", options => { - options.Authority = "https://localhost:5001"; - + options.Authority = builder.Configuration.GetValue("AuthIssuer"); options.ClientId = "mvc"; options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0"; options.ResponseType = "code"; @@ -47,6 +47,7 @@ builder.Services using (var app = builder.Build()) { + if (app.Environment.IsDevelopment()) app.UseDeveloperExceptionPage(); else diff --git a/src/sampleWebAsWebApiClient/appsettings.json b/src/sampleWebAsWebApiClient/appsettings.json index 405c9bc0..8fe2cb9c 100755 --- a/src/sampleWebAsWebApiClient/appsettings.json +++ b/src/sampleWebAsWebApiClient/appsettings.json @@ -6,6 +6,7 @@ } }, "AllowedHosts": "*", + "AuthIssuer": "https://localhost:5001", "Kestrel": { "Endpoints": {