Skip to content

Commit 21162a2

Browse files
feature(#11): this commit introduces validation of the token type and issuer in the OnTokenValidated event. Specific errors are logged in HttpContext.Items and returned in OnChallenge, enabling more detailed authentication responses.
1 parent a5f1b17 commit 21162a2

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

  • Applications/Backend/Source/HttpsRichardy.Federation.WebApi/Constants

Applications/Backend/Source/HttpsRichardy.Federation.WebApi/Constants/Authentication.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ public static class Authentication
2626
return context.Response.WriteAsync(JsonSerializer.Serialize(AuthenticationErrors.InvalidTokenFormat, _serializer));
2727
},
2828

29+
OnTokenValidated = context =>
30+
{
31+
var request = context.HttpContext.Request;
32+
if (context.SecurityToken is not Microsoft.IdentityModel.JsonWebTokens.JsonWebToken token)
33+
{
34+
context.HttpContext.Items["authentication.error"] = AuthenticationErrors.InvalidTokenFormat;
35+
context.Fail("The token format is invalid or the token is malformed.");
36+
37+
return Task.CompletedTask;
38+
}
39+
40+
var expectedIssuer = $"{request.Scheme}://{request.Host}".TrimEnd('/');
41+
var actualIssuer = token.Issuer?.TrimEnd('/');
42+
43+
if (!string.Equals(actualIssuer, expectedIssuer, StringComparison.OrdinalIgnoreCase))
44+
{
45+
context.HttpContext.Items["authentication.error"] = AuthenticationErrors.InvalidIssuer;
46+
context.Fail("The token issuer is invalid.");
47+
48+
return Task.CompletedTask;
49+
}
50+
51+
return Task.CompletedTask;
52+
},
53+
2954
OnChallenge = context =>
3055
{
3156
context.HandleResponse();
@@ -36,7 +61,11 @@ public static class Authentication
3661
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
3762
context.Response.ContentType = MediaTypeNames.Application.Json;
3863

39-
return context.Response.WriteAsync(JsonSerializer.Serialize(AuthenticationErrors.Unauthenticated, _serializer));
64+
var error = context.HttpContext.Items["authentication.error"] as Error
65+
?? AuthenticationErrors.Unauthenticated;
66+
67+
return context.Response.WriteAsync(JsonSerializer.Serialize(error, _serializer));
4068
}
4169
};
42-
}
70+
}
71+

0 commit comments

Comments
 (0)