Lots of Bugfixes (#2977)

This commit is contained in:
Joe Milazzo 2024-06-04 17:43:15 -05:00 committed by GitHub
parent 8c629695ef
commit 616ed7a75d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 427 additions and 244 deletions

View file

@ -7,17 +7,23 @@ namespace API.Extensions;
public static class ClaimsPrincipalExtensions
{
private const string NotAuthenticatedMessage = "User is not authenticated";
/// <summary>
/// Get's the authenticated user's username
/// </summary>
/// <remarks>Warning! Username's can contain .. and /, do not use folders or filenames explicitly with the Username</remarks>
/// <param name="user"></param>
/// <returns></returns>
/// <exception cref="KavitaException"></exception>
public static string GetUsername(this ClaimsPrincipal user)
{
var userClaim = user.FindFirst(JwtRegisteredClaimNames.Name);
if (userClaim == null) throw new KavitaException("User is not authenticated");
var userClaim = user.FindFirst(JwtRegisteredClaimNames.Name) ?? throw new KavitaException(NotAuthenticatedMessage);
return userClaim.Value;
}
public static int GetUserId(this ClaimsPrincipal user)
{
var userClaim = user.FindFirst(ClaimTypes.NameIdentifier);
if (userClaim == null) throw new KavitaException("User is not authenticated");
var userClaim = user.FindFirst(ClaimTypes.NameIdentifier) ?? throw new KavitaException(NotAuthenticatedMessage);
return int.Parse(userClaim.Value);
}
}