♻️ 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
This commit is contained in:
2025-11-02 14:51:39 +08:00
parent 08f924f647
commit 4bd59f107b
2 changed files with 32 additions and 10 deletions

View File

@@ -29,15 +29,14 @@ public class GoogleOidcService(
throw new InvalidOperationException("Authorization endpoint not found in discovery document");
}
var queryParams = new Dictionary<string, string>
{
{ "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}";

View File

@@ -43,6 +43,29 @@ public abstract class OidcService(
/// </summary>
public abstract string GetAuthorizationUrl(string state, string nonce);
/// <summary>
/// Builds common authorization URL query parameters
/// </summary>
protected Dictionary<string, string> BuildAuthorizationParameters(string clientId, string redirectUri, string scope, string responseType, string state, string nonce, string? responseMode = null)
{
var parameters = new Dictionary<string, string>
{
["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;
}
/// <summary>
/// Process the callback from the OIDC provider
/// </summary>