-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathAssertionService.cs
More file actions
executable file
·75 lines (61 loc) · 2.46 KB
/
AssertionService.cs
File metadata and controls
executable file
·75 lines (61 loc) · 2.46 KB
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
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Duende.IdentityModel;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
namespace Client;
public class AssertionService(IConfiguration configuration)
{
public string CreateClientToken()
{
var now = DateTimeOffset.UtcNow;
var clientId = configuration.GetValue<string>("ClientId");
// in production, you should load that key from some secure location
var key = configuration.GetValue<string>("Secrets:Key");
var token = new JwtSecurityToken(
clientId,
Urls.IdentityServer,
new List<Claim>()
{
new Claim(JwtClaimTypes.JwtId, Guid.NewGuid().ToString()),
new Claim(JwtClaimTypes.Subject, clientId),
new Claim(JwtClaimTypes.IssuedAt, now.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64)
},
now.UtcDateTime,
now.UtcDateTime.AddMinutes(1),
new SigningCredentials(new JsonWebKey(key), "RS256")
);
token.Header[JwtClaimTypes.TokenType] = "client-authentication+jwt";
var tokenHandler = new JwtSecurityTokenHandler();
tokenHandler.OutboundClaimTypeMap.Clear();
return tokenHandler.WriteToken(token);
}
public string SignAuthorizationRequest(OpenIdConnectMessage message)
{
var now = DateTime.UtcNow;
var clientId = configuration.GetValue<string>("ClientId");
// in production you should load that key from some secure location
var key = configuration.GetValue<string>("Secrets:Key");
var claims = new List<Claim>();
foreach (var parameter in message.Parameters)
{
claims.Add(new Claim(parameter.Key, parameter.Value));
}
var token = new JwtSecurityToken(
clientId,
Urls.IdentityServer,
claims,
now,
now.AddMinutes(1),
new SigningCredentials(new JsonWebKey(key), "RS256")
);
var tokenHandler = new JwtSecurityTokenHandler();
tokenHandler.OutboundClaimTypeMap.Clear();
return tokenHandler.WriteToken(token);
}
}