33using System . Text . Encodings . Web ;
44
55using Microsoft . AspNetCore . Authentication ;
6+ using Microsoft . AspNetCore . Http ;
67using Microsoft . Extensions . DependencyInjection ;
78using Microsoft . Extensions . Logging ;
89using Microsoft . Extensions . Options ;
@@ -16,6 +17,7 @@ namespace AspNetCore.SecurityKey;
1617public class SecurityKeyAuthenticationHandler : AuthenticationHandler < SecurityKeyAuthenticationSchemeOptions >
1718{
1819 private static readonly AuthenticateResult InvalidSecurityKey = AuthenticateResult . Fail ( "Invalid Security Key" ) ;
20+ private static readonly AuthenticateResult AuthenticationError = AuthenticateResult . Fail ( "Authentication error" ) ;
1921
2022 /// <summary>
2123 /// Initializes a new instance of the <see cref="SecurityKeyAuthenticationHandler"/> class.
@@ -40,6 +42,7 @@ public SecurityKeyAuthenticationHandler(
4042 protected override async Task < AuthenticateResult > HandleAuthenticateAsync ( )
4143 {
4244 var startTimestamp = 0L ;
45+ string ? endpoint = null ;
4346 Activity ? activity = null ;
4447
4548 try
@@ -59,7 +62,10 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
5962 startTimestamp = Stopwatch . GetTimestamp ( ) ;
6063 activity = SecurityKeyDiagnostics . ActivitySource . StartActivity ( SecurityKeyDiagnostics . AuthenticationActivityName , ActivityKind . Server ) ;
6164
65+ endpoint = GetEndpoint ( ) ;
66+
6267 activity ? . SetTag ( SecurityKeyDiagnostics . AuthenticationSchemeTagName , Scheme . Name ) ;
68+ activity ? . SetTag ( SecurityKeyDiagnostics . EndpointTagName , endpoint ) ;
6369
6470 var ipAddress = keyExtractor . GetRemoteAddress ( Context ) ;
6571
@@ -70,7 +76,6 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
7076
7177 // Authenticate the security key and get the claims identity
7278 var identity = await keyValidator . Authenticate ( securityKey , ipAddress , Scheme . Name , Context . RequestAborted ) ;
73- var securityKeyHash = SecurityKeyDiagnostics . ComputeSecurityKeyHash ( securityKey ) ;
7479
7580 if ( ! identity . IsAuthenticated )
7681 {
@@ -81,7 +86,8 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
8186 activity : activity ,
8287 startTimestamp : startTimestamp ,
8388 authenticationResult : SecurityKeyDiagnostics . AuthenticationResultFailure ,
84- securityKeyHash : securityKeyHash ,
89+ securityKey : securityKey ,
90+ endpoint : endpoint ,
8591 failureReason : SecurityKeyDiagnostics . InvalidSecurityKeyFailureReason ) ;
8692 }
8793
@@ -94,53 +100,57 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
94100 activity : activity ,
95101 startTimestamp : startTimestamp ,
96102 authenticationResult : SecurityKeyDiagnostics . AuthenticationResultSuccess ,
97- securityKeyHash : securityKeyHash ) ;
103+ securityKey : securityKey ,
104+ endpoint : endpoint ) ;
98105
99106 }
107+ catch ( OperationCanceledException ) when ( Context . RequestAborted . IsCancellationRequested )
108+ {
109+ throw ;
110+ }
100111 catch ( Exception ex ) when ( ex is not OperationCanceledException )
101112 {
102113 activity ? . AddException ( ex ) ;
103114
104- CompleteAuthentication (
105- result : AuthenticateResult . Fail ( ex ) ,
115+ return CompleteAuthentication (
116+ result : AuthenticationError ,
106117 activity : activity ,
107118 startTimestamp : startTimestamp ,
108119 authenticationResult : SecurityKeyDiagnostics . AuthenticationResultFailure ,
120+ endpoint : endpoint ,
109121 failureReason : SecurityKeyDiagnostics . AuthenticationErrorFailureReason ) ;
110-
111- throw ;
112122 }
113123 finally
114124 {
115125 activity ? . Dispose ( ) ;
116126 }
117127 }
118128
119- private static AuthenticateResult CompleteAuthentication (
129+ private AuthenticateResult CompleteAuthentication (
120130 AuthenticateResult result ,
121131 Activity ? activity ,
122132 long startTimestamp ,
123133 string authenticationResult ,
124- string ? securityKeyHash = null ,
134+ string ? securityKey = null ,
135+ string ? endpoint = null ,
125136 string ? failureReason = null )
126137 {
127- activity ? . SetTag ( SecurityKeyDiagnostics . AuthenticationResultTagName , authenticationResult ) ;
128-
129- if ( securityKeyHash is not null )
130- activity ? . SetTag ( SecurityKeyDiagnostics . SecurityKeyHashTagName , securityKeyHash ) ;
131-
132- if ( failureReason is not null )
133- activity ? . SetTag ( SecurityKeyDiagnostics . AuthenticationFailureReasonTagName , failureReason ) ;
134-
135- if ( authenticationResult == SecurityKeyDiagnostics . AuthenticationResultFailure )
136- activity ? . SetStatus ( ActivityStatusCode . Error , failureReason ) ;
137-
138- SecurityKeyDiagnostics . RecordAuthenticationMetrics (
138+ SecurityKeyDiagnostics . CompleteAuthentication (
139+ activity : activity ,
139140 startTimestamp : startTimestamp ,
140141 authenticationResult : authenticationResult ,
141- failureReason : failureReason ,
142- securityKeyHash : securityKeyHash ) ;
142+ scheme : Scheme . Name ,
143+ securityKey : securityKey ,
144+ endpoint : endpoint ,
145+ failureReason : failureReason ) ;
143146
144147 return result ;
145148 }
149+
150+ private string GetEndpoint ( )
151+ {
152+ return Context . GetEndpoint ( ) ? . DisplayName
153+ ?? Request . Path . Value
154+ ?? "unknown" ;
155+ }
146156}
0 commit comments