11namespace ArenaService ;
22
3+ using System . IdentityModel . Tokens . Jwt ;
4+ using System . Net . Http . Headers ;
5+ using System . Text ;
36using System . Text . Json . Serialization ;
47using ArenaService . Auth ;
58using ArenaService . Data ;
@@ -15,6 +18,7 @@ namespace ArenaService;
1518using Microsoft . AspNetCore . Authentication ;
1619using Microsoft . EntityFrameworkCore ;
1720using Microsoft . Extensions . Options ;
21+ using Microsoft . IdentityModel . Tokens ;
1822using Microsoft . OpenApi . Models ;
1923using Newtonsoft . Json . Converters ;
2024using 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