@@ -25,7 +25,7 @@ public class ElectronAuthenticationMiddleware
2525 private const string AuthCookieName = "ElectronAuth" ;
2626
2727 public ElectronAuthenticationMiddleware (
28- RequestDelegate next ,
28+ RequestDelegate next ,
2929 IElectronAuthenticationService authService ,
3030 ILogger < ElectronAuthenticationMiddleware > logger )
3131 {
@@ -37,10 +37,10 @@ public ElectronAuthenticationMiddleware(
3737 public async Task InvokeAsync ( HttpContext context )
3838 {
3939 var path = context . Request . Path . Value ;
40-
40+
4141 // Check if authentication cookie exists
4242 var authCookie = context . Request . Cookies [ AuthCookieName ] ;
43-
43+
4444 if ( ! string . IsNullOrEmpty ( authCookie ) )
4545 {
4646 // Cookie present - validate it
@@ -52,8 +52,7 @@ public async Task InvokeAsync(HttpContext context)
5252 else
5353 {
5454 // Invalid cookie - reject
55- _logger . LogWarning ( "Authentication failed: Invalid cookie for path {Path} from {RemoteIp}" ,
56- path , context . Connection . RemoteIpAddress ) ;
55+ _logger . LogWarning ( "Authentication failed: Invalid cookie for path {Path} from {RemoteIp}" , path , context . Connection . RemoteIpAddress ) ;
5756 context . Response . StatusCode = 401 ;
5857 await context . Response . WriteAsync ( "Unauthorized: Invalid authentication" ) ;
5958 return ;
@@ -62,14 +61,14 @@ public async Task InvokeAsync(HttpContext context)
6261
6362 // No cookie - check for token in query string (first-time authentication)
6463 var token = context . Request . Query [ "token" ] . ToString ( ) ;
65-
64+
6665 if ( ! string . IsNullOrEmpty ( token ) )
6766 {
6867 if ( _authService . ValidateToken ( token ) )
6968 {
7069 // Valid token - set cookie for future requests
7170 _logger . LogInformation ( "Authentication successful: Setting cookie for path {Path}" , path ) ;
72-
71+
7372 context . Response . Cookies . Append ( AuthCookieName , token , new CookieOptions
7473 {
7574 HttpOnly = true , // Prevent JavaScript access (XSS protection)
@@ -78,24 +77,22 @@ public async Task InvokeAsync(HttpContext context)
7877 Secure = false , // False because localhost is HTTP
7978 IsEssential = true // Required for app to function
8079 } ) ;
81-
80+
8281 await _next ( context ) ;
8382 return ;
8483 }
8584 else
8685 {
8786 // Invalid token - reject
88- _logger . LogWarning ( "Authentication failed: Invalid token (prefix: {TokenPrefix}...) for path {Path} from {RemoteIp}" ,
89- token . Length > 8 ? token . Substring ( 0 , 8 ) : token , path , context . Connection . RemoteIpAddress ) ;
87+ _logger . LogWarning ( "Authentication failed: Invalid token (prefix: {TokenPrefix}...) for path {Path} from {RemoteIp}" , token . Length > 8 ? token . Substring ( 0 , 8 ) : token , path , context . Connection . RemoteIpAddress ) ;
9088 context . Response . StatusCode = 401 ;
9189 await context . Response . WriteAsync ( "Unauthorized: Invalid authentication" ) ;
9290 return ;
9391 }
9492 }
9593
9694 // Neither cookie nor valid token present - reject
97- _logger . LogWarning ( "Authentication failed: No cookie or token provided for path {Path} from {RemoteIp}" ,
98- path , context . Connection . RemoteIpAddress ) ;
95+ _logger . LogWarning ( "Authentication failed: No cookie or token provided for path {Path} from {RemoteIp}" , path , context . Connection . RemoteIpAddress ) ;
9996 context . Response . StatusCode = 401 ;
10097 await context . Response . WriteAsync ( "Unauthorized: Authentication required" ) ;
10198 }
0 commit comments