Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PasswordKeeper.BusinessLogic/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PasswordKeeper.BusinessLogic;
public class Users(PasswordKeeper.DataAccess.Users users)
{
/// <inheritdoc cref="PasswordKeeper.DataAccess.Users.UpsertUser"/>
public async Task<bool> UpsertUser(UserDto userDto)
public async Task<UserDto?> UpsertUser(UserDto userDto)
{
return await users.UpsertUser(userDto);
}
Expand Down
6 changes: 3 additions & 3 deletions PasswordKeeper.DataAccess/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class Users(IDbContextFactory<Entities> dbContextFactory, IMapper mapper)
/// Upserts a user. If the user doesn't exist, inserts it, otherwise updates it.
/// </summary>
/// <param name="userDto">The user to upsert.</param>
/// <returns><c>true</c> if the user was upserted successfully, otherwise <c>false</c>.</returns>
public async Task<bool> UpsertUser(UserDto userDto)
/// <returns>The upserted user data or <c>null</c> if the operation failed.</returns>
public async Task<UserDto?> UpsertUser(UserDto userDto)
{
await using var context = await dbContextFactory.CreateDbContextAsync();

Expand All @@ -49,6 +49,6 @@ public async Task<bool> UpsertUser(UserDto userDto)

await context.SaveChangesAsync();

return true;
return mapper.Map<UserDto>(user);
}
}
27 changes: 23 additions & 4 deletions PasswordKeeperServer/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Authentication;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -47,9 +48,9 @@
PasswordSalt = Convert.ToBase64String(salt!),
};

await users.UpsertUser(userDto);
userDto = await users.UpsertUser(userDto);

var token = GenerateJwtToken(user.Username);
var token = GenerateJwtToken(user.Username, userDto.Id);

Check warning on line 53 in PasswordKeeperServer/Controllers/AuthenticationController.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 53 in PasswordKeeperServer/Controllers/AuthenticationController.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
return Ok(new { token, });
}

Expand All @@ -59,7 +60,7 @@
if (Users.VerifyPassword(user.Password, userDto.PasswordHash,
Convert.FromBase64String(userDto.PasswordSalt)))
{
var token = GenerateJwtToken(user.Username);
var token = GenerateJwtToken(user.Username, userDto.Id);
return Ok(new { token, });
}
}
Expand All @@ -77,17 +78,35 @@
return Ok();
}

/// <summary>
/// Gets the logged-in user's ID.
/// </summary>
/// <returns>The logged-in user's ID, or -1 if the claim containing the user ID is not found.</returns>
long GetLoggedUserId()
{
var claim = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier && long.TryParse(c.Value, out _));

if (long.TryParse(claim?.Value, out var result))
{
return result;
}

return -1;
}

/// <summary>
/// Generates a JWT token for the given username.
/// </summary>
/// <param name="username">The username to generate the JWT token for.</param>
/// <param name="userId">The user ID to generate the JWT token for.</param>
/// <returns>The JWT token.</returns>
private string GenerateJwtToken(string username)
private string GenerateJwtToken(string username, long userId)
{
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, username),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.NameId, userId.ToString()),
};

var key = new SymmetricSecurityKey(Program.JwtKey);
Expand Down
7 changes: 6 additions & 1 deletion PasswordKeeperServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi;
using MySql.Data.MySqlClient;
using PasswordKeeper.BusinessLogic;
using PasswordKeeper.DAO;
Expand Down Expand Up @@ -70,7 +71,11 @@ public static void Main(string[] args)
builder.Services.AddAutoMapper(typeof(AutoMapperProfile));

// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddOpenApi("v3",options =>
{
options.ShouldInclude = operation => operation.HttpMethod != null;
options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_0;
});

var app = builder.Build();

Expand Down