Lots of Bugfixes (#2977)
This commit is contained in:
parent
8c629695ef
commit
616ed7a75d
26 changed files with 427 additions and 244 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue