Add WmsLite monorepo with JWT auth, tenant context, and project READMEs.
Implement API security baseline (sys_user, JWT, audit fields, transactions, unique constraints) and Web real login with 401 handling. Document setup and development workflows in root, WmsLite.Api, and WmsLite.Web README files. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
fa1b286463
241 files changed
+31188
No files matched your search
@@ -0,0 +1,56 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace WmsLite.Api.Infrastructure.Auth;
|
||||
|
||||
public sealed class HttpCurrentUserContext : ICurrentUserContext
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public HttpCurrentUserContext(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
private ClaimsPrincipal? User => _httpContextAccessor.HttpContext?.User;
|
||||
|
||||
public bool IsAuthenticated => User?.Identity?.IsAuthenticated == true;
|
||||
|
||||
public string UserId =>
|
||||
FindClaim(AuthClaimTypes.UserId)
|
||||
?? FindClaim(System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames.Sub)
|
||||
?? RequireClaim(AuthClaimTypes.UserId);
|
||||
|
||||
public string TenantId => RequireClaim(AuthClaimTypes.TenantId);
|
||||
|
||||
public string OwnerCode => RequireClaim(AuthClaimTypes.OwnerCode);
|
||||
|
||||
public string? DisplayName => User?.FindFirst(AuthClaimTypes.DisplayName)?.Value
|
||||
?? User?.FindFirst(ClaimTypes.Name)?.Value;
|
||||
|
||||
private string? FindClaim(string type) =>
|
||||
User?.FindFirst(type)?.Value is { Length: > 0 } value ? value.Trim() : null;
|
||||
|
||||
private string RequireClaim(string type)
|
||||
{
|
||||
if (!IsAuthenticated)
|
||||
{
|
||||
throw new UnauthorizedAccessException("User is not authenticated.");
|
||||
}
|
||||
|
||||
var value = User!.FindFirst(type)?.Value;
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
throw new UnauthorizedAccessException($"Missing claim: {type}.");
|
||||
}
|
||||
|
||||
return value.Trim();
|
||||
}
|
||||
}
|
||||
|
||||
public static class AuthClaimTypes
|
||||
{
|
||||
public const string UserId = "sub";
|
||||
public const string TenantId = "tenant_id";
|
||||
public const string OwnerCode = "owner_code";
|
||||
public const string DisplayName = "name";
|
||||
}
|
||||
Reference in new issue
Block a user