2026-09-06 16:11:37 +01:00
|
|
|
#nullable enable annotations
|
|
|
|
|
|
2025-02-09 12:04:14 +00:00
|
|
|
using System.Security.Claims;
|
|
|
|
|
using IdentityModel;
|
|
|
|
|
using IdentityServer8.Models;
|
|
|
|
|
using IdentityServer8.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2025-02-15 12:45:27 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-09 12:04:14 +00:00
|
|
|
using Yavsc.Models;
|
|
|
|
|
|
|
|
|
|
namespace Yavsc.Services
|
|
|
|
|
{
|
2025-02-15 20:10:01 +00:00
|
|
|
public class ProfileService : IProfileService
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
|
public ProfileService(
|
2026-08-20 20:50:52 +01:00
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
|
ILogger<DefaultProfileService> logger)
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-15 20:10:01 +00:00
|
|
|
private async Task<List<Claim>> GetClaimsFromUserAsync(
|
2025-02-09 16:57:10 +00:00
|
|
|
ProfileDataRequestContext context,
|
|
|
|
|
ApplicationUser user)
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
2026-08-20 20:50:52 +01:00
|
|
|
|
2025-02-09 12:04:14 +00:00
|
|
|
var claims = new List<Claim> {
|
|
|
|
|
new Claim(JwtClaimTypes.Subject,user.Id.ToString()),
|
|
|
|
|
};
|
2025-02-15 18:40:22 +00:00
|
|
|
|
|
|
|
|
foreach (var scope in context.RequestedResources.ParsedScopes)
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
2025-02-15 20:06:07 +00:00
|
|
|
if (context.Client.AllowedScopes.Contains(scope.ParsedName))
|
|
|
|
|
{
|
|
|
|
|
claims.Add(new Claim(JwtClaimTypes.Scope, scope.ParsedName));
|
|
|
|
|
}
|
2025-02-09 16:57:10 +00:00
|
|
|
}
|
2025-02-09 12:04:14 +00:00
|
|
|
|
2026-09-25 20:42:54 +01:00
|
|
|
// name / email / roles are gated on the client being granted
|
|
|
|
|
// the "profile" identity scope. They MUST land in the *access
|
|
|
|
|
// token*: the Blogs/Api bearer resource hosts read the login
|
|
|
|
|
// via GetUserName() (FindFirstValue("name")) and roles via
|
|
|
|
|
// IsInMsRole() from the access token. But "profile" is an
|
|
|
|
|
// identity scope, which only appears in the id-token caller's
|
|
|
|
|
// ParsedScopes — never in the access-token caller's — so the
|
|
|
|
|
// previous gate (claimAdds.Contains("profile")) left name,
|
|
|
|
|
// email and roles out of every access token, and GetUserName()
|
|
|
|
|
// resolved to null on every bearer host (the MyFiles upload
|
|
|
|
|
// 500). Gate on the client's AllowedScopes instead, which is
|
|
|
|
|
// caller-independent, so both the access token and the id
|
|
|
|
|
// token carry these claims for any user grant whose client is
|
|
|
|
|
// allowed "profile".
|
|
|
|
|
if (context.Client.AllowedScopes.Contains(JwtClaimTypes.Profile))
|
2025-02-09 16:57:10 +00:00
|
|
|
{
|
2026-09-25 20:42:54 +01:00
|
|
|
// "name" carries the login (UserName), not the display
|
|
|
|
|
// FullName: GetUserName() / User.Identity.Name feed
|
|
|
|
|
// filesystem paths (user files root, avatar file name),
|
|
|
|
|
// and FullName can contain spaces. No bearer-host
|
|
|
|
|
// consumer displays this claim as a human name today.
|
|
|
|
|
claims.Add(new Claim(JwtClaimTypes.Name, user.UserName));
|
2025-02-11 04:45:05 +00:00
|
|
|
claims.Add(new Claim(JwtClaimTypes.Email, user.Email));
|
|
|
|
|
var roles = await this._userManager.GetRolesAsync(user);
|
2026-09-25 20:42:54 +01:00
|
|
|
if (roles.Count() > 0)
|
2025-02-11 04:45:05 +00:00
|
|
|
{
|
2026-08-20 20:50:52 +01:00
|
|
|
claims.AddRange(roles.Select(r => new Claim(Constants.RoleClaimType, r)));
|
2025-02-11 04:45:05 +00:00
|
|
|
}
|
2025-02-09 16:57:10 +00:00
|
|
|
}
|
2025-02-09 12:04:14 +00:00
|
|
|
return claims;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 07:49:18 +01:00
|
|
|
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
2025-02-15 18:40:22 +00:00
|
|
|
var subjectId = GetSubjectId(context.Subject);
|
2025-07-07 07:49:18 +01:00
|
|
|
if (subjectId == null) return;
|
2025-02-09 12:04:14 +00:00
|
|
|
var user = await _userManager.FindByIdAsync(subjectId);
|
2025-07-07 07:49:18 +01:00
|
|
|
if (user == null) return;
|
2025-02-09 16:57:10 +00:00
|
|
|
context.IssuedClaims = await GetClaimsFromUserAsync(context, user);
|
2025-02-09 12:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-15 20:10:01 +00:00
|
|
|
public async Task IsActiveAsync(IsActiveContext context)
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
2025-02-15 18:40:22 +00:00
|
|
|
string? subjectId = GetSubjectId(context.Subject);
|
|
|
|
|
if (subjectId == null)
|
|
|
|
|
{
|
|
|
|
|
context.IsActive = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-02-09 12:04:14 +00:00
|
|
|
var user = await _userManager.FindByIdAsync(subjectId);
|
|
|
|
|
context.IsActive = user != null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-15 18:40:22 +00:00
|
|
|
private static string? GetSubjectId(ClaimsPrincipal claimsPrincipal)
|
|
|
|
|
{
|
|
|
|
|
return claimsPrincipal.Claims.FirstOrDefault(c => c.Type == "sub")?.Value;
|
|
|
|
|
}
|
2025-02-09 12:04:14 +00:00
|
|
|
}
|
|
|
|
|
}
|