Skip to content

Commit 67bdb16

Browse files
committed
Generate (and cache) a token to use as the default value
1 parent 4c171c1 commit 67bdb16

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

src/Pages/Home/JwtDecoder/JwtDecoder.cshtml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<div class="h-100 d-flex flex-column">
4444
<div class="form-group flex-grow-1">
4545
<label for="jwt-input" class="sr-only">Paste your JWT here...</label>
46-
<div id="jwt-input" class="form-control bg-dark text-light p-2 h-100 jwt-input-editable" contenteditable="true" rows="8" style="min-height: 10em;" placeholder="Paste your JWT here..."></div>
46+
<div id="jwt-input" class="form-control bg-dark text-light p-2 h-100 jwt-input-editable" contenteditable="true" rows="8" style="min-height: 10em;" placeholder="Paste your JWT here...">@Model.View?.Token</div>
4747
</div>
4848
<div class="form-group">
4949
<label for="jwks-url">Issuer, Discovery Document or JWKs URI</label>
@@ -327,7 +327,8 @@
327327
}
328328
});
329329
330-
$('#jwt-input').on('input', async function() {
330+
const jwtInput = $('#jwt-input');
331+
jwtInput.on('input', async function() {
331332
decodedJwt = {
332333
header: null,
333334
payload: null,
@@ -361,7 +362,14 @@
361362
}
362363
});
363364
364-
await showDecodedJwt(null, null, null, ' ');
365+
@if (Model.View?.Token != null)
366+
{
367+
@:jwtInput.trigger('input'); // Trigger input event to decode the JWT if it was provided
368+
}
369+
else
370+
{
371+
@:await showDecodedJwt(null, null, null, ' ');
372+
}
365373
}
366374
367375
function colorJwtInput(target, parts) {
Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,62 @@
1-
using Microsoft.AspNetCore.Authorization;
1+
using IdentityModel.Client;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
24
using Microsoft.AspNetCore.Mvc.RazorPages;
5+
using Microsoft.Extensions.Caching.Memory;
36

47
namespace IdentityServerHost.Pages.Home;
58

69
[AllowAnonymous]
7-
public class JwtDecoder : PageModel
10+
public class JwtDecoder(IHttpClientFactory clientFactory, IMemoryCache cache) : PageModel
811
{
12+
private const string CacheKey = "jwt_decoder::access_token";
13+
14+
public ViewModel View { get; set; } = default!;
15+
16+
public async Task<IActionResult> OnGet()
17+
{
18+
var token = await GetOrCreateToken();
19+
View = new ViewModel { Token = token };
20+
21+
return Page();
22+
}
23+
24+
private async Task<string> GetOrCreateToken()
25+
{
26+
try
27+
{
28+
if (cache.TryGetValue(CacheKey, out string cachedToken))
29+
{
30+
return cachedToken;
31+
}
32+
33+
var request = HttpContext.Request;
34+
var authority = request.Scheme + "://" + request.Host.ToUriComponent();
35+
36+
var client = clientFactory.CreateClient();
37+
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
38+
{
39+
Address = $"{authority}/connect/token",
40+
41+
ClientId = "m2m",
42+
ClientSecret = "secret",
43+
44+
Scope = "api"
45+
});
46+
47+
if (!response.IsError)
48+
{
49+
var token = response.AccessToken;
50+
cache.Set(CacheKey, token, TimeSpan.FromMinutes(20));
51+
52+
return token;
53+
}
54+
}
55+
catch
56+
{
57+
return null;
58+
}
59+
60+
return null;
61+
}
962
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace IdentityServerHost.Pages.Home;
2+
3+
public class ViewModel
4+
{
5+
public string Token { get; set; }
6+
}

0 commit comments

Comments
 (0)