From f9a661c8d2be3cd33a5200a77ca25606318cc0ee Mon Sep 17 00:00:00 2001 From: Kamigen <46357922+Edouard127@users.noreply.github.com> Date: Wed, 1 May 2024 17:54:21 -0400 Subject: [PATCH] Test: ASP Google login --- src/Web/Endpoints/Google.cs | 23 +++++++++++++++++++++++ src/Web/Program.cs | 24 +++++++++++++++++++++--- src/Web/wwwroot/api/specification.json | 13 +++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/Web/Endpoints/Google.cs diff --git a/src/Web/Endpoints/Google.cs b/src/Web/Endpoints/Google.cs new file mode 100644 index 0000000..274426c --- /dev/null +++ b/src/Web/Endpoints/Google.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Google; + +namespace Hutopy.Web.Endpoints; + +public class Google : EndpointGroupBase +{ + public override void Map(WebApplication app) + { + app.MapGroup(this) + .MapGet("/o/sign-in", Callback); + } + + private static async Task Callback(ISender sender, HttpContext context) + { + await context.ChallengeAsync(GoogleDefaults.AuthenticationScheme, + new AuthenticationProperties + { + RedirectUri = "/o/google/callback", + ExpiresUtc = DateTimeOffset.UtcNow.AddDays(30), + }); + } +} diff --git a/src/Web/Program.cs b/src/Web/Program.cs index 0e3863c..1b712f9 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -1,11 +1,11 @@ using Hutopy.Application; -using Hutopy.Application.Common.Interfaces; using Hutopy.Domain.Interfaces; using Hutopy.Infrastructure; using Hutopy.Infrastructure.Data; using Hutopy.Infrastructure.Services; using Hutopy.Web; using Azure.Identity; +using Hutopy.Infrastructure.Identity; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Google; using Microsoft.AspNetCore.Identity; @@ -52,12 +52,30 @@ builder.Services.AddInfrastructureServices(builder.Configuration); builder.Services.AddWebServices(); // OAuth -builder.Services.AddAuthentication() +builder.Services.AddAuthentication(options => + { + options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme; + }) + .AddCookie(options => + { + options.Cookie.Name = "Hutopy"; + options.Cookie.SecurePolicy = builder.Environment.IsDevelopment() ? CookieSecurePolicy.None : CookieSecurePolicy.Always; + options.Cookie.SameSite = SameSiteMode.Strict; + options.Cookie.HttpOnly = true; + options.Cookie.IsEssential = true; + options.Cookie.MaxAge = TimeSpan.FromDays(30); + }) .AddGoogle(options => { options.ClientId = builder.Configuration["Google:ClientId"] ?? throw new ArgumentNullException("The Google ClientId is missing."); options.ClientSecret = builder.Configuration["Google:ClientSecret"] ?? throw new ArgumentNullException("The Google ClientSecret is missing."); - options.CallbackPath = "/api/google/o/signin-callback"; + options.CallbackPath = "/o/google/callback"; + options.Events.OnRedirectToAuthorizationEndpoint = context => + { + context.Response.Redirect(context.RedirectUri + "&prompt=consent"); + return Task.CompletedTask; + }; }); /*.AddFacebook(options => { diff --git a/src/Web/wwwroot/api/specification.json b/src/Web/wwwroot/api/specification.json index 5a1838c..c080bf3 100644 --- a/src/Web/wwwroot/api/specification.json +++ b/src/Web/wwwroot/api/specification.json @@ -26,6 +26,19 @@ } } }, + "/api/Google/o/sign-in": { + "get": { + "tags": [ + "Google" + ], + "operationId": "GetApiGoogleOSignIn", + "responses": { + "200": { + "description": "" + } + } + } + }, "/api/JoinUs": { "get": { "tags": [