generated from StraykerPL/PaternRepo
-
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.
Merge pull request #154 from Strayker-Software/feature/153-add-authen…
…tication-and-authorization Add authentication and authorization
- Loading branch information
Showing
89 changed files
with
4,399 additions
and
1,367 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
2 changes: 2 additions & 0 deletions
2
binder-web-backend/Binder.Api/Controllers/AppVersionsController.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
42 changes: 42 additions & 0 deletions
42
binder-web-backend/Binder.Api/Controllers/AuthController.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,42 @@ | ||
using Binder.Api.Models; | ||
using Binder.Api.Providers.Interfaces; | ||
using Binder.Application.Services.Middleware; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using SimpleAuthentication.JwtBearer; | ||
using System.Security.Authentication; | ||
|
||
namespace Binder.Api.Controllers | ||
{ | ||
[Route("api/auth")] | ||
[ApiController] | ||
public sealed class AuthController : ControllerBase | ||
{ | ||
private readonly IJwtBearerService _jwtBearerService; | ||
private readonly IAuthProvider _authProvider; | ||
|
||
public AuthController( | ||
IJwtBearerService bearerService, | ||
IAuthProvider authProvider) | ||
{ | ||
_jwtBearerService = bearerService; | ||
_authProvider = authProvider; | ||
} | ||
|
||
[HttpPost] | ||
[Route("login")] | ||
[AllowAnonymous] | ||
public ActionResult<LoginResponseDTO> Login(LoginRequestDTO loginRequest) | ||
{ | ||
if (_authProvider.Authenticate(loginRequest.UserName, loginRequest.Password)) | ||
{ | ||
var token = _jwtBearerService.CreateToken(loginRequest.UserName); | ||
|
||
return Ok(new LoginResponseDTO(token)); | ||
} | ||
|
||
return Unauthorized(GetProblemDetailsByExceptionFactory | ||
.GetProblemDetails(new InvalidCredentialException())); | ||
} | ||
} | ||
} |
25 changes: 21 additions & 4 deletions
25
binder-web-backend/Binder.Api/Controllers/TablesController.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
24 changes: 19 additions & 5 deletions
24
binder-web-backend/Binder.Api/Controllers/ToDoTasksController.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
16 changes: 16 additions & 0 deletions
16
binder-web-backend/Binder.Api/Extensions/AddProvidersExtensions.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,16 @@ | ||
using Binder.Api.Providers; | ||
using Binder.Api.Providers.Interfaces; | ||
|
||
namespace Binder.Api.Extensions | ||
{ | ||
public static class AddProvidersExtensions | ||
{ | ||
public static IServiceCollection AddProviders(this IServiceCollection services) | ||
{ | ||
services.AddScoped<JwtDataProvider>(); | ||
services.AddScoped<IAuthProvider, UsernamePasswordAuthProvider>(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
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,4 @@ | ||
namespace Binder.Api.Models | ||
{ | ||
public record class LoginRequestDTO(string UserName, 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,4 @@ | ||
namespace Binder.Api.Models | ||
{ | ||
public record class LoginResponseDTO(string 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
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 |
---|---|---|
|
@@ -35,6 +35,6 @@ | |
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Docker" | ||
} | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
binder-web-backend/Binder.Api/Providers/Interfaces/IAuthProvider.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,7 @@ | ||
namespace Binder.Api.Providers.Interfaces | ||
{ | ||
public interface IAuthProvider | ||
{ | ||
bool Authenticate(string username, string password); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
binder-web-backend/Binder.Api/Providers/JwtDataProvider.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,25 @@ | ||
using SimpleAuthentication.JwtBearer; | ||
using System.Security.Claims; | ||
|
||
namespace Binder.Api.Providers | ||
{ | ||
public sealed class JwtDataProvider | ||
{ | ||
private readonly IJwtBearerService _jwtBearerService; | ||
|
||
public JwtDataProvider(IJwtBearerService bearerService) | ||
{ | ||
_jwtBearerService = bearerService; | ||
} | ||
|
||
public string? GetUserNameFromToken(string token) | ||
{ | ||
if (_jwtBearerService.TryValidateToken(token, true, out ClaimsPrincipal? principal) && principal is not null) | ||
{ | ||
return principal.Identity!.Name!; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
binder-web-backend/Binder.Api/Providers/UsernamePasswordAuthProvider.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,22 @@ | ||
using Binder.Api.Providers.Interfaces; | ||
using Binder.Application.Models.Interfaces; | ||
|
||
namespace Binder.Api.Providers | ||
{ | ||
public sealed class UsernamePasswordAuthProvider : IAuthProvider | ||
{ | ||
private readonly IUsersService _usersService; | ||
|
||
public UsernamePasswordAuthProvider(IUsersService usersService) | ||
{ | ||
_usersService = usersService; | ||
} | ||
|
||
public bool Authenticate(string username, string password) | ||
{ | ||
var passwordFromDb = _usersService.GetUserByName(username).Password; | ||
|
||
return password == passwordFromDb; | ||
} | ||
} | ||
} |
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
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.