checkpoint

This commit is contained in:
2021-11-10 18:46:29 -06:00
parent 2a3b7b32b4
commit 20f44c8b90
26 changed files with 519 additions and 407 deletions

View File

@@ -1,57 +1,79 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Gameboard.ShogiUI.Sockets.Repositories.CouchModels;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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)
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"
});
public static User CreateMsalUser(string id) => new(id, id, WhichLoginPlatform.Microsoft);
public static User CreateGuestUser(string id)
{
Name = name;
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);
}
/// <summary>
/// Constructor for guest user.
/// </summary>
public User(string name, Guid webSessionId)
public string Id { get; }
public string DisplayName { get; }
public WhichLoginPlatform LoginPlatform { get; }
public bool IsGuest => LoginPlatform == WhichLoginPlatform.Guest;
public User(string id, string displayName, WhichLoginPlatform platform)
{
Name = name;
WebSessionId = webSessionId;
Id = id;
DisplayName = displayName;
LoginPlatform = platform;
}
public ClaimsIdentity CreateMsalUserIdentity()
public User(UserDocument document)
{
var claims = new List<Claim>()
Id = document.Id;
DisplayName = document.DisplayName;
LoginPlatform = document.Platform;
}
public ClaimsIdentity CreateClaimsIdentity()
{
if (LoginPlatform == WhichLoginPlatform.Guest)
{
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>(4)
{
new Claim(ClaimTypes.NameIdentifier, Id),
new Claim(ClaimTypes.Name, DisplayName),
new Claim(ClaimTypes.Role, "Guest"),
new Claim(ClaimTypes.Role, "Shogi") // The Shogi role grants access to api controllers.
};
return new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
}
var claims = new List<Claim>()
else
{
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);
var claims = new List<Claim>(3)
{
new Claim(ClaimTypes.NameIdentifier, Id),
new Claim(ClaimTypes.Name, DisplayName),
new Claim(ClaimTypes.Role, "Shogi") // The Shogi role grants access to api controllers.
};
return new ClaimsIdentity(claims, JwtBearerDefaults.AuthenticationScheme);
}
}
}
}