aboutsummaryrefslogtreecommitdiffstats
path: root/src/server/IdentityServer/ProfileService.cs
blob: 0c14dd5c6078bb3eb16309f4b5f0dd4e20144194 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Dough.Models;
using Dough.Models.Database;
using Dough.Utilities;
using IdentityModel;
using IdentityServer4.Models;
using IdentityServer4.Services;

namespace Dough.IdentityServer
{
    public class ProfileService : IProfileService
    {
        private readonly MainDbContext _context;

        public ProfileService(MainDbContext context)
        {
            _context = context;
        }

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var userId = context.Subject.GetClaimValueOrDefault(JwtClaimTypes.Subject)?.ToGuidOrDefault();
            if (userId == default) return;
            var user = _context.Users.SingleOrDefault((Guid) userId);
            var claims = new List<Claim>
            {
                new Claim(JwtClaimTypes.PreferredUserName, user.Username)
            };
            context.AddRequestedClaims(claims);
        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            var userId = context.Subject.GetClaimValueOrDefault(JwtClaimTypes.Subject)?.ToGuidOrDefault();
            if (userId == default) return;
            var user = _context.Users.SingleOrDefault((Guid) userId);
            context.IsActive = !user.Hidden;
        }
    }
}