Skip to content

Commit 5da6125

Browse files
committed
Add test case with expired token
Signed-off-by: Victor Chang <vicchang@nvidia.com>
1 parent efe5bd8 commit 5da6125

File tree

9 files changed

+24
-9
lines changed

9 files changed

+24
-9
lines changed

src/Authentication/Extensions/HttpContextExtension.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public static List<string> GetValidEndpoints(this HttpContext httpcontext, List<
4646
{
4747
return claim.Endpoints!;
4848
}
49-
5049
}
5150

5251
return new List<string>();

src/Authentication/Extensions/MonaiAuthenticationExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using System.Text;
1818
using Microsoft.AspNetCore.Authentication;
1919
using Microsoft.AspNetCore.Authentication.JwtBearer;
20-
using Microsoft.AspNetCore.Authorization;
2120
using Microsoft.Extensions.Configuration;
2221
using Microsoft.Extensions.DependencyInjection;
2322
using Microsoft.Extensions.Logging;

src/Authentication/Tests/EndpointAuthorizationMiddlewareTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsAuthentic
109109
Assert.Equal(HttpStatusCode.Forbidden, responseMessage.StatusCode);
110110
}
111111

112+
[Theory]
113+
[InlineData("role-with-test")]
114+
public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserProvidesAnExpiredToken_ExpectToDenyRequest(string role)
115+
{
116+
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(false);
117+
118+
var server = host.GetTestServer();
119+
server.BaseAddress = new Uri("https://example.com/");
120+
121+
var token = MockJwtTokenHandler.GenerateJwtToken(role, -5);
122+
123+
var client = server.CreateClient();
124+
client.DefaultRequestHeaders.Add("Authorization", $"{JwtBearerDefaults.AuthenticationScheme} {token}");
125+
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
126+
127+
Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode);
128+
}
112129

113130
private static Action<IWebHostBuilder> SetupWebServer(string configFile) => webBuilder =>
114131
{

src/Authentication/Tests/MockJwtTokenHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ static MockJwtTokenHandler()
3737
SigningCredentials = new SigningCredentials(SecurityKey, SecurityAlgorithms.HmacSha256Signature);
3838
}
3939

40-
public static string GenerateJwtToken(string role)
40+
public static string GenerateJwtToken(string role, int expiresInMinutes = 5)
4141
{
4242
var claims = new[] { new Claim("user_roles", role) };
43-
return TokenHandler.WriteToken(new JwtSecurityToken(Issuer, "monai-app", claims, null, DateTime.UtcNow.AddMinutes(20), SigningCredentials));
43+
return TokenHandler.WriteToken(new JwtSecurityToken(Issuer, "monai-app", claims, null, DateTime.UtcNow.AddMinutes(expiresInMinutes), SigningCredentials));
4444
}
4545
}
4646
}

src/Authentication/Tests/test.auth.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
}
2929
}
3030
}
31-
}
31+
}

src/Authentication/Tests/test.bypassd.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"MonaiDeployAuthentication": {
33
"BypassAuthentication": true
44
}
5-
}
5+
}

src/Authentication/Tests/test.emptyopenid.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
"BypassAuthentication": false,
44
"OpenId": {}
55
}
6-
}
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
{
2-
}
2+
}

src/Authentication/example.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
}
2828
}
2929
}
30-
}
30+
}

0 commit comments

Comments
 (0)