-
Notifications
You must be signed in to change notification settings - Fork 317
/
TokenExtensions.cs
157 lines (136 loc) · 5.28 KB
/
TokenExtensions.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.
using IdentityModel;
using Duende.IdentityServer.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using Duende.IdentityServer.Configuration;
using Microsoft.AspNetCore.Routing;
namespace Duende.IdentityServer.Extensions
{
/// <summary>
/// Extensions for Token
/// </summary>
public static class TokenExtensions
{
/// <summary>
/// Creates the default JWT payload dictionary
/// </summary>
/// <param name="token"></param>
/// <param name="options"></param>
/// <param name="clock"></param>
/// <param name="logger"></param>
/// <returns></returns>
public static Dictionary<string, object> CreateJwtPayloadDictionary(this Token token,
IdentityServerOptions options, ISystemClock clock, ILogger logger)
{
try
{
var payload = new Dictionary<string, object>();
// set issuer
payload.Add(JwtClaimTypes.Issuer, token.Issuer);
// set times (nbf, exp, iat)
var now = clock.UtcNow.ToUnixTimeSeconds();
var exp = now + token.Lifetime;
payload.Add(JwtClaimTypes.NotBefore, now);
payload.Add(JwtClaimTypes.IssuedAt, now);
payload.Add(JwtClaimTypes.Expiration, exp);
// add audience claim(s)
if (token.Audiences.Any())
{
if (token.Audiences.Count == 1)
{
payload.Add(JwtClaimTypes.Audience, token.Audiences.First());
}
else
{
payload.Add(JwtClaimTypes.Audience, token.Audiences);
}
}
// add confirmation claim (if present)
if (token.Confirmation.IsPresent())
{
payload.Add(JwtClaimTypes.Confirmation,
JsonSerializer.Deserialize<JsonElement>(token.Confirmation));
}
// scope claims
var scopeClaims = token.Claims.Where(x => x.Type == JwtClaimTypes.Scope).ToArray();
if (!scopeClaims.IsNullOrEmpty())
{
var scopeValues = scopeClaims.Select(x => x.Value).ToArray();
if (options.EmitScopesAsSpaceDelimitedStringInJwt)
{
payload.Add(JwtClaimTypes.Scope, string.Join(" ", scopeValues));
}
else
{
payload.Add(JwtClaimTypes.Scope, scopeValues);
}
}
// amr claims
var amrClaims = token.Claims.Where(x => x.Type == JwtClaimTypes.AuthenticationMethod).ToArray();
if (!amrClaims.IsNullOrEmpty())
{
var amrValues = amrClaims.Select(x => x.Value).Distinct().ToArray();
payload.Add(JwtClaimTypes.AuthenticationMethod, amrValues);
}
var simpleClaimTypes = token.Claims.Where(c =>
c.Type != JwtClaimTypes.AuthenticationMethod && c.Type != JwtClaimTypes.Scope)
.Select(c => c.Type)
.Distinct();
// other claims
foreach (var claimType in simpleClaimTypes)
{
var claims = token.Claims.Where(c => c.Type == claimType).ToArray();
if (claims.Count() > 1)
{
payload.Add(claimType, AddObjects(claims));
}
else
{
payload.Add(claimType, AddObject(claims.First()));
}
}
return payload;
}
catch (Exception ex)
{
logger.LogCritical(ex, "Error creating the JWT payload");
throw;
}
}
private static IEnumerable<object> AddObjects(IEnumerable<Claim> claims)
{
foreach (var claim in claims)
{
yield return AddObject(claim);
}
}
private static object AddObject(Claim claim)
{
if (claim.Type == ClaimValueTypes.Boolean)
{
return Boolean.Parse(claim.Value);
}
if (claim.Type == ClaimValueTypes.Integer || claim.Type == ClaimValueTypes.Integer32)
{
return Int32.Parse(claim.Value);
}
if (claim.Type == ClaimValueTypes.Integer64)
{
return Int64.Parse(claim.Value);
}
if (claim.Type == IdentityServerConstants.ClaimValueTypes.Json)
{
return JsonSerializer.Deserialize<JsonElement>(claim.Value);
}
return claim.Value;
}
}
}