Skip to content

Commit

Permalink
Added custom google handler with prompt = 'select_account'
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Jan 19, 2024
1 parent 676e148 commit 9108230
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
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);
}
}
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);
}
}
3 changes: 2 additions & 1 deletion src/backend/src/FoodDiary.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using System.Threading.Tasks;
using FoodDiary.API.Authentication;
using FoodDiary.API.Extensions;
using FoodDiary.API.Middlewares;
using FoodDiary.API.Options;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void ConfigureServices(IServiceCollection services)
return Task.CompletedTask;
};
})
.AddGoogle(Constants.AuthenticationSchemes.OAuthGoogle, options =>
.AddCustomGoogle(Constants.AuthenticationSchemes.OAuthGoogle, options =>
{
options.SignInScheme = Constants.AuthenticationSchemes.Cookie;
options.ClientId = _googleAuthOptions.ClientId;
Expand Down

0 comments on commit 9108230

Please sign in to comment.