Skip to content

Commit

Permalink
[GGAuthen][Hai] config credential
Browse files Browse the repository at this point in the history
  • Loading branch information
HaiHungNguyenn committed Jan 23, 2024
1 parent 054494e commit c5b7b9a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Domus.Api/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,12 +34,17 @@ public async Task<IActionResult> Login(LoginRequest request)
async () => await _authService.LoginAsync(request).ConfigureAwait(false)
).ConfigureAwait(false);
}



[HttpPost("refresh-token")]
public async Task<IActionResult> RefreshToken(RefreshTokenRequest request)
{
return await ExecuteServiceLogic(
async () => await _authService.RefreshTokenAsync(request).ConfigureAwait(false)
).ConfigureAwait(false);
}



}
2 changes: 1 addition & 1 deletion Domus.Api/Controllers/FileController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +14,6 @@ public FileController(IFileService fileService)
{
_fileService = fileService;
}

[HttpGet("/get")]
public async Task<IActionResult> GetFile(string fileName)
{
Expand Down
22 changes: 21 additions & 1 deletion Domus.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GoogleSettings>();
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;
}

Expand Down Expand Up @@ -122,4 +139,7 @@ public static IServiceCollection RegisterServices(this IServiceCollection servic

return services;
}



}
6 changes: 6 additions & 0 deletions Domus.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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();

Expand Down
7 changes: 7 additions & 0 deletions Domus.Common/Settings/GoogleSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Domus.Common.Settings;

public class GoogleSettings
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
}
1 change: 1 addition & 0 deletions Domus.Service/Domus.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0-beta.1" />
<PackageReference Include="MailKit" Version="4.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="7.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
7 changes: 6 additions & 1 deletion Domus.Service/Models/Email/Email.cs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit c5b7b9a

Please sign in to comment.