84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using Gameboard.ShogiUI.Sockets.Repositories.CouchModels;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using System.Collections.ObjectModel;
|
|
using System.Security.Claims;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Models
|
|
{
|
|
public class User
|
|
{
|
|
public static readonly ReadOnlyCollection<string> Adjectives = new(new[] {
|
|
"Fortuitous", "Retractable", "Happy", "Habbitable", "Creative", "Fluffy", "Impervious", "Kingly"
|
|
});
|
|
public static readonly ReadOnlyCollection<string> Subjects = new(new[] {
|
|
"Hippo", "Basil", "Mouse", "Walnut", "Prince", "Lima Bean", "Coala", "Potato", "Penguin"
|
|
});
|
|
public static User CreateMsalUser(string id) => new(id, id, WhichLoginPlatform.Microsoft);
|
|
public static User CreateGuestUser(string id)
|
|
{
|
|
var random = new Random();
|
|
// Adjective
|
|
var index = (int)Math.Floor(random.NextDouble() * Adjectives.Count);
|
|
var adj = Adjectives[index];
|
|
// Subject
|
|
index = (int)Math.Floor(random.NextDouble() * Subjects.Count);
|
|
var subj = Subjects[index];
|
|
|
|
return new User(id, $"{adj} {subj}", WhichLoginPlatform.Guest);
|
|
}
|
|
|
|
public string Id { get; }
|
|
public string DisplayName { get; }
|
|
|
|
public WhichLoginPlatform LoginPlatform { get; }
|
|
|
|
public bool IsGuest => LoginPlatform == WhichLoginPlatform.Guest;
|
|
|
|
public bool IsAdmin => LoginPlatform == WhichLoginPlatform.Microsoft && Id == "Hauth@live.com";
|
|
|
|
public User(string id, string displayName, WhichLoginPlatform platform)
|
|
{
|
|
Id = id;
|
|
DisplayName = displayName;
|
|
LoginPlatform = platform;
|
|
}
|
|
|
|
public User(UserDocument document)
|
|
{
|
|
Id = document.Id;
|
|
DisplayName = document.DisplayName;
|
|
LoginPlatform = document.Platform;
|
|
}
|
|
|
|
public ClaimsIdentity CreateClaimsIdentity()
|
|
{
|
|
if (LoginPlatform == WhichLoginPlatform.Guest)
|
|
{
|
|
var claims = new List<Claim>(4)
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, Id),
|
|
new Claim(ClaimTypes.Name, DisplayName),
|
|
new Claim(ClaimTypes.Role, "Guest"),
|
|
};
|
|
return new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
|
}
|
|
else
|
|
{
|
|
var claims = new List<Claim>(3)
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, Id),
|
|
new Claim(ClaimTypes.Name, DisplayName),
|
|
};
|
|
return new ClaimsIdentity(claims, JwtBearerDefaults.AuthenticationScheme);
|
|
}
|
|
}
|
|
|
|
public ServiceModels.Types.User ToServiceModel() => new()
|
|
{
|
|
Id = Id,
|
|
Name = DisplayName
|
|
};
|
|
}
|
|
}
|