| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Security.Claims; |
| | 4 | | using Jellyfin.Api.Constants; |
| | 5 | |
|
| | 6 | | namespace Jellyfin.Api.Extensions; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Extensions for <see cref="ClaimsPrincipal"/>. |
| | 10 | | /// </summary> |
| | 11 | | public static class ClaimsPrincipalExtensions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Get user id from claims. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="user">Current claims principal.</param> |
| | 17 | | /// <returns>User id.</returns> |
| | 18 | | public static Guid GetUserId(this ClaimsPrincipal user) |
| | 19 | | { |
| 218 | 20 | | var value = GetClaimValue(user, InternalClaimTypes.UserId); |
| 218 | 21 | | return string.IsNullOrEmpty(value) |
| 218 | 22 | | ? default |
| 218 | 23 | | : Guid.Parse(value); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Get device id from claims. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="user">Current claims principal.</param> |
| | 30 | | /// <returns>Device id.</returns> |
| | 31 | | public static string? GetDeviceId(this ClaimsPrincipal user) |
| 0 | 32 | | => GetClaimValue(user, InternalClaimTypes.DeviceId); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Get device from claims. |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="user">Current claims principal.</param> |
| | 38 | | /// <returns>Device.</returns> |
| | 39 | | public static string? GetDevice(this ClaimsPrincipal user) |
| 0 | 40 | | => GetClaimValue(user, InternalClaimTypes.Device); |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Get client from claims. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="user">Current claims principal.</param> |
| | 46 | | /// <returns>Client.</returns> |
| | 47 | | public static string? GetClient(this ClaimsPrincipal user) |
| 12 | 48 | | => GetClaimValue(user, InternalClaimTypes.Client); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Get version from claims. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="user">Current claims principal.</param> |
| | 54 | | /// <returns>Version.</returns> |
| | 55 | | public static string? GetVersion(this ClaimsPrincipal user) |
| 0 | 56 | | => GetClaimValue(user, InternalClaimTypes.Version); |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Get token from claims. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="user">Current claims principal.</param> |
| | 62 | | /// <returns>Token.</returns> |
| | 63 | | public static string? GetToken(this ClaimsPrincipal user) |
| 2 | 64 | | => GetClaimValue(user, InternalClaimTypes.Token); |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Gets a flag specifying whether the request is using an api key. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="user">Current claims principal.</param> |
| | 70 | | /// <returns>The flag specifying whether the request is using an api key.</returns> |
| | 71 | | public static bool GetIsApiKey(this ClaimsPrincipal user) |
| | 72 | | { |
| 153 | 73 | | var claimValue = GetClaimValue(user, InternalClaimTypes.IsApiKey); |
| 153 | 74 | | return bool.TryParse(claimValue, out var parsedClaimValue) |
| 153 | 75 | | && parsedClaimValue; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | private static string? GetClaimValue(in ClaimsPrincipal user, string name) |
| 385 | 79 | | => user.Claims.FirstOrDefault(claim => claim.Type.Equals(name, StringComparison.OrdinalIgnoreCase))?.Value; |
| | 80 | | } |