-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentityServerTools.cs
More file actions
90 lines (77 loc) · 3.26 KB
/
IdentityServerTools.cs
File metadata and controls
90 lines (77 loc) · 3.26 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using IdentityServer7.Models;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using IdentityServer7.Extensions;
using System.Security.Claims;
using IdentityServer7.Services;
using IdentityModel;
using System;
using Microsoft.AspNetCore.Authentication;
namespace IdentityServer7
{
/// <summary>
/// Class for useful helpers for interacting with IdentityServer
/// </summary>
public class IdentityServerTools
{
internal readonly IHttpContextAccessor ContextAccessor;
private readonly ITokenCreationService _tokenCreation;
private readonly ISystemClock _clock;
/// <summary>
/// Initializes a new instance of the <see cref="IdentityServerTools" /> class.
/// </summary>
/// <param name="contextAccessor">The context accessor.</param>
/// <param name="tokenCreation">The token creation service.</param>
/// <param name="clock">The clock.</param>
public IdentityServerTools(IHttpContextAccessor contextAccessor, ITokenCreationService tokenCreation, ISystemClock clock)
{
ContextAccessor = contextAccessor;
_tokenCreation = tokenCreation;
_clock = clock;
}
/// <summary>
/// Issues a JWT.
/// </summary>
/// <param name="lifetime">The lifetime.</param>
/// <param name="claims">The claims.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">claims</exception>
public virtual async Task<string> IssueJwtAsync(int lifetime, IEnumerable<Claim> claims)
{
if (claims == null) throw new ArgumentNullException(nameof(claims));
var issuer = ContextAccessor.HttpContext.GetIdentityServerIssuerUri();
var token = new Token
{
CreationTime = _clock.UtcNow.UtcDateTime,
Issuer = issuer,
Lifetime = lifetime,
Claims = new HashSet<Claim>(claims, new ClaimComparer())
};
return await _tokenCreation.CreateTokenAsync(token);
}
/// <summary>
/// Issues a JWT.
/// </summary>
/// <param name="lifetime">The lifetime.</param>
/// <param name="issuer">The issuer.</param>
/// <param name="claims">The claims.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">claims</exception>
public virtual async Task<string> IssueJwtAsync(int lifetime, string issuer, IEnumerable<Claim> claims)
{
if (String.IsNullOrWhiteSpace(issuer)) throw new ArgumentNullException(nameof(issuer));
if (claims == null) throw new ArgumentNullException(nameof(claims));
var token = new Token
{
CreationTime = _clock.UtcNow.UtcDateTime,
Issuer = issuer,
Lifetime = lifetime,
Claims = new HashSet<Claim>(claims, new ClaimComparer())
};
return await _tokenCreation.CreateTokenAsync(token);
}
}
}