Test: ASP Google login

This commit is contained in:
Kamigen
2024-05-01 17:54:21 -04:00
parent 025195627c
commit f9a661c8d2
3 changed files with 57 additions and 3 deletions

View File

@@ -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),
});
}
}

View File

@@ -1,11 +1,11 @@
using Hutopy.Application; using Hutopy.Application;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Domain.Interfaces; using Hutopy.Domain.Interfaces;
using Hutopy.Infrastructure; using Hutopy.Infrastructure;
using Hutopy.Infrastructure.Data; using Hutopy.Infrastructure.Data;
using Hutopy.Infrastructure.Services; using Hutopy.Infrastructure.Services;
using Hutopy.Web; using Hutopy.Web;
using Azure.Identity; using Azure.Identity;
using Hutopy.Infrastructure.Identity;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google; using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
@@ -52,12 +52,30 @@ builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Services.AddWebServices(); builder.Services.AddWebServices();
// OAuth // 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 => .AddGoogle(options =>
{ {
options.ClientId = builder.Configuration["Google:ClientId"] ?? throw new ArgumentNullException("The Google ClientId is missing."); 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.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 => /*.AddFacebook(options =>
{ {

View File

@@ -26,6 +26,19 @@
} }
} }
}, },
"/api/Google/o/sign-in": {
"get": {
"tags": [
"Google"
],
"operationId": "GetApiGoogleOSignIn",
"responses": {
"200": {
"description": ""
}
}
}
},
"/api/JoinUs": { "/api/JoinUs": {
"get": { "get": {
"tags": [ "tags": [