-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWfaIdentityStrategy.cs
80 lines (67 loc) · 2.53 KB
/
WfaIdentityStrategy.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Security.Claims;
using System.Threading.Tasks;
using CK.Auth;
using CK.DB.Actor;
using CK.DB.AspNet.OIddict.Identity;
using CK.SqlServer;
using OpenIddict.Abstractions;
namespace CK.DB.OIddict.DefaultServer.App
{
public class WfaIdentityStrategy : IIdentityStrategy
{
private readonly IAuthenticationTypeSystem _authenticationTypeSystem;
private readonly UserTable _userTable;
public WfaIdentityStrategy
(
IAuthenticationTypeSystem authenticationTypeSystem,
UserTable userTable
)
{
_authenticationTypeSystem = authenticationTypeSystem;
_userTable = userTable;
}
/// <inheritdoc />
public async Task<AuthenticationInfo?> ValidateAuthAsync( ClaimsPrincipal? authResult )
{
var userName = authResult?.GetClaim( OpenIddictConstants.Claims.Name );
var schemes = authResult?.GetClaim( Constants.Claims.Schemes );
if( userName is null ) return default;
int userId;
using( var sqlCallContext = new SqlStandardCallContext() )
{
userId = await _userTable.FindByNameAsync( sqlCallContext, userName );
}
if( userId <= 0 )
return default;
var info = new Dictionary<string, string> { { Constants.Claims.Schemes, schemes! } };
return new AuthenticationInfo( userName, userId, info );
}
/// <inheritdoc />
public void SetUserClaims
(
ClaimsIdentity identity,
AuthenticationInfo authenticationInfo,
ImmutableArray<string> scopes
)
{
var userName = authenticationInfo.UserName;
var userId = authenticationInfo.UserId;
var schemes = authenticationInfo.Info[Constants.Claims.Schemes];
identity.SetClaim( OpenIddictConstants.Claims.Subject, userId )
.SetClaim( OpenIddictConstants.Claims.Email, userName )
.SetClaim( OpenIddictConstants.Claims.Username, userName )
.SetClaim( OpenIddictConstants.Claims.Name, userName );
if( scopes.Contains( "authinfo" ) )
identity.SetClaim( OpenIddictConstants.Claims.Subject, $"{userId}#{schemes}" );
}
}
public static class Constants
{
public static class Claims
{
public const string Schemes = "schemes";
}
}
}