From c5b7b9ab7e7263924b45821dcd4d0df685e64933 Mon Sep 17 00:00:00 2001 From: HaiHungNguyenn Date: Tue, 23 Jan 2024 17:41:24 +0700 Subject: [PATCH] [GGAuthen][Hai] config credential --- Domus.Api/Controllers/AuthController.cs | 9 ++++++++ Domus.Api/Controllers/FileController.cs | 2 +- .../Extensions/ServiceCollectionExtensions.cs | 22 ++++++++++++++++++- Domus.Api/Program.cs | 6 +++++ Domus.Common/Settings/GoogleSettings.cs | 7 ++++++ Domus.Service/Domus.Service.csproj | 1 + Domus.Service/Models/Email/Email.cs | 7 +++++- 7 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 Domus.Common/Settings/GoogleSettings.cs diff --git a/Domus.Api/Controllers/AuthController.cs b/Domus.Api/Controllers/AuthController.cs index 54304d6..c528ced 100644 --- a/Domus.Api/Controllers/AuthController.cs +++ b/Domus.Api/Controllers/AuthController.cs @@ -1,6 +1,10 @@ using Domus.Api.Controllers.Base; using Domus.Service.Interfaces; using Domus.Service.Models.Requests; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.Google; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Domus.Api.Controllers; @@ -30,7 +34,9 @@ public async Task Login(LoginRequest request) async () => await _authService.LoginAsync(request).ConfigureAwait(false) ).ConfigureAwait(false); } + + [HttpPost("refresh-token")] public async Task RefreshToken(RefreshTokenRequest request) { @@ -38,4 +44,7 @@ public async Task RefreshToken(RefreshTokenRequest request) async () => await _authService.RefreshTokenAsync(request).ConfigureAwait(false) ).ConfigureAwait(false); } + + + } diff --git a/Domus.Api/Controllers/FileController.cs b/Domus.Api/Controllers/FileController.cs index 51bec76..bb9acb0 100644 --- a/Domus.Api/Controllers/FileController.cs +++ b/Domus.Api/Controllers/FileController.cs @@ -1,6 +1,7 @@ using Domus.Api.Controllers.Base; using Domus.Service.Interfaces; using Domus.Service.Models.Common; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Domus.Api.Controllers; @@ -13,7 +14,6 @@ public FileController(IFileService fileService) { _fileService = fileService; } - [HttpGet("/get")] public async Task GetFile(string fileName) { diff --git a/Domus.Api/Extensions/ServiceCollectionExtensions.cs b/Domus.Api/Extensions/ServiceCollectionExtensions.cs index 0b62147..c318a43 100644 --- a/Domus.Api/Extensions/ServiceCollectionExtensions.cs +++ b/Domus.Api/Extensions/ServiceCollectionExtensions.cs @@ -14,6 +14,8 @@ using Domus.Service.AutoMappings; using Domus.Service.Implementations; using Domus.Service.Interfaces; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.Google; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.IdentityModel.Tokens; @@ -70,7 +72,22 @@ public static IServiceCollection AddJwtAuthentication(this IServiceCollection se ClockSkew = TimeSpan.Zero }; }); - + return services; + } + + public static IServiceCollection AddGgAuthentication(this IServiceCollection services, IConfiguration configuration) + { + var googleSettings = configuration.GetSection(nameof(GoogleSettings)).Get(); + services.AddAuthentication(options => + { + options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme; + }).AddCookie() + .AddGoogle(GoogleDefaults.AuthenticationScheme, options => + { + options.ClientId = googleSettings.ClientId; + options.ClientSecret = googleSettings.ClientSecret; + }); return services; } @@ -122,4 +139,7 @@ public static IServiceCollection RegisterServices(this IServiceCollection servic return services; } + + + } diff --git a/Domus.Api/Program.cs b/Domus.Api/Program.cs index 144c306..cd4de41 100644 --- a/Domus.Api/Program.cs +++ b/Domus.Api/Program.cs @@ -1,5 +1,7 @@ using Domus.Api.Extensions; using Domus.Common.Helpers; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.Google; using NLog; LogManager.Setup() @@ -16,6 +18,10 @@ builder.Services.AddJwtAuthentication(builder.Configuration); builder.Services.AddDefaultCorsPolicy(builder.Configuration); builder.Services.RegisterServices(); +builder.Services.AddGgAuthentication(builder.Configuration); + + + var app = builder.Build(); diff --git a/Domus.Common/Settings/GoogleSettings.cs b/Domus.Common/Settings/GoogleSettings.cs new file mode 100644 index 0000000..93af6da --- /dev/null +++ b/Domus.Common/Settings/GoogleSettings.cs @@ -0,0 +1,7 @@ +namespace Domus.Common.Settings; + +public class GoogleSettings +{ + public string ClientId { get; set; } + public string ClientSecret { get; set; } +} \ No newline at end of file diff --git a/Domus.Service/Domus.Service.csproj b/Domus.Service/Domus.Service.csproj index 10c3b38..06edd63 100644 --- a/Domus.Service/Domus.Service.csproj +++ b/Domus.Service/Domus.Service.csproj @@ -9,6 +9,7 @@ + diff --git a/Domus.Service/Models/Email/Email.cs b/Domus.Service/Models/Email/Email.cs index acebe3e..d0275f6 100644 --- a/Domus.Service/Models/Email/Email.cs +++ b/Domus.Service/Models/Email/Email.cs @@ -1,8 +1,13 @@ -namespace Domus.Service.Models.Email; +using System.ComponentModel.DataAnnotations; + +namespace Domus.Service.Models.Email; public class Email { + [Required] public string To { get; set; } = String.Empty; + [Required] public string Subject { get; set; } = String.Empty; + [Required] public string EmailBody { get; set; } = String.Empty; } \ No newline at end of file