Skip to content

Commit cf51119

Browse files
authored
Merge pull request #107 from Atralupus/jwt-headless
Request with jwt token
2 parents 9db6af2 + d081a22 commit cf51119

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,7 @@ fabric.properties
9191

9292
# End of https://www.toptal.com/developers/gitignore/api/rider,dotnetcore
9393

94-
ArenaService/appsettings.Local.json
94+
**/appsettings.Local.json
95+
**/appsettings.Internal.json
96+
**/appsettings.Main.json
9597
ArenaService.IntegrationTests/**/*.received.*

ArenaService/Setup.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace ArenaService;
22

3+
using System.IdentityModel.Tokens.Jwt;
4+
using System.Net.Http.Headers;
5+
using System.Text;
36
using System.Text.Json.Serialization;
47
using ArenaService.Auth;
58
using ArenaService.Data;
@@ -15,6 +18,7 @@ namespace ArenaService;
1518
using Microsoft.AspNetCore.Authentication;
1619
using Microsoft.EntityFrameworkCore;
1720
using Microsoft.Extensions.Options;
21+
using Microsoft.IdentityModel.Tokens;
1822
using Microsoft.OpenApi.Models;
1923
using Newtonsoft.Json.Converters;
2024
using StackExchange.Redis;
@@ -43,6 +47,26 @@ public void ConfigureServices(IServiceCollection services)
4347
{
4448
var headlessOptions = provider.GetRequiredService<IOptions<HeadlessOptions>>();
4549
client.BaseAddress = headlessOptions.Value.HeadlessEndpoint;
50+
51+
if (
52+
headlessOptions.Value.JwtSecretKey is not null
53+
&& headlessOptions.Value.JwtIssuer is not null
54+
)
55+
{
56+
var key = new SymmetricSecurityKey(
57+
Encoding.UTF8.GetBytes(headlessOptions.Value.JwtSecretKey)
58+
);
59+
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
60+
var token = new JwtSecurityToken(
61+
issuer: headlessOptions.Value.JwtIssuer,
62+
expires: DateTime.UtcNow.AddMinutes(5),
63+
signingCredentials: creds
64+
);
65+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
66+
"Bearer",
67+
new JwtSecurityTokenHandler().WriteToken(token)
68+
);
69+
}
4670
}
4771
);
4872

0 commit comments

Comments
 (0)