-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
9f66427
commit 35f2097
Showing
14 changed files
with
615 additions
and
24 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
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,52 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Application.Backend.Api.Models; | ||
using Application.Backend.Authorization; | ||
using Application.Backend.Core; | ||
using Application.Backend.Database; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Application.Backend.Api; | ||
|
||
[ApiController] | ||
[Route("/api/account")] | ||
public class AccountController(DataContext dataContext, IOptions<AppSettings> appSettings) : Controller | ||
{ | ||
private readonly AppSettings _appSettings = appSettings.Value; | ||
|
||
[HttpPost("login")] | ||
public IActionResult Login(LoginRequest request) | ||
{ | ||
var user = dataContext.Users.FirstOrDefault(user => user.Email == request.Email); | ||
if (user == null) | ||
return NotFound(); | ||
|
||
if (Password.Verify(request.Password, user.PasswordHash, user.PasswordSalt) == false) | ||
return BadRequest(); | ||
|
||
var issuer = _appSettings.Jwt.Issuer; | ||
var audience = _appSettings.Jwt.Audience; | ||
var key = Encoding.ASCII.GetBytes(_appSettings.Jwt.Key); | ||
var tokenDescriptor = new SecurityTokenDescriptor | ||
{ | ||
Subject = new ClaimsIdentity(new[] | ||
{ | ||
new Claim("Id", user.Id.ToString()), | ||
new Claim(JwtRegisteredClaimNames.Email, user.Email), | ||
new Claim(Policy.CanEditForecasts, (user.Role == Role.Administrator).ToString()) | ||
}), | ||
Expires = DateTime.UtcNow.AddDays(1), | ||
Issuer = issuer, | ||
Audience = audience, | ||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature) | ||
}; | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var token = tokenHandler.CreateToken(tokenDescriptor); | ||
var jwtToken = tokenHandler.WriteToken(token); | ||
|
||
return Ok(jwtToken); | ||
} | ||
} |
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 @@ | ||
namespace Application.Backend.Api.Models; | ||
|
||
public record LoginRequest | ||
{ | ||
public required string Email { get; set; } | ||
|
||
public required string Password { get; set; } | ||
} |
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,7 @@ | ||
namespace Application.Backend.Authorization; | ||
|
||
public class Policy | ||
{ | ||
public const string CanEditForecasts = "CanEditForecasts"; | ||
public const string CanDeleteForecasts = "CanDeleteForecasts"; | ||
} |
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 Application.Backend.Authorization; | ||
|
||
public class Role | ||
{ | ||
public const string Administrator = "Administrator"; | ||
public const string User = "User"; | ||
} |
129 changes: 129 additions & 0 deletions
129
2024/React/Backend/Database/Migrations/20240309145632_AddRoleMigration.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
2024/React/Backend/Database/Migrations/20240309145632_AddRoleMigration.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,72 @@ | ||
using System; | ||
using Application.Backend.Authorization; | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace Application.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class AddRoleMigration : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropForeignKey( | ||
name: "FK_Deliveries_Users_UserId", | ||
table: "Deliveries"); | ||
|
||
migrationBuilder.AddColumn<string>( | ||
name: "Role", | ||
table: "Users", | ||
type: "nvarchar(max)", | ||
nullable: false, | ||
defaultValue: Role.Administrator); | ||
|
||
migrationBuilder.AlterColumn<Guid>( | ||
name: "UserId", | ||
table: "Deliveries", | ||
type: "uniqueidentifier", | ||
nullable: true, | ||
oldClrType: typeof(Guid), | ||
oldType: "uniqueidentifier"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_Deliveries_Users_UserId", | ||
table: "Deliveries", | ||
column: "UserId", | ||
principalTable: "Users", | ||
principalColumn: "Id"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropForeignKey( | ||
name: "FK_Deliveries_Users_UserId", | ||
table: "Deliveries"); | ||
|
||
migrationBuilder.DropColumn( | ||
name: "Role", | ||
table: "Users"); | ||
|
||
migrationBuilder.AlterColumn<Guid>( | ||
name: "UserId", | ||
table: "Deliveries", | ||
type: "uniqueidentifier", | ||
nullable: false, | ||
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"), | ||
oldClrType: typeof(Guid), | ||
oldType: "uniqueidentifier", | ||
oldNullable: true); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_Deliveries_Users_UserId", | ||
table: "Deliveries", | ||
column: "UserId", | ||
principalTable: "Users", | ||
principalColumn: "Id", | ||
onDelete: ReferentialAction.Cascade); | ||
} | ||
} | ||
} |
Oops, something went wrong.