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;
|
|
|
|
|
using Yavsc.Models;
|
|
|
|
|
|
|
|
|
|
namespace Yavsc.Services
|
|
|
|
|
{
|
|
|
|
|
public class ProfileService : IProfileService
|
|
|
|
|
{
|
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
|
public ProfileService(
|
2025-02-09 16:57:10 +00:00
|
|
|
UserManager<ApplicationUser> userManager)
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-09 16:57:10 +00:00
|
|
|
public async Task<List<Claim>> GetClaimsFromUserAsync(
|
|
|
|
|
ProfileDataRequestContext context,
|
|
|
|
|
ApplicationUser user)
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
2025-02-09 16:57:10 +00:00
|
|
|
|
|
|
|
|
var allowedScopes = context.Client.AllowedScopes
|
|
|
|
|
.Where(s => s != JwtClaimTypes.Subject)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (allowedScopes.Contains("profile"))
|
|
|
|
|
{
|
|
|
|
|
allowedScopes.Remove("profile");
|
|
|
|
|
allowedScopes.Add(JwtClaimTypes.Name);
|
|
|
|
|
allowedScopes.Add(JwtClaimTypes.FamilyName);
|
|
|
|
|
allowedScopes.Add(JwtClaimTypes.Email);
|
|
|
|
|
allowedScopes.Add(JwtClaimTypes.PreferredUserName);
|
|
|
|
|
allowedScopes.Add("http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-09 12:04:14 +00:00
|
|
|
var claims = new List<Claim> {
|
|
|
|
|
new Claim(JwtClaimTypes.Subject,user.Id.ToString()),
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-09 16:57:10 +00:00
|
|
|
foreach (var subClaim in context.Subject.Claims)
|
2025-02-09 12:04:14 +00:00
|
|
|
{
|
2025-02-09 16:57:10 +00:00
|
|
|
if (allowedScopes.Contains(subClaim.Type))
|
|
|
|
|
claims.Add(subClaim);
|
|
|
|
|
}
|
2025-02-09 12:04:14 +00:00
|
|
|
|
2025-02-09 16:57:10 +00:00
|
|
|
AddClaims(allowedScopes, claims, JwtClaimTypes.Email, user.Email);
|
|
|
|
|
AddClaims(allowedScopes, claims, JwtClaimTypes.PreferredUserName, user.FullName);
|
|
|
|
|
|
|
|
|
|
foreach (var scope in context.Client.AllowedScopes)
|
|
|
|
|
{
|
|
|
|
|
claims.Add(new Claim("scope", scope));
|
|
|
|
|
}
|
2025-02-09 12:04:14 +00:00
|
|
|
|
|
|
|
|
return claims;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-09 16:57:10 +00:00
|
|
|
private static void AddClaims(List<string> allowedScopes, List<Claim> claims,
|
|
|
|
|
string claimType, string claimValue
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
if (allowedScopes.Contains(claimType))
|
|
|
|
|
if (!claims.Any(c => c.Type == claimType))
|
|
|
|
|
claims.Add(new Claim(claimType, claimValue));
|
|
|
|
|
}
|
2025-02-09 12:04:14 +00:00
|
|
|
|
|
|
|
|
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
|
|
|
|
|
{
|
|
|
|
|
var subjectId = context.Subject.Claims.FirstOrDefault(c => c.Type == "sub").Value;
|
|
|
|
|
var user = await _userManager.FindByIdAsync(subjectId);
|
2025-02-09 16:57:10 +00:00
|
|
|
context.IssuedClaims = await GetClaimsFromUserAsync(context, user);
|
2025-02-09 12:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task IsActiveAsync(IsActiveContext context)
|
|
|
|
|
{
|
|
|
|
|
var subjectId = context.Subject.Claims.FirstOrDefault(c => c.Type == "sub").Value;
|
|
|
|
|
var user = await _userManager.FindByIdAsync(subjectId);
|
|
|
|
|
context.IsActive = user != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|