Skip to content

Commit 528da28

Browse files
authored
Merge pull request #630 from bcgov/yj
chore: aps auth
2 parents c72eda9 + f7818e1 commit 528da28

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

server/StrDss.Api/Authentication/ApsJwtBearerEvents.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public ApsJwtBearerEvents(ICurrentUser currentUser, IUserService userService, IR
2727

2828
public override async Task AuthenticationFailed(AuthenticationFailedContext context)
2929
{
30-
_logger.LogWarning("ApsJwt Authentication failed: " + context.Exception.Message);
30+
var clientId = context.HttpContext.User?.FindFirst(StrDssClaimTypes.ClientId)?.Value ?? "Unknown";
31+
var ipAddress = context.HttpContext.Connection?.RemoteIpAddress?.ToString() ?? "Unknown IP";
32+
33+
clientId = clientId == "" ? "" : clientId;
34+
35+
_logger.LogWarning($"[AUTH] Aps Authentication failed for user '{clientId}' from IP address '{ipAddress}'.");
36+
3137
await base.AuthenticationFailed(context);
3238
}
3339

server/StrDss.Api/Authentication/KcJwtBearerEvents.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,27 @@ public KcJwtBearerEvents(ICurrentUser currentUser, IUserService userService, IBc
2222
_logger = logger;
2323
}
2424

25-
//public override Task Challenge(JwtBearerChallengeContext context)
26-
//{
27-
// var username = context.HttpContext.User?.Identity?.Name ?? "Unknown";
28-
// var ipAddress = context.HttpContext.Connection.RemoteIpAddress;
29-
// var ip = ipAddress == null ? "Unknown" : ipAddress.ToString();
30-
31-
// if (!context.HttpContext.Request.Headers.ContainsKey("Authorization"))
32-
// {
33-
// _logger.LogWarning($"[AUTH] Authentication failed for user '{username}' from IP address '{ip}'. Authorization header is missing.");
34-
// }
35-
36-
// return base.Challenge(context);
37-
//}
38-
3925
public override async Task AuthenticationFailed(AuthenticationFailedContext context)
4026
{
41-
_logger.LogWarning("KcJwt Authentication failed: " + context.Exception.Message);
27+
try
28+
{
29+
var username = context.HttpContext.User?.Identity?.Name ?? "Unknown";
30+
var ipAddress = context.HttpContext.Connection?.RemoteIpAddress?.ToString() ?? "Unknown IP";
31+
32+
if (!context.HttpContext.Request.Headers.ContainsKey("Authorization"))
33+
{
34+
_logger.LogWarning($"[AUTH] KC Authentication failed for user '{username}' from IP address '{ipAddress}'. Authorization header is missing.");
35+
}
36+
else
37+
{
38+
_logger.LogDebug($"[AUTH] Authorization header present. Proceeding with Aps authentication for user '{username}' from IP address '{ipAddress}'.");
39+
}
40+
}
41+
catch (Exception ex)
42+
{
43+
_logger.LogError(ex, "An error occurred while processing the authentication failure.");
44+
}
45+
4246
await base.AuthenticationFailed(context);
4347
}
4448

server/StrDss.Api/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using StrDss.Service.Bceid;
1919
using Npgsql;
2020
using Serilog;
21+
using Microsoft.AspNetCore.Authentication;
2122

2223
var builder = WebApplication.CreateBuilder(args);
2324

@@ -195,6 +196,26 @@
195196

196197
app.UseAuthorization();
197198

199+
//app.Use(async (context, next) =>
200+
//{
201+
// var result = await context.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
202+
203+
// if (!result.Succeeded)
204+
// {
205+
// // Try to authenticate with the second scheme
206+
// result = await context.AuthenticateAsync(apsAuthScheme);
207+
208+
// if (!result.Succeeded)
209+
// {
210+
// // Authentication failed for both schemes, challenge the user
211+
// await context.ChallengeAsync(apsAuthScheme);
212+
// return;
213+
// }
214+
// }
215+
216+
// await next();
217+
//});
218+
198219
app.MapControllers();
199220

200221
app.Run();

0 commit comments

Comments
 (0)