Skip to content

Commit c8faa30

Browse files
authored
Merge pull request #331 from meysamhadeli/fix/fix-jwt-config
fix/fix jwt config
2 parents c9b1767 + 33c2f9c commit c8faa30

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

building-blocks/Jwt/JwtExtensions.cs

+10-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public static IServiceCollection AddJwt(this IServiceCollection services)
3030
options.TokenValidationParameters = new TokenValidationParameters
3131
{
3232
ValidateAudience = false,
33-
ClockSkew = TimeSpan.FromSeconds(2) // For prevent add default value (5min) to life time token!
33+
ClockSkew = TimeSpan.FromSeconds(2), // For prevent add default value (5min) to life time token!
34+
ValidateLifetime = true, // Enforce token expiry
3435
};
3536

3637
options.RequireHttpsMetadata = jwtOptions.RequireHttpsMetadata;
@@ -48,20 +49,14 @@ public static IServiceCollection AddJwt(this IServiceCollection services)
4849
.RequireAuthenticatedUser()
4950
.Build();
5051

51-
// Add your scope policy (optional)
52-
if (!string.IsNullOrEmpty(jwtOptions.Audience))
53-
{
54-
options.AddPolicy(
55-
nameof(ApiScope),
56-
policy =>
57-
{
58-
policy.AuthenticationSchemes.Add(
59-
JwtBearerDefaults.AuthenticationScheme);
60-
61-
policy.RequireAuthenticatedUser();
62-
policy.RequireClaim("scope", jwtOptions.Audience);
63-
});
64-
}
52+
options.AddPolicy(
53+
nameof(ApiScope),
54+
policy =>
55+
{
56+
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
57+
policy.RequireAuthenticatedUser();
58+
policy.RequireClaim("scope", jwtOptions.Audience);
59+
});
6560
});
6661
}
6762

building-blocks/TestBase/TestBase.cs

+24-14
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
using BuildingBlocks.Mongo;
88
using BuildingBlocks.PersistMessageProcessor;
99
using BuildingBlocks.Web;
10+
using Duende.IdentityServer.EntityFramework.Entities;
1011
using EasyNetQ.Management.Client;
1112
using Grpc.Net.Client;
1213
using MassTransit;
1314
using MassTransit.Testing;
1415
using MediatR;
16+
using Microsoft.AspNetCore.Authorization;
1517
using Microsoft.AspNetCore.Hosting;
1618
using Microsoft.AspNetCore.Http;
1719
using Microsoft.AspNetCore.Mvc.Testing;
@@ -57,16 +59,15 @@ public HttpClient HttpClient
5759
{
5860
get
5961
{
60-
var claims =
61-
new Dictionary<string, object>
62-
{
63-
{ClaimTypes.Name, "[email protected]"},
64-
{ClaimTypes.Role, "admin"},
65-
{"scope", "flight-api"}
66-
};
62+
var claims = new Dictionary<string, object>
63+
{
64+
{ ClaimTypes.Name, "[email protected]" },
65+
{ ClaimTypes.Role, "admin" },
66+
{ "scope", "flight-api" }
67+
};
6768

68-
var httpClient = _factory?.CreateClient();
69-
httpClient.SetFakeBearerToken(claims);
69+
var httpClient = _factory.CreateClient();
70+
httpClient.SetFakeBearerToken(claims); // Uses FakeJwtBearer
7071
return httpClient;
7172
}
7273
}
@@ -106,19 +107,28 @@ protected TestFixture()
106107
.AsImplementedInterfaces()
107108
.WithScopedLifetime());
108109

109-
// add authentication using a fake jwt bearer - we can use SetAdminUser method to set authenticate user to existing HttContextAccessor
110+
// Add Fake JWT Authentication - we can use SetAdminUser method to set authenticate user to existing HttContextAccessor
110111
// https://github.com/webmotions/fake-authentication-jwtbearer
111112
// https://github.com/webmotions/fake-authentication-jwtbearer/issues/14
112113
services.AddAuthentication(
113114
options =>
114115
{
115-
options.DefaultAuthenticateScheme =
116-
FakeJwtBearerDefaults.AuthenticationScheme;
116+
options.DefaultAuthenticateScheme = FakeJwtBearerDefaults.AuthenticationScheme;
117117

118-
options.DefaultChallengeScheme =
119-
FakeJwtBearerDefaults.AuthenticationScheme;
118+
options.DefaultChallengeScheme = FakeJwtBearerDefaults.AuthenticationScheme;
120119
})
121120
.AddFakeJwtBearer();
121+
122+
// Mock Authorization Policies
123+
services.AddAuthorization(options =>
124+
{
125+
options.AddPolicy(nameof(ApiScope), policy =>
126+
{
127+
policy.AddAuthenticationSchemes(FakeJwtBearerDefaults.AuthenticationScheme);
128+
policy.RequireAuthenticatedUser();
129+
policy.RequireClaim("scope", "flight-api"); // Test-specific scope
130+
});
131+
});
122132
});
123133
});
124134
}

0 commit comments

Comments
 (0)