♻️ 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:
@@ -29,15 +29,14 @@ public class GoogleOidcService(
|
|||||||
throw new InvalidOperationException("Authorization endpoint not found in discovery document");
|
throw new InvalidOperationException("Authorization endpoint not found in discovery document");
|
||||||
}
|
}
|
||||||
|
|
||||||
var queryParams = new Dictionary<string, string>
|
var queryParams = BuildAuthorizationParameters(
|
||||||
{
|
config.ClientId,
|
||||||
{ "client_id", config.ClientId },
|
config.RedirectUri,
|
||||||
{ "redirect_uri", config.RedirectUri },
|
"openid email profile",
|
||||||
{ "response_type", "code" },
|
"code",
|
||||||
{ "scope", "openid email profile" },
|
state,
|
||||||
{ "state", state }, // No '|codeVerifier' appended anymore
|
nonce
|
||||||
{ "nonce", nonce }
|
);
|
||||||
};
|
|
||||||
|
|
||||||
var queryString = string.Join("&", queryParams.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}"));
|
var queryString = string.Join("&", queryParams.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}"));
|
||||||
return $"{discoveryDocument.AuthorizationEndpoint}?{queryString}";
|
return $"{discoveryDocument.AuthorizationEndpoint}?{queryString}";
|
||||||
|
|||||||
@@ -43,6 +43,29 @@ public abstract class OidcService(
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract string GetAuthorizationUrl(string state, string nonce);
|
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>
|
/// <summary>
|
||||||
/// Process the callback from the OIDC provider
|
/// Process the callback from the OIDC provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user