From 025195627c0faf30dbde05b0dcde616663186c3b Mon Sep 17 00:00:00 2001
From: Kamigen <46357922+Edouard127@users.noreply.github.com>
Date: Mon, 29 Apr 2024 18:30:04 -0400
Subject: [PATCH] Test: Integrated google auth
---
Directory.Packages.props | 2 +
.../Common/Interfaces/IGoogleService.cs | 8 ----
.../Google/Commands/CreateGoogleUser.cs | 20 ---------
src/Infrastructure/Infrastructure.csproj | 1 +
src/Infrastructure/Services/GoogleService.cs | 24 -----------
src/Web/Endpoints/Google.cs | 32 --------------
src/Web/Program.cs | 18 +++++++-
src/Web/Web.csproj | 2 +
src/Web/wwwroot/api/specification.json | 42 -------------------
9 files changed, 22 insertions(+), 127 deletions(-)
delete mode 100644 src/Application/Common/Interfaces/IGoogleService.cs
delete mode 100644 src/Application/Google/Commands/CreateGoogleUser.cs
delete mode 100644 src/Infrastructure/Services/GoogleService.cs
delete mode 100644 src/Web/Endpoints/Google.cs
diff --git a/Directory.Packages.props b/Directory.Packages.props
index 758ff65..d3ae35d 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -14,6 +14,8 @@
+
+
diff --git a/src/Application/Common/Interfaces/IGoogleService.cs b/src/Application/Common/Interfaces/IGoogleService.cs
deleted file mode 100644
index aba62f7..0000000
--- a/src/Application/Common/Interfaces/IGoogleService.cs
+++ /dev/null
@@ -1,8 +0,0 @@
-using Google.Apis.Oauth2.v2.Data;
-
-namespace Hutopy.Application.Common.Interfaces;
-
-public interface IGoogleService
-{
- Task GetUserInfoAsync(string accessToken);
-}
diff --git a/src/Application/Google/Commands/CreateGoogleUser.cs b/src/Application/Google/Commands/CreateGoogleUser.cs
deleted file mode 100644
index a5f2bca..0000000
--- a/src/Application/Google/Commands/CreateGoogleUser.cs
+++ /dev/null
@@ -1,20 +0,0 @@
-using Hutopy.Application.Common.Interfaces;
-
-namespace Hutopy.Application.Google.Commands;
-
-public record CreateGoogleUserCommand : IRequest
-{
- public required string AccessToken { get; init; }
-}
-
-public class CreateGoogleUser(
- IApplicationDbContext context
- ) : IRequestHandler
-{
- public async Task Handle(CreateGoogleUserCommand request, CancellationToken cancellationToken)
- {
- await context.SaveChangesAsync(cancellationToken);
-
- return Guid.NewGuid();
- }
-}
diff --git a/src/Infrastructure/Infrastructure.csproj b/src/Infrastructure/Infrastructure.csproj
index 2958bd6..108c958 100644
--- a/src/Infrastructure/Infrastructure.csproj
+++ b/src/Infrastructure/Infrastructure.csproj
@@ -4,6 +4,7 @@
Hutopy.Infrastructure
+
diff --git a/src/Infrastructure/Services/GoogleService.cs b/src/Infrastructure/Services/GoogleService.cs
deleted file mode 100644
index b8abdf2..0000000
--- a/src/Infrastructure/Services/GoogleService.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-using Google.Apis.Auth.OAuth2;
-using Google.Apis.Services;
-using Google.Apis.Oauth2.v2;
-using Google.Apis.Oauth2.v2.Data;
-using Hutopy.Application.Common.Interfaces;
-
-namespace Hutopy.Infrastructure.Services;
-
-public class GoogleService : IGoogleService
-{
- public async Task GetUserInfoAsync(string accessToken)
- {
- var user = GoogleCredential.FromAccessToken(accessToken);
-
- var service = new Oauth2Service(
- new BaseClientService.Initializer
- {
- HttpClientInitializer = user,
- ApplicationName = "Hutopy"
- });
-
- return await service.Userinfo.Get().ExecuteAsync();
- }
-}
diff --git a/src/Web/Endpoints/Google.cs b/src/Web/Endpoints/Google.cs
deleted file mode 100644
index cd75695..0000000
--- a/src/Web/Endpoints/Google.cs
+++ /dev/null
@@ -1,32 +0,0 @@
-using Hutopy.Application.Common.Interfaces;
-using Hutopy.Application.Google.Commands;
-using Hutopy.Domain.Interfaces;
-
-namespace Hutopy.Web.Endpoints;
-
-public class Google : EndpointGroupBase
-{
- public override void Map(WebApplication app)
- {
- app.MapGroup(this)
- .MapPost(CreateGoogleUser);
- }
-
- public static async Task CreateGoogleUser(ISender sender, CreateGoogleUserCommand command, IUserService userService, IGoogleService googleService)
- {
- var googleUser = await googleService.GetUserInfoAsync(command.AccessToken) ?? throw new Exception("Failed to get user info from Google");
-
-
-
- var user = await userService.FindUserByEmailAsync(googleUser.Email);
- if (user != null)
- {
- // TODO: Return login information
- return await sender.Send(command);
- }
-
- await userService.CreateUserAsync(googleUser);
-
- return await sender.Send(command);
- }
-}
diff --git a/src/Web/Program.cs b/src/Web/Program.cs
index 76fb1d8..0e3863c 100644
--- a/src/Web/Program.cs
+++ b/src/Web/Program.cs
@@ -6,6 +6,9 @@ using Hutopy.Infrastructure.Data;
using Hutopy.Infrastructure.Services;
using Hutopy.Web;
using Azure.Identity;
+using Microsoft.AspNetCore.Authentication.Cookies;
+using Microsoft.AspNetCore.Authentication.Google;
+using Microsoft.AspNetCore.Identity;
var builder = WebApplication.CreateBuilder(args);
@@ -48,8 +51,21 @@ builder.Services.AddApplicationServices();
builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Services.AddWebServices();
+// OAuth
+builder.Services.AddAuthentication()
+ .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";
+ });
+ /*.AddFacebook(options =>
+ {
+ options.AppId = ""; // TODO
+ options.AppSecret = ""; // TODO
+ });*/ // We can add a lot more if needed, microsoft, twitter, etc.
+
builder.Services.AddScoped();
-builder.Services.AddScoped();
var app = builder.Build();
diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj
index a5444e7..d5e0c62 100644
--- a/src/Web/Web.csproj
+++ b/src/Web/Web.csproj
@@ -14,6 +14,8 @@
+
+
diff --git a/src/Web/wwwroot/api/specification.json b/src/Web/wwwroot/api/specification.json
index f5515cb..5a1838c 100644
--- a/src/Web/wwwroot/api/specification.json
+++ b/src/Web/wwwroot/api/specification.json
@@ -26,39 +26,6 @@
}
}
},
- "/api/Google": {
- "post": {
- "tags": [
- "Google"
- ],
- "operationId": "CreateGoogleUser",
- "requestBody": {
- "x-name": "command",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/CreateGoogleUserCommand"
- }
- }
- },
- "required": true,
- "x-position": 1
- },
- "responses": {
- "200": {
- "description": "",
- "content": {
- "application/json": {
- "schema": {
- "type": "string",
- "format": "guid"
- }
- }
- }
- }
- }
- }
- },
"/api/JoinUs": {
"get": {
"tags": [
@@ -682,15 +649,6 @@
}
}
},
- "CreateGoogleUserCommand": {
- "type": "object",
- "additionalProperties": false,
- "properties": {
- "accessToken": {
- "type": "string"
- }
- }
- },
"PaginatedListOfFutureCreatorListDto": {
"type": "object",
"additionalProperties": false,