-
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.
feat(authentication,dotnet8): bumped dotnet and implemented auth
- Loading branch information
Samuel Verdejo
committed
Nov 16, 2023
1 parent
b5033fc
commit 1140e93
Showing
32 changed files
with
401 additions
and
53 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,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "chrome", | ||
"request": "launch", | ||
"name": "Launch Chrome against localhost", | ||
"url": "http://localhost:8080", | ||
"webRoot": "${workspaceFolder}" | ||
} | ||
] | ||
} |
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
2 changes: 1 addition & 1 deletion
2
Application/Budgets/Commands/Handlers/DeleteBudgetCommandHandler.cs
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,8 @@ | ||
using Domain; | ||
|
||
namespace Application; | ||
|
||
public interface IIdentityService | ||
{ | ||
public Task<string> GenerateToken(User user); | ||
} |
33 changes: 33 additions & 0 deletions
33
Application/Users/Commands/Handlers/LoginCommandHandler.cs
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,33 @@ | ||
using System.Reflection.Metadata; | ||
using Application.Shared.Command; | ||
using Domain; | ||
|
||
namespace Application; | ||
|
||
public sealed class LoginCommandHandler : ICommandHandler<LoginCommand, string> | ||
{ | ||
private readonly IUserRepository _userRepository; | ||
|
||
private readonly IIdentityService _identityService; | ||
|
||
public LoginCommandHandler(IUserRepository userRepository, IIdentityService identityService) | ||
{ | ||
_userRepository = userRepository; | ||
_identityService = identityService; | ||
} | ||
|
||
public async Task<string> Handle(LoginCommand request, CancellationToken cancellationToken = default) | ||
{ | ||
var mail = UserMail.Create(request.Mail); | ||
var criteria = new UserByMailCriteria(mail); | ||
|
||
User? user = await _userRepository.MatchFirstOrDefault(criteria); | ||
|
||
if (user is null) | ||
throw new UserNotFoundException(mail); | ||
|
||
var token = await _identityService.GenerateToken(user); | ||
|
||
return token; | ||
} | ||
} |
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,5 @@ | ||
using Application.Shared.Command; | ||
|
||
namespace Application; | ||
|
||
public sealed record LoginCommand(string Mail) : ICommand<string>; |
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,25 @@ | ||
using Domain.Shared.Base; | ||
|
||
namespace Domain; | ||
|
||
public class User : AggregateRoot<User> | ||
{ | ||
public UserId Id { get; private set; } | ||
|
||
public UserMail Mail { get; private set; } | ||
|
||
protected User(UserId id, UserMail mail) | ||
{ | ||
Id = id; | ||
Mail = mail; | ||
} | ||
|
||
public static User Create(UserId id, UserMail mail) | ||
{ | ||
var user = new User(id, mail); | ||
|
||
user.RecordEvent(new UserCreatedEvent(id.Value, mail.Value)); | ||
|
||
return user; | ||
} | ||
} |
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,14 @@ | ||
using Domain.Shared.Base; | ||
|
||
namespace Domain; | ||
|
||
public class UserNotFoundException : DomainException | ||
{ | ||
public UserNotFoundException(UserMail mail) : base($"The user with mail [{mail.Value}] has not been found in the database.") | ||
{ | ||
} | ||
|
||
public UserNotFoundException(UserId id) : base($"The user with id [{id.Value}] has not been found in the database.") | ||
{ | ||
} | ||
} |
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,5 @@ | ||
using Domain.Shared.Base; | ||
|
||
namespace Domain; | ||
|
||
public sealed record UserCreatedEvent(Guid id, string Mail) : IDomainEvent; |
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,11 @@ | ||
using System.Linq.Expressions; | ||
using Domain.Shared.Base; | ||
|
||
namespace Domain; | ||
|
||
public class UserByMailCriteria : Criteria<User> | ||
{ | ||
public UserByMailCriteria(UserMail mail) : base(user => user.Mail == mail) | ||
{ | ||
} | ||
} |
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 @@ | ||
using Domain.Shared.Base; | ||
|
||
namespace Domain; | ||
|
||
public interface IUserRepository : IRepository<User> | ||
{ | ||
} |
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 @@ | ||
namespace Domain; | ||
|
||
public record UserId | ||
{ | ||
public Guid Value { get; private set; } | ||
|
||
private UserId(Guid value) => | ||
Value = value; | ||
|
||
public static UserId Create(Guid value) => | ||
new(value); | ||
} |
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 @@ | ||
namespace Domain; | ||
|
||
public record UserMail | ||
{ | ||
public string Value { get; private set; } | ||
|
||
private UserMail(string value) => | ||
Value = value; | ||
|
||
public static UserMail Create(string value) => | ||
new(value); | ||
} |
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,43 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Application; | ||
using Domain; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Infrastructure; | ||
|
||
public class IdentityService : IIdentityService | ||
{ | ||
private readonly JwtOptions _options; | ||
|
||
public IdentityService(IOptions<JwtOptions> options) | ||
{ | ||
_options = options.Value; | ||
} | ||
|
||
public async Task<string> GenerateToken(User user) | ||
Check warning on line 20 in Infrastructure/Authentication/IdentityService.cs GitHub Actions / Build project and run Architectural and Unit testing
|
||
{ | ||
var claims = new Claim[] | ||
{ | ||
new Claim(JwtRegisteredClaimNames.Sub, user.Id.Value.ToString()), | ||
new Claim(JwtRegisteredClaimNames.Email, user.Mail.Value) | ||
}; | ||
|
||
var credentials = new SigningCredentials( | ||
new SymmetricSecurityKey( | ||
Encoding.UTF8.GetBytes(_options.SecretKey)), | ||
SecurityAlgorithms.HmacSha256); | ||
|
||
var token = new JwtSecurityToken( | ||
_options.Issuer, | ||
_options.Audience, | ||
claims, null, | ||
DateTime.UtcNow.AddDays(1), | ||
credentials); | ||
|
||
return new JwtSecurityTokenHandler() | ||
.WriteToken(token); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
Infrastructure/Authentication/Options/JwtBearerOptionsSetup.cs
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,31 @@ | ||
using System.Text; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Infrastructure; | ||
|
||
public class JwtBearerOptionsSetup : IConfigureOptions<JwtBearerOptions> | ||
{ | ||
private readonly JwtOptions _options; | ||
|
||
public JwtBearerOptionsSetup(IOptions<JwtOptions> options) | ||
{ | ||
_options = options.Value; | ||
} | ||
|
||
public void Configure(JwtBearerOptions options) | ||
{ | ||
options.TokenValidationParameters = new() | ||
{ | ||
ValidateIssuer = true, | ||
ValidateAudience = true, | ||
ValidateLifetime = true, | ||
ValidateIssuerSigningKey = true, | ||
ValidIssuer = _options.Issuer, | ||
ValidAudience = _options.Audience, | ||
IssuerSigningKey = new SymmetricSecurityKey( | ||
Encoding.UTF8.GetBytes(_options.SecretKey)) | ||
}; | ||
} | ||
} |
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,10 @@ | ||
namespace Infrastructure; | ||
|
||
public class JwtOptions | ||
{ | ||
public string Issuer { get; init; } | ||
Check warning on line 5 in Infrastructure/Authentication/Options/JwtOptions.cs GitHub Actions / Build project and run Architectural and Unit testing
|
||
|
||
public string Audience { get; init; } | ||
Check warning on line 7 in Infrastructure/Authentication/Options/JwtOptions.cs GitHub Actions / Build project and run Architectural and Unit testing
|
||
|
||
public string SecretKey { get; init; } | ||
Check warning on line 9 in Infrastructure/Authentication/Options/JwtOptions.cs GitHub Actions / Build project and run Architectural and Unit testing
|
||
} |
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,21 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Infrastructure; | ||
|
||
public class JwtOptionsSetup : IConfigureOptions<JwtOptions> | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
private const string SectionName = nameof(JwtOptions); | ||
|
||
public JwtOptionsSetup(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public void Configure(JwtOptions options) | ||
{ | ||
_configuration.GetSection(SectionName).Bind(options); | ||
} | ||
} |
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
Oops, something went wrong.