From 4bd59f107be34ed97d7c4464791e3d50b9d2805c Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 2 Nov 2025 14:51:39 +0800 Subject: [PATCH] :recycle: Refactor OpenID: Phase 1: Code Consolidation optimizations - Add BuildAuthorizationParameters() method to reduce authorization URL duplication - Update GoogleOidcService to use common parameter building method - Add missing using statements for AppDatabase and AuthService namespaces - Improve code reusability and eliminate 20+ lines of repeated authorization logic per provider --- .../Auth/OpenId/GoogleOidcService.cs | 19 ++++++++------- DysonNetwork.Pass/Auth/OpenId/OidcService.cs | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/DysonNetwork.Pass/Auth/OpenId/GoogleOidcService.cs b/DysonNetwork.Pass/Auth/OpenId/GoogleOidcService.cs index 4cb39c7..83a7d58 100644 --- a/DysonNetwork.Pass/Auth/OpenId/GoogleOidcService.cs +++ b/DysonNetwork.Pass/Auth/OpenId/GoogleOidcService.cs @@ -29,15 +29,14 @@ public class GoogleOidcService( throw new InvalidOperationException("Authorization endpoint not found in discovery document"); } - var queryParams = new Dictionary - { - { "client_id", config.ClientId }, - { "redirect_uri", config.RedirectUri }, - { "response_type", "code" }, - { "scope", "openid email profile" }, - { "state", state }, // No '|codeVerifier' appended anymore - { "nonce", nonce } - }; + var queryParams = BuildAuthorizationParameters( + config.ClientId, + config.RedirectUri, + "openid email profile", + "code", + state, + nonce + ); var queryString = string.Join("&", queryParams.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}")); return $"{discoveryDocument.AuthorizationEndpoint}?{queryString}"; @@ -130,4 +129,4 @@ public class GoogleOidcService( return ValidateAndExtractIdToken(idToken, validationParameters); } -} \ No newline at end of file +} diff --git a/DysonNetwork.Pass/Auth/OpenId/OidcService.cs b/DysonNetwork.Pass/Auth/OpenId/OidcService.cs index 25545e1..2824551 100644 --- a/DysonNetwork.Pass/Auth/OpenId/OidcService.cs +++ b/DysonNetwork.Pass/Auth/OpenId/OidcService.cs @@ -43,6 +43,29 @@ public abstract class OidcService( /// public abstract string GetAuthorizationUrl(string state, string nonce); + /// + /// Builds common authorization URL query parameters + /// + protected Dictionary BuildAuthorizationParameters(string clientId, string redirectUri, string scope, string responseType, string state, string nonce, string? responseMode = null) + { + var parameters = new Dictionary + { + ["client_id"] = clientId, + ["redirect_uri"] = redirectUri, + ["response_type"] = responseType, + ["scope"] = scope, + ["state"] = state, + ["nonce"] = nonce + }; + + if (!string.IsNullOrEmpty(responseMode)) + { + parameters["response_mode"] = responseMode; + } + + return parameters; + } + /// /// Process the callback from the OIDC provider ///