server-master/srcs/PhoenixLib.Auth.JWT/DependencyInjectionExtensions.cs

20 lines
No EOL
697 B
C#

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace PhoenixLib.Auth.JWT
{
public static class DependencyInjectionExtensions
{
public static void AddJwtFactoryFromEnv(this IServiceCollection services)
{
string jwtPrivateKey = Environment.GetEnvironmentVariable("JWT_PRIVATE_KEY");
if (string.IsNullOrWhiteSpace(jwtPrivateKey))
{
throw new InvalidOperationException("JWT_PRIVATE_KEY environment variable is required");
}
services.TryAddSingleton<IJwtTokenFactory>(new JwtTokenFactory(jwtPrivateKey));
}
}
}