From 8551a6363be77b78867bf87e19bddbcda0c8ffa5 Mon Sep 17 00:00:00 2001 From: S-Sebb <794194678@qq.com> Date: Sun, 2 Jun 2024 20:50:56 +0100 Subject: [PATCH] Added a separate flag on the backend that controls whether authentication checks are performed in apis --- TaikoLocalServer/Configurations/AuthSettings.json | 3 ++- TaikoLocalServer/Controllers/Api/AuthController.cs | 6 +++--- TaikoLocalServer/Controllers/Api/CardsController.cs | 4 ++-- .../Controllers/Api/DanBestDataController.cs | 2 +- .../Controllers/Api/FavoriteSongsController.cs | 4 ++-- TaikoLocalServer/Controllers/Api/GameDataController.cs | 10 +++++----- TaikoLocalServer/Controllers/Api/PlayDataController.cs | 2 +- .../Controllers/Api/PlayHistoryController.cs | 2 +- .../Controllers/Api/UserSettingsController.cs | 6 +++--- TaikoLocalServer/Controllers/Api/UsersController.cs | 6 +++--- .../Filters/AuthorizeIfRequiredAttribute.cs | 2 +- TaikoLocalServer/Program.cs | 4 ---- TaikoLocalServer/Settings/AuthSettings.cs | 2 +- 13 files changed, 25 insertions(+), 28 deletions(-) diff --git a/TaikoLocalServer/Configurations/AuthSettings.json b/TaikoLocalServer/Configurations/AuthSettings.json index 1bc26af..b1bea33 100644 --- a/TaikoLocalServer/Configurations/AuthSettings.json +++ b/TaikoLocalServer/Configurations/AuthSettings.json @@ -2,6 +2,7 @@ "AuthSettings": { "JwtKey": "SuperSecretKeyAndHeresItsPadding", "JwtIssuer": "http://localhost:5000", - "JwtAudience": "http://localhost:5000" + "JwtAudience": "http://localhost:5000", + "AuthenticationRequired": false } } \ No newline at end of file diff --git a/TaikoLocalServer/Controllers/Api/AuthController.cs b/TaikoLocalServer/Controllers/Api/AuthController.cs index 48b1edf..5bb5c81 100644 --- a/TaikoLocalServer/Controllers/Api/AuthController.cs +++ b/TaikoLocalServer/Controllers/Api/AuthController.cs @@ -182,7 +182,7 @@ public class AuthController(IAuthService authService, IUserDatumService userDatu [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task ChangePassword(ChangePasswordRequest changePasswordRequest) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) @@ -232,7 +232,7 @@ public class AuthController(IAuthService authService, IUserDatumService userDatu [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task ResetPassword(ResetPasswordRequest resetPasswordRequest) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) @@ -260,7 +260,7 @@ public class AuthController(IAuthService authService, IUserDatumService userDatu [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public IActionResult GenerateOtp(GenerateOtpRequest generateOtpRequest) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) diff --git a/TaikoLocalServer/Controllers/Api/CardsController.cs b/TaikoLocalServer/Controllers/Api/CardsController.cs index 9277f6a..10922fe 100644 --- a/TaikoLocalServer/Controllers/Api/CardsController.cs +++ b/TaikoLocalServer/Controllers/Api/CardsController.cs @@ -16,7 +16,7 @@ public class CardsController(IAuthService authService, IOptions se [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task DeleteAccessCode(string accessCode) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) @@ -45,7 +45,7 @@ public class CardsController(IAuthService authService, IOptions se [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task BindAccessCode(BindAccessCodeRequest bindAccessCodeRequest) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) diff --git a/TaikoLocalServer/Controllers/Api/DanBestDataController.cs b/TaikoLocalServer/Controllers/Api/DanBestDataController.cs index 6b60286..9bd2b6e 100644 --- a/TaikoLocalServer/Controllers/Api/DanBestDataController.cs +++ b/TaikoLocalServer/Controllers/Api/DanBestDataController.cs @@ -18,7 +18,7 @@ public class DanBestDataController(IDanScoreDatumService danScoreDatumService, I [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task GetDanBestData(uint baid) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) diff --git a/TaikoLocalServer/Controllers/Api/FavoriteSongsController.cs b/TaikoLocalServer/Controllers/Api/FavoriteSongsController.cs index d7c517c..56f75dd 100644 --- a/TaikoLocalServer/Controllers/Api/FavoriteSongsController.cs +++ b/TaikoLocalServer/Controllers/Api/FavoriteSongsController.cs @@ -16,7 +16,7 @@ public class FavoriteSongsController(IUserDatumService userDatumService, IAuthSe [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task UpdateFavoriteSong(SetFavoriteRequest request) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) @@ -45,7 +45,7 @@ public class FavoriteSongsController(IUserDatumService userDatumService, IAuthSe [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task GetFavoriteSongs(uint baid) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) diff --git a/TaikoLocalServer/Controllers/Api/GameDataController.cs b/TaikoLocalServer/Controllers/Api/GameDataController.cs index 5350474..e14c663 100644 --- a/TaikoLocalServer/Controllers/Api/GameDataController.cs +++ b/TaikoLocalServer/Controllers/Api/GameDataController.cs @@ -15,7 +15,7 @@ public class GameDataController(IGameDataService gameDataService, IAuthService a [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public IActionResult GetMusicDetails() { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) @@ -31,7 +31,7 @@ public class GameDataController(IGameDataService gameDataService, IAuthService a [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public IActionResult GetCostumes() { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) @@ -47,7 +47,7 @@ public class GameDataController(IGameDataService gameDataService, IAuthService a [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public IActionResult GetTitles() { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) @@ -63,7 +63,7 @@ public class GameDataController(IGameDataService gameDataService, IAuthService a [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public IActionResult GetLockedCostumes() { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) @@ -79,7 +79,7 @@ public class GameDataController(IGameDataService gameDataService, IAuthService a [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public IActionResult GetLockedTitles() { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) diff --git a/TaikoLocalServer/Controllers/Api/PlayDataController.cs b/TaikoLocalServer/Controllers/Api/PlayDataController.cs index 48f317d..7eeaea4 100644 --- a/TaikoLocalServer/Controllers/Api/PlayDataController.cs +++ b/TaikoLocalServer/Controllers/Api/PlayDataController.cs @@ -19,7 +19,7 @@ public class PlayDataController(IUserDatumService userDatumService, ISongBestDat [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task> GetSongBestRecords(uint baid) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) diff --git a/TaikoLocalServer/Controllers/Api/PlayHistoryController.cs b/TaikoLocalServer/Controllers/Api/PlayHistoryController.cs index a5eef15..d6e9931 100644 --- a/TaikoLocalServer/Controllers/Api/PlayHistoryController.cs +++ b/TaikoLocalServer/Controllers/Api/PlayHistoryController.cs @@ -17,7 +17,7 @@ public class PlayHistoryController(IUserDatumService userDatumService, ISongPlay [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task> GetSongHistory(uint baid) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) diff --git a/TaikoLocalServer/Controllers/Api/UserSettingsController.cs b/TaikoLocalServer/Controllers/Api/UserSettingsController.cs index 0ef05c9..98a4bd3 100644 --- a/TaikoLocalServer/Controllers/Api/UserSettingsController.cs +++ b/TaikoLocalServer/Controllers/Api/UserSettingsController.cs @@ -17,7 +17,7 @@ public class UserSettingsController(IUserDatumService userDatumService, IAuthSer [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task>> GetAllUserSetting() { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) @@ -96,7 +96,7 @@ public class UserSettingsController(IUserDatumService userDatumService, IAuthSer [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task> GetUserSetting(uint baid) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) @@ -172,7 +172,7 @@ public class UserSettingsController(IUserDatumService userDatumService, IAuthSer [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task SaveUserSetting(uint baid, UserSetting userSetting) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo is null) diff --git a/TaikoLocalServer/Controllers/Api/UsersController.cs b/TaikoLocalServer/Controllers/Api/UsersController.cs index eac83f6..f305a0d 100644 --- a/TaikoLocalServer/Controllers/Api/UsersController.cs +++ b/TaikoLocalServer/Controllers/Api/UsersController.cs @@ -16,7 +16,7 @@ public class UsersController(IUserDatumService userDatumService, IAuthService au [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task GetUser(uint baid) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) @@ -38,7 +38,7 @@ public class UsersController(IUserDatumService userDatumService, IAuthService au [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task> GetUsers() { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) @@ -59,7 +59,7 @@ public class UsersController(IUserDatumService userDatumService, IAuthService au [ServiceFilter(typeof(AuthorizeIfRequiredAttribute))] public async Task DeleteUser(uint baid) { - if (authSettings.LoginRequired) + if (authSettings.AuthenticationRequired) { var tokenInfo = authService.ExtractTokenInfo(HttpContext); if (tokenInfo == null) diff --git a/TaikoLocalServer/Filters/AuthorizeIfRequiredAttribute.cs b/TaikoLocalServer/Filters/AuthorizeIfRequiredAttribute.cs index 029b00f..a6db03d 100644 --- a/TaikoLocalServer/Filters/AuthorizeIfRequiredAttribute.cs +++ b/TaikoLocalServer/Filters/AuthorizeIfRequiredAttribute.cs @@ -8,7 +8,7 @@ namespace TaikoLocalServer.Filters [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class AuthorizeIfRequiredAttribute(IOptions settings) : Attribute, IAsyncAuthorizationFilter { - private readonly bool loginRequired = settings.Value.LoginRequired; + private readonly bool loginRequired = settings.Value.AuthenticationRequired; public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { diff --git a/TaikoLocalServer/Program.cs b/TaikoLocalServer/Program.cs index dcf3901..7713783 100644 --- a/TaikoLocalServer/Program.cs +++ b/TaikoLocalServer/Program.cs @@ -46,7 +46,6 @@ try builder.Configuration.AddJsonFile($"{configurationsDirectory}/ServerSettings.json", optional: false, reloadOnChange: false); builder.Configuration.AddJsonFile($"{configurationsDirectory}/DataSettings.json", optional: true, reloadOnChange: false); builder.Configuration.AddJsonFile($"{configurationsDirectory}/AuthSettings.json", optional: true, reloadOnChange: false); - builder.Configuration.AddJsonFile("wwwroot/appsettings.json", optional: false, reloadOnChange: false); builder.Host.UseSerilog((context, configuration) => { @@ -88,9 +87,6 @@ try builder.Services.Configure(builder.Configuration.GetSection(nameof(DataSettings))); builder.Services.Configure(builder.Configuration.GetSection(nameof(AuthSettings))); - var loginRequired = builder.Configuration.GetSection("WebUiSettings").GetValue("LoginRequired"); - builder.Services.Configure(options => { options.LoginRequired = loginRequired; }); - // Add Authentication with JWT builder.Services.AddAuthentication(options => { diff --git a/TaikoLocalServer/Settings/AuthSettings.cs b/TaikoLocalServer/Settings/AuthSettings.cs index 3e7fe69..24aee9f 100644 --- a/TaikoLocalServer/Settings/AuthSettings.cs +++ b/TaikoLocalServer/Settings/AuthSettings.cs @@ -8,5 +8,5 @@ public class AuthSettings public string JwtAudience { get; set; } = string.Empty; - public bool LoginRequired { get; set; } + public bool AuthenticationRequired { get; set; } } \ No newline at end of file