massive checkpoint
This commit is contained in:
@@ -1,18 +1,57 @@
|
||||
using System;
|
||||
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 static readonly string GuestPrefix = "Guest-";
|
||||
public string Name { get; }
|
||||
public Guid? WebSessionId { get; }
|
||||
public bool IsGuest => Name.StartsWith(GuestPrefix) && WebSessionId.HasValue;
|
||||
|
||||
public User(string name, Guid? webSessionId = null)
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user