yep
This commit is contained in:
@@ -176,7 +176,7 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<GetSessionsResponse> GetSessions()
|
||||
public async Task<ActionResult<GetSessionsResponse>> GetSessions()
|
||||
{
|
||||
var user = await ReadUserOrThrow();
|
||||
var sessions = await gameboardRepository.ReadSessionMetadatas();
|
||||
@@ -190,11 +190,11 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
|
||||
.Select(s => mapper.Map(s))
|
||||
.ToList();
|
||||
|
||||
return new GetSessionsResponse
|
||||
return Ok(new GetSessionsResponse
|
||||
{
|
||||
PlayerHasJoinedSessions = sessionsJoinedByUser,
|
||||
AllOtherSessions = sessionsNotJoinedByUser
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut("{gameName}")]
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Extensions
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static string? UserId(this ClaimsPrincipal self)
|
||||
{
|
||||
return self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||
}
|
||||
public static class Extensions
|
||||
{
|
||||
private static readonly string MsalUsernameClaim = "preferred_username";
|
||||
|
||||
public static string? DisplayName(this ClaimsPrincipal self)
|
||||
{
|
||||
return self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
|
||||
}
|
||||
public static string? UserId(this ClaimsPrincipal self)
|
||||
{
|
||||
return self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||
}
|
||||
|
||||
public static bool IsGuest(this ClaimsPrincipal self)
|
||||
{
|
||||
return self.HasClaim(c => c.Type == ClaimTypes.Role && c.Value == "Guest");
|
||||
}
|
||||
public static string? DisplayName(this ClaimsPrincipal self)
|
||||
{
|
||||
return self.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
|
||||
}
|
||||
|
||||
public static string ToCamelCase(this string self)
|
||||
{
|
||||
return char.ToLowerInvariant(self[0]) + self[1..];
|
||||
}
|
||||
}
|
||||
public static bool IsMicrosoft(this ClaimsPrincipal self)
|
||||
{
|
||||
return self.HasClaim(c => c.Type == MsalUsernameClaim);
|
||||
}
|
||||
|
||||
public static string ChangeFirstCharacterToLowerCase(this string self)
|
||||
{
|
||||
return char.ToLowerInvariant(self[0]) + self[1..];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,9 +31,9 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
throw new InvalidOperationException("Cannot create user from given claims.");
|
||||
}
|
||||
|
||||
var user = principal.IsGuest()
|
||||
? User.CreateGuestUser(id)
|
||||
: User.CreateMsalUser(id);
|
||||
var user = principal.IsMicrosoft()
|
||||
? User.CreateMsalUser(id)
|
||||
: User.CreateGuestUser(id);
|
||||
|
||||
await repository.CreateUser(user);
|
||||
return user;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
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;
|
||||
|
||||
@@ -62,7 +60,6 @@ namespace Gameboard.ShogiUI.Sockets.Models
|
||||
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);
|
||||
}
|
||||
@@ -72,7 +69,6 @@ namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ namespace Gameboard.ShogiUI.Sockets
|
||||
services.AddSingleton<IValidator<JoinGameRequest>, JoinGameRequestValidator>();
|
||||
services.AddSingleton<ISocketService, SocketService>();
|
||||
services.AddTransient<IGameboardRepository, GameboardRepository>();
|
||||
services.AddSingleton<IClaimsTransformation, ShogiUserClaimsTransformer>();
|
||||
services.AddTransient<IClaimsTransformation, ShogiUserClaimsTransformer>();
|
||||
services.AddHttpClient("couchdb", c =>
|
||||
{
|
||||
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin:admin"));
|
||||
|
||||
@@ -1,43 +1,44 @@
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets
|
||||
{
|
||||
/// <summary>
|
||||
/// Standardizes the claims from third party issuers. Also registers new msal users in the database.
|
||||
/// </summary>
|
||||
public class ShogiUserClaimsTransformer : IClaimsTransformation
|
||||
{
|
||||
private static readonly string MsalUsernameClaim = "preferred_username";
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
/// <summary>
|
||||
/// Standardizes the claims from third party issuers. Also registers new msal users in the database.
|
||||
/// </summary>
|
||||
public class ShogiUserClaimsTransformer : IClaimsTransformation
|
||||
{
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
|
||||
public ShogiUserClaimsTransformer(IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
}
|
||||
public ShogiUserClaimsTransformer(IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
}
|
||||
|
||||
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
|
||||
{
|
||||
var nameClaim = principal.Claims.FirstOrDefault(c => c.Type == MsalUsernameClaim);
|
||||
if (nameClaim != default)
|
||||
{
|
||||
var user = await gameboardRepository.ReadUser(nameClaim.Value);
|
||||
if (user == null)
|
||||
{
|
||||
var newUser = Models.User.CreateMsalUser(nameClaim.Value);
|
||||
await gameboardRepository.CreateUser(newUser);
|
||||
user = newUser;
|
||||
}
|
||||
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
|
||||
{
|
||||
var id = principal.UserId();
|
||||
if (!string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
var user = await gameboardRepository.ReadUser(id);
|
||||
if (user == null)
|
||||
{
|
||||
var newUser = principal.IsMicrosoft()
|
||||
? Models.User.CreateMsalUser(id)
|
||||
: Models.User.CreateGuestUser(id);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
return new ClaimsPrincipal(user.CreateClaimsIdentity());
|
||||
}
|
||||
}
|
||||
return principal;
|
||||
}
|
||||
}
|
||||
await gameboardRepository.CreateUser(newUser);
|
||||
user = newUser;
|
||||
}
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
return new ClaimsPrincipal(user.CreateClaimsIdentity());
|
||||
}
|
||||
}
|
||||
return principal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user