Added challenge result

This commit is contained in:
Kamigen
2024-05-01 18:43:20 -04:00
parent b966e28d9a
commit cd2bf64af5
3 changed files with 35 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Google; using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Mvc;
namespace Hutopy.Web.Endpoints; namespace Hutopy.Web.Endpoints;
@@ -11,13 +12,15 @@ public class Google : EndpointGroupBase
.MapGet("/o/sign-in", Callback); .MapGet("/o/sign-in", Callback);
} }
private static async Task Callback(ISender sender, HttpContext context) private static async Task<IActionResult> Callback(ISender sender, HttpContext context)
{ {
await context.ChallengeAsync(GoogleDefaults.AuthenticationScheme, var properties = new AuthenticationProperties
new AuthenticationProperties
{ {
RedirectUri = "/signin-google", RedirectUri = "/signin-google", ExpiresUtc = DateTimeOffset.UtcNow.AddDays(30),
ExpiresUtc = DateTimeOffset.UtcNow.AddDays(30), };
});
await context.ChallengeAsync(GoogleDefaults.AuthenticationScheme, properties);
return new ChallengeResult(GoogleDefaults.AuthenticationScheme, properties);
} }
} }

View File

@@ -5,10 +5,7 @@ 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.Identity;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -52,11 +49,16 @@ builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Services.AddWebServices(); builder.Services.AddWebServices();
// OAuth // OAuth
builder.Services.AddAuthentication() builder.Services.AddAuthorization();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{ {
options.Cookie.Name = "Hutopy"; options.Cookie.Name = "Hutopy";
options.Cookie.SecurePolicy = builder.Environment.IsDevelopment() ? CookieSecurePolicy.None : CookieSecurePolicy.Always; options.Cookie.SecurePolicy =
builder.Environment.IsDevelopment() ? CookieSecurePolicy.None : CookieSecurePolicy.Always;
options.Cookie.SameSite = SameSiteMode.Strict; options.Cookie.SameSite = SameSiteMode.Strict;
options.Cookie.HttpOnly = true; options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true; options.Cookie.IsEssential = true;
@@ -64,8 +66,10 @@ builder.Services.AddAuthentication()
}) })
.AddGoogle(options => .AddGoogle(options =>
{ {
options.ClientId = builder.Configuration["Google:ClientId"] ?? throw new ArgumentNullException("The Google ClientId is missing."); options.ClientId = builder.Configuration["Google:ClientId"] ??
options.ClientSecret = builder.Configuration["Google:ClientSecret"] ?? throw new ArgumentNullException("The Google ClientSecret is missing."); throw new ArgumentNullException("The Google ClientId is missing.");
options.ClientSecret = builder.Configuration["Google:ClientSecret"] ??
throw new ArgumentNullException("The Google ClientSecret is missing.");
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Events.OnRedirectToAuthorizationEndpoint = context => options.Events.OnRedirectToAuthorizationEndpoint = context =>
{ {
@@ -73,16 +77,15 @@ builder.Services.AddAuthentication()
return Task.CompletedTask; return Task.CompletedTask;
}; };
}); });
/*.AddFacebook(options =>
{
options.AppId = ""; // TODO
options.AppSecret = ""; // TODO
});*/ // We can add a lot more if needed, microsoft, twitter, etc.
builder.Services.AddControllers();
builder.Services.AddScoped<IUserService, UserService>(); builder.Services.AddScoped<IUserService, UserService>();
var app = builder.Build(); var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.UseCors("AllowAll"); app.UseCors("AllowAll");
app.UseCors("AllowHutopyUi"); app.UseCors("AllowHutopyUi");
app.UseCors("AllowHutopyUiPreview"); app.UseCors("AllowHutopyUiPreview");
@@ -111,8 +114,6 @@ app.MapControllerRoute(
name: "default", name: "default",
pattern: "{controller}/{action=Index}/{id?}"); pattern: "{controller}/{action=Index}/{id?}");
app.MapRazorPages();
app.MapFallbackToFile("index.html"); app.MapFallbackToFile("index.html");
app.UseExceptionHandler(options => { }); app.UseExceptionHandler(options => { });

View File

@@ -34,7 +34,15 @@
"operationId": "GetApiGoogleOSignIn", "operationId": "GetApiGoogleOSignIn",
"responses": { "responses": {
"200": { "200": {
"description": "" "description": "",
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
} }
} }
} }