-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace CleanAspCore.Api.Tests.TestSetup; | ||
|
||
public static class TestJwtGenerator | ||
{ | ||
private const string Audience = "TestUsers"; | ||
private static readonly string _issuer = Guid.NewGuid().ToString(); | ||
private static readonly SecurityKey _securityKey = new SymmetricSecurityKey("A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4E5F6"u8.ToArray()) { KeyId = Guid.NewGuid().ToString() }; | ||
private static readonly SigningCredentials _signingCredentials = new SigningCredentials(_securityKey, SecurityAlgorithms.HmacSha256); | ||
|
||
private static readonly JwtSecurityTokenHandler _sTokenHandler = new(); | ||
|
||
public static string GenerateJwtToken(IEnumerable<Claim> claims) | ||
{ | ||
return _sTokenHandler.WriteToken(new JwtSecurityToken(_issuer, Audience, claims, null, DateTime.UtcNow.AddMinutes(20), _signingCredentials)); | ||
} | ||
|
||
public static IServiceCollection ConfigureTestJwt(this IServiceCollection services) | ||
{ | ||
services.RemoveAll(typeof(IConfigureOptions<JwtBearerOptions>)); // Remove any already configured jwt bearer options configurators as we want to be in control of this in the tests. | ||
services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options => | ||
{ | ||
options.Configuration = new OpenIdConnectConfiguration | ||
{ | ||
Issuer = _issuer, | ||
SigningKeys = { _securityKey } | ||
}; | ||
options.Audience = Audience; | ||
}); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters