Skip to content

Commit d940668

Browse files
committed
ASP.NET Member new directions
1 parent 35121b6 commit d940668

File tree

4 files changed

+85
-45
lines changed

4 files changed

+85
-45
lines changed

src/NetDevPack.Security.JwtExtensions/JwkRetriever.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/NetDevPack.Security.JwtExtensions/JwksExtension.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Microsoft.AspNetCore.Authentication.JwtBearer;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Authentication.JwtBearer;
26
using Microsoft.IdentityModel.Tokens;
37

48
namespace NetDevPack.Security.JwtExtensions
@@ -7,8 +11,6 @@ public static class JwksExtension
711
{
812
public static void SetJwksOptions(this JwtBearerOptions options, JwkOptions jwkOptions)
913
{
10-
options.Authority = null;
11-
options.Audience = null;
1214

1315
if (options.TokenValidationParameters == null)
1416
options.TokenValidationParameters = new TokenValidationParameters();
@@ -19,5 +21,37 @@ public static void SetJwksOptions(this JwtBearerOptions options, JwkOptions jwkO
1921
options.TokenValidationParameters.ValidateAudience = false;
2022
options.TokenValidationParameters.ValidIssuer = jwkOptions.Issuer;
2123
}
24+
25+
public class JwkRetriever
26+
{
27+
private static readonly HttpClient HttpClient = new HttpClient();
28+
29+
public JwkRetriever(JwkOptions jwkOptions)
30+
{
31+
Options = jwkOptions;
32+
}
33+
34+
public JwkOptions Options { get; }
35+
public JwkList LastResponse { get; private set; }
36+
public IEnumerable<SecurityKey> IssuerSigningKeyResolver(string token, SecurityToken securityToken, string kid, TokenValidationParameters validationParameters)
37+
{
38+
if (LastResponse == null || LastResponse.When.Add(Options.KeepFor) < DateTime.Now)
39+
{
40+
var jwkTask = GetJwks();
41+
jwkTask.Wait();
42+
LastResponse = new JwkList(jwkTask.Result);
43+
}
44+
45+
return LastResponse.Jwks.Keys;
46+
}
47+
48+
private async Task<JsonWebKeySet> GetJwks()
49+
{
50+
var response = await HttpClient.GetAsync(Options.JwksUri);
51+
var responseString = await response.Content.ReadAsStringAsync();
52+
return new JsonWebKeySet(responseString);
53+
54+
}
55+
}
2256
}
2357
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Microsoft.IdentityModel.Logging;
4+
using Microsoft.IdentityModel.Protocols;
5+
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
6+
using Microsoft.IdentityModel.Tokens;
7+
8+
namespace NetDevPack.Security.JwtExtensions
9+
{
10+
public class JwksRetriever : IConfigurationRetriever<OpenIdConnectConfiguration>
11+
{
12+
public Task<OpenIdConnectConfiguration> GetConfigurationAsync(string address, IDocumentRetriever retriever, CancellationToken cancel)
13+
{
14+
return GetAsync(address, retriever, cancel);
15+
}
16+
17+
/// <summary>
18+
/// Retrieves a populated <see cref="OpenIdConnectConfiguration"/> given an address and an <see cref="IDocumentRetriever"/>.
19+
/// </summary>
20+
/// <param name="address">address of the jwks uri.</param>
21+
/// <param name="retriever">the <see cref="IDocumentRetriever"/> to use to read the jwks</param>
22+
/// <param name="cancel"><see cref="CancellationToken"/>.</param>
23+
/// <returns>A populated <see cref="OpenIdConnectConfiguration"/> instance.</returns>
24+
public static async Task<OpenIdConnectConfiguration> GetAsync(string address, IDocumentRetriever retriever, CancellationToken cancel)
25+
{
26+
if (string.IsNullOrWhiteSpace(address))
27+
throw LogHelper.LogArgumentNullException(nameof(address));
28+
29+
if (retriever == null)
30+
throw LogHelper.LogArgumentNullException(nameof(retriever));
31+
32+
var doc = await retriever.GetDocumentAsync(address, cancel).ConfigureAwait(false);
33+
LogHelper.LogVerbose("IDX21811: Deserializing the string: '{0}' obtained from metadata endpoint into openIdConnectConfiguration object.", doc);
34+
var jwks = new JsonWebKeySet(doc);
35+
var openIdConnectConfiguration = new OpenIdConnectConfiguration()
36+
{
37+
JsonWebKeySet = jwks,
38+
JwksUri = address,
39+
};
40+
foreach (var securityKey in jwks.GetSigningKeys())
41+
openIdConnectConfiguration.SigningKeys.Add(securityKey);
42+
43+
return openIdConnectConfiguration;
44+
}
45+
}
46+
}

src/NetDevPack.Security.JwtExtensions/NetDevPack.Security.JwtExtensions.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<Version>3.1.0</Version>
66
<Authors>Bruno Brito</Authors>
7-
<PackageIconUrl>https://jpproject.blob.core.windows.net/images/helldog.png</PackageIconUrl>
8-
<PackageTags>jwt jwks rsa ecdsa hmac jwks_uri</PackageTags>
7+
<PackageIconUrl>https://raw.githubusercontent.com/NetDevPack/NetDevPack/master/assets/IconNuget.png</PackageIconUrl>
8+
<PackageTags>jwt jwks jwks_uri</PackageTags>
99
<Title>Extension to load JWKS from custom uri</Title>
1010
<Description>Component for easy use of JWKS endpoint for Assymetric keys</Description>
1111
<NeutralLanguage>en</NeutralLanguage>

0 commit comments

Comments
 (0)