Skip to content

Commit

Permalink
Request with jwt token
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus committed Feb 13, 2025
1 parent 9db6af2 commit d081a22
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ fabric.properties

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

ArenaService/appsettings.Local.json
**/appsettings.Local.json
**/appsettings.Internal.json
**/appsettings.Main.json
ArenaService.IntegrationTests/**/*.received.*
24 changes: 24 additions & 0 deletions ArenaService/Setup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace ArenaService;

using System.IdentityModel.Tokens.Jwt;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json.Serialization;
using ArenaService.Auth;
using ArenaService.Data;
Expand All @@ -15,6 +18,7 @@ namespace ArenaService;
using Microsoft.AspNetCore.Authentication;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
using StackExchange.Redis;
Expand Down Expand Up @@ -43,6 +47,26 @@ public void ConfigureServices(IServiceCollection services)
{
var headlessOptions = provider.GetRequiredService<IOptions<HeadlessOptions>>();
client.BaseAddress = headlessOptions.Value.HeadlessEndpoint;

if (
headlessOptions.Value.JwtSecretKey is not null
&& headlessOptions.Value.JwtIssuer is not null
)
{
var key = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(headlessOptions.Value.JwtSecretKey)
);
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: headlessOptions.Value.JwtIssuer,
expires: DateTime.UtcNow.AddMinutes(5),
signingCredentials: creds
);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
new JwtSecurityTokenHandler().WriteToken(token)
);
}
}
);

Expand Down

0 comments on commit d081a22

Please sign in to comment.