-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using EduAutomation.Domain.Auth0; | ||
|
||
namespace EduAutomation.Application.Auth0; | ||
|
||
public interface IAuthService | ||
{ | ||
Task<string?> Login(LoginDto dto); | ||
Task<string?> Register(RegisterDto dto); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace EduAutomation.Domain.Auth0; | ||
|
||
public record LoginDto(string Login, string Password); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace EduAutomation.Domain.Auth0; | ||
|
||
public record RegisterDto( | ||
string Login, | ||
string FullName, | ||
string Password, | ||
string Email); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace EduAutomation.Infrastructure.Auth0; | ||
|
||
public class Auth0Options | ||
{ | ||
public const string SectionName = "Auth0"; | ||
|
||
public required string ClientId { get; init; } | ||
public required string ClientSecret { get; init; } | ||
public required string Domain { get; init; } | ||
public required string Authority { get; init; } | ||
public required string Audience { get; init; } | ||
public required string Realm { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Auth0.AuthenticationApi; | ||
using Auth0.AuthenticationApi.Models; | ||
using EduAutomation.Application.Auth0; | ||
using EduAutomation.Domain.Auth0; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace EduAutomation.Infrastructure.Auth0; | ||
|
||
public class AuthService( | ||
IAuthenticationApiClient authenticationApiClient, | ||
IOptions<Auth0Options> auth0Options) : IAuthService | ||
{ | ||
private readonly Auth0Options _auth0Options = auth0Options.Value; | ||
|
||
public async Task<string?> Login(LoginDto dto) | ||
{ | ||
try | ||
{ | ||
var auth0Response = await authenticationApiClient.GetTokenAsync(new ResourceOwnerTokenRequest | ||
{ | ||
Username = dto.Login, | ||
Password = dto.Password, | ||
Realm = _auth0Options.Realm, | ||
|
||
Audience = _auth0Options.Audience, | ||
ClientId = _auth0Options.ClientId, | ||
ClientSecret = _auth0Options.ClientSecret, | ||
Scope = "openid" | ||
}); | ||
|
||
return !string.IsNullOrEmpty(auth0Response?.AccessToken ?? null) | ||
? auth0Response!.AccessToken : null; | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
return null; | ||
} | ||
} | ||
|
||
public async Task<string?> Register(RegisterDto dto) | ||
{ | ||
try | ||
{ | ||
var username = $"{dto.FullName[0]}-{Guid.NewGuid().ToString()[..6]}"; | ||
await authenticationApiClient.SignupUserAsync(new SignupUserRequest | ||
{ | ||
Username = username, | ||
Nickname = username, | ||
Name = dto.FullName , | ||
Password = dto.Password, | ||
Email = dto.Email, | ||
|
||
Connection = "edu-automation", | ||
ClientId = _auth0Options.ClientId | ||
}); | ||
|
||
var jwtToken = await Login(new LoginDto(username, dto.Password)); | ||
return jwtToken; | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using EduAutomation.Application.Auth0; | ||
using EduAutomation.Rest.Auth0.Mappers; | ||
using EduAutomation.Rest.Auth0.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace EduAutomation.Rest.Auth0; | ||
|
||
public static class AuthEndpoints | ||
{ | ||
public static async Task<IResult> Login( | ||
LoginRequest payload, | ||
CancellationToken cancellationToken, | ||
[FromServices] IAuthService authService) | ||
{ | ||
var loginDto = payload.ToDomainModel(); | ||
var jwtToken = await authService.Login(loginDto); | ||
|
||
return !string.IsNullOrEmpty(jwtToken) | ||
? Results.Ok(jwtToken) | ||
: Results.Unauthorized(); | ||
} | ||
|
||
public static async Task<IResult> Register( | ||
RegisterRequest payload, | ||
CancellationToken cancellationToken, | ||
[FromServices] IAuthService authService) | ||
{ | ||
var registerDto = payload.ToDomainModel(); | ||
var jwtToken = await authService.Register(registerDto); | ||
|
||
return !string.IsNullOrEmpty(jwtToken) | ||
? Results.Ok(jwtToken) | ||
: Results.Unauthorized(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using EduAutomation.Domain.Auth0; | ||
using EduAutomation.Rest.Auth0.Models; | ||
|
||
namespace EduAutomation.Rest.Auth0.Mappers; | ||
|
||
public static class LoginRequestMapper | ||
{ | ||
public static LoginDto ToDomainModel(this LoginRequest r) | ||
{ | ||
return new LoginDto(r.Login, r.Password); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using EduAutomation.Domain.Auth0; | ||
using EduAutomation.Rest.Auth0.Models; | ||
|
||
namespace EduAutomation.Rest.Auth0.Mappers; | ||
|
||
public static class RegisterRequestMapper | ||
{ | ||
public static RegisterDto ToDomainModel(this RegisterRequest r) | ||
{ | ||
return new RegisterDto(r.Login, r.FullName, r.Password, r.Email); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace EduAutomation.Rest.Auth0.Models; | ||
|
||
public record LoginRequest(string Login, string Password); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace EduAutomation.Rest.Auth0.Models; | ||
|
||
public record RegisterRequest( | ||
string Login, | ||
string FullName, | ||
string Password, | ||
string Email); |