Fix claims.

Use OID instead of email for microsoft identifier.
Fix PlayerCount route.
Add created date to user table.
Create spectator icon.
This commit is contained in:
2023-01-20 20:48:38 -06:00
parent 1d0beaf69f
commit 26fd955aa4
35 changed files with 672 additions and 1426 deletions

View File

@@ -1,42 +1,30 @@
using System.Security.Claims;
using Microsoft.Identity.Web;
using System.Security.Claims;
namespace Shogi.Api.Extensions;
public static class ClaimsExtensions
{
private static readonly string MsalUsernameClaim = "preferred_username";
// https://learn.microsoft.com/en-us/azure/active-directory/develop/id-tokens#payload-claims
public static string? GetGuestUserId(this ClaimsPrincipal self)
{
return self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
}
/// <summary>
/// Get Id from claims after applying shogi-specific claims transformations.
/// </summary>
public static string GetShogiUserId(this ClaimsPrincipal self)
{
var id = self.GetNameIdentifierId();
if (string.IsNullOrEmpty(id)) throw new InvalidOperationException("Shogi UserId not found in claims.");
return id;
}
public static string? DisplayName(this ClaimsPrincipal self)
{
return self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
}
/// <summary>
/// Get display name from claims after applying shogi-specific claims transformations.
/// </summary>
public static string GetShogiUserDisplayname(this ClaimsPrincipal self)
{
var displayName = self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
if (string.IsNullOrEmpty(displayName)) throw new InvalidOperationException("Shogi Display name not found in claims.");
return displayName;
}
public static bool IsMicrosoft(this ClaimsPrincipal self)
{
return self.HasClaim(c => c.Type == MsalUsernameClaim);
}
public static string? GetMicrosoftUserId(this ClaimsPrincipal self)
{
return self.Claims.FirstOrDefault(c => c.Type == MsalUsernameClaim)?.Value;
}
/// <summary>
/// Reads the userId from claims after claims transformation has occurred.
/// Throws if a shogi userid is not found.
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
public static string GetShogiUserId(this ClaimsPrincipal self)
{
var id = self.IsMicrosoft() ? self.GetMicrosoftUserId() : self.GetGuestUserId();
if (string.IsNullOrEmpty(id)) throw new InvalidOperationException("Shogi UserId not found in claims.");
return id;
}
}