-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom google handler with prompt = 'select_account'
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/backend/src/FoodDiary.API/Authentication/AuthenticationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Google; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace FoodDiary.API.Authentication; | ||
|
||
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")] | ||
public static class AuthenticationExtensions | ||
{ | ||
public static AuthenticationBuilder AddCustomGoogle( | ||
this AuthenticationBuilder builder, | ||
string authenticationScheme, | ||
Action<GoogleOptions> configureOptions) | ||
{ | ||
return builder.AddOAuth<GoogleOptions, CustomGoogleHandler>( | ||
authenticationScheme, | ||
GoogleDefaults.DisplayName, | ||
configureOptions); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/backend/src/FoodDiary.API/Authentication/CustomGoogleHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text.Encodings.Web; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Google; | ||
using Microsoft.AspNetCore.Authentication.OAuth; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace FoodDiary.API.Authentication; | ||
|
||
public class CustomGoogleHandler(IOptionsMonitor<GoogleOptions> options, ILoggerFactory logger, UrlEncoder encoder) | ||
: GoogleHandler(options, logger, encoder) | ||
{ | ||
protected override string BuildChallengeUrl(AuthenticationProperties properties, string redirectUri) | ||
{ | ||
var query = QueryHelpers.ParseQuery(new Uri(base.BuildChallengeUrl(properties, redirectUri)).Query); | ||
|
||
SetQueryParam(query, properties, OAuthChallengeProperties.ScopeKey, base.FormatScope, Options.Scope); | ||
SetQueryParam(query, properties, GoogleChallengeProperties.AccessTypeKey, Options.AccessType); | ||
SetQueryParam(query, properties, GoogleChallengeProperties.ApprovalPromptKey); | ||
SetQueryParam(query, properties, GoogleChallengeProperties.PromptParameterKey, "select_account"); | ||
SetQueryParam(query, properties, GoogleChallengeProperties.LoginHintKey); | ||
SetQueryParam(query, properties, GoogleChallengeProperties.IncludeGrantedScopesKey, v => v?.ToString(CultureInfo.InvariantCulture).ToLowerInvariant(), new bool?()); | ||
|
||
query["state"] = (StringValues) Options.StateDataFormat.Protect(properties); | ||
|
||
return QueryHelpers.AddQueryString(Options.AuthorizationEndpoint, query); | ||
} | ||
|
||
private static void SetQueryParam<T>( | ||
IDictionary<string, StringValues> queryStrings, | ||
AuthenticationProperties properties, | ||
string name, | ||
Func<T, string> formatter, | ||
T defaultValue) | ||
{ | ||
var parameter = properties.GetParameter<T>(name); | ||
string str; | ||
if (parameter != null) | ||
str = formatter(parameter); | ||
else if (!properties.Items.TryGetValue(name, out str)) | ||
str = formatter(defaultValue); | ||
properties.Items.Remove(name); | ||
if (str == null) | ||
return; | ||
queryStrings[name] = (StringValues) str; | ||
} | ||
|
||
private static void SetQueryParam( | ||
IDictionary<string, StringValues> queryStrings, | ||
AuthenticationProperties properties, | ||
string name, | ||
string? defaultValue = null) | ||
{ | ||
SetQueryParam(queryStrings, properties, name, (Func<string, string>) (x => x), defaultValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters