58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Security.Claims;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Models
|
|
{
|
|
public class User
|
|
{
|
|
public string Name { get; }
|
|
public Guid? WebSessionId { get; }
|
|
|
|
public bool IsGuest => WebSessionId.HasValue;
|
|
|
|
public User(string name)
|
|
{
|
|
Name = name;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor for guest user.
|
|
/// </summary>
|
|
public User(string name, Guid webSessionId)
|
|
{
|
|
Name = name;
|
|
WebSessionId = webSessionId;
|
|
}
|
|
|
|
public ClaimsIdentity CreateMsalUserIdentity()
|
|
{
|
|
var claims = new List<Claim>()
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, Name),
|
|
new Claim(ClaimTypes.Role, "Shogi") // The Shogi role grants access to api controllers.
|
|
};
|
|
return new ClaimsIdentity(claims, JwtBearerDefaults.AuthenticationScheme);
|
|
}
|
|
|
|
public ClaimsIdentity CreateGuestUserIdentity()
|
|
{
|
|
// TODO: Make this method static and factory-like.
|
|
if (!WebSessionId.HasValue)
|
|
{
|
|
throw new InvalidOperationException("Cannot create guest identity without a session identifier.");
|
|
}
|
|
|
|
var claims = new List<Claim>()
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, WebSessionId.Value.ToString()),
|
|
new Claim(ClaimTypes.Role, "Guest"),
|
|
new Claim(ClaimTypes.Role, "Shogi") // The Shogi role grants access to api controllers.
|
|
};
|
|
return new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
|
}
|
|
}
|
|
}
|