yavsc/src/Yavsc.Server/Services/ProfileService.cs

92 lines
3.1 KiB
C#
Raw Normal View History

2025-02-09 12:04:14 +00:00
using System.Security.Claims;
using IdentityModel;
using IdentityServer8.Models;
using IdentityServer8.Services;
2025-02-09 16:57:10 +00:00
using IdentityServer8.Stores;
2025-02-09 12:04:14 +00:00
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(
2025-02-15 20:10:01 +00: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
{
2025-02-15 18:40:22 +00: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
List<string> claimAdds = new List<string>();
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));
claimAdds.Add(scope.ParsedName);
}
2025-02-09 16:57:10 +00:00
}
2025-02-09 12:04:14 +00:00
2025-02-15 18:40:22 +00:00
if (claimAdds.Contains(JwtClaimTypes.Profile))
2025-02-09 16:57:10 +00:00
{
2025-02-15 18:40:22 +00:00
claimAdds.Remove("profile");
claimAdds.Add(JwtClaimTypes.Name);
claimAdds.Add(JwtClaimTypes.Email);
claimAdds.Add(JwtClaimTypes.Role);
2025-02-11 04:45:05 +00:00
}
2025-02-15 18:40:22 +00:00
if (claimAdds.Contains(JwtClaimTypes.Name))
claims.Add(new Claim(JwtClaimTypes.Name, user.FullName));
if (claimAdds.Contains(JwtClaimTypes.Email))
2025-02-11 04:45:05 +00:00
claims.Add(new Claim(JwtClaimTypes.Email, user.Email));
2025-02-15 18:40:22 +00:00
if (claimAdds.Contains(JwtClaimTypes.Role))
2025-02-11 04:45:05 +00:00
{
var roles = await this._userManager.GetRolesAsync(user);
if (roles.Count()>0)
{
2025-07-07 07:49:18 +01:00
claims.AddRange(roles.Select(r => new Claim(JwtClaimTypes.Role, 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
}
}