11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics ;
4- using System . Linq ;
54using System . Net ;
65using System . Net . Http ;
76using System . Threading . Tasks ;
@@ -28,6 +27,11 @@ public sealed class AuthenticationDetails
2827 /// OAuth resource to request authentication for.
2928 /// </summary>
3029 public Uri Resource { get ; internal set ; }
30+
31+ /// <summary>
32+ /// Error message if probing failed.
33+ /// </summary>
34+ public string ErrorMessage { get ; internal set ; } = string . Empty ;
3135 }
3236
3337 /// <summary>
@@ -57,7 +61,7 @@ public AuthorityResolver(HttpClient httpClient, Action<TraceEventType, string> l
5761 }
5862
5963 /// <summary>
60- /// Attemtps to solicit a WWW-Authenticate reply using an unauthenticated GET call to the given endpoint.
64+ /// Attempts to solicit a WWW-Authenticate reply using an unauthenticated GET call to the given endpoint.
6165 /// Parses returned header for details
6266 /// </summary>
6367 /// <param name="endpoint">endpoint to challenge for authority and resource</param>
@@ -81,28 +85,31 @@ public async Task<AuthenticationDetails> ProbeForExpectedAuthentication(Uri endp
8185 {
8286 errDetails = $ "; details: { wex . Message } ({ wex . Status } )";
8387 }
84- LogError ( $ "Failed to get response from: { endpoint } ; error: { ex . Message } { errDetails } ") ;
88+
89+ details . ErrorMessage = $ "Failed to get response from: { endpoint } ; error: { ex . Message } { errDetails } ";
90+ LogError ( details . ErrorMessage ) ;
8591 return details ;
8692 }
8793
8894
8995 if ( response . StatusCode == HttpStatusCode . NotFound || response . StatusCode == HttpStatusCode . BadRequest )
9096 {
9197 // didn't find endpoint.
92- LogError ( $ "Failed to get Authority and Resource error. Attempt to Access Endpoint { endpoint } resulted in { response . StatusCode } .") ;
98+ details . ErrorMessage = $ "Failed to get Authority and Resource error. Attempt to Access Endpoint { endpoint } resulted in { response . StatusCode } .";
99+ LogError ( details . ErrorMessage ) ;
93100 return details ;
94101 }
95102
96103 if ( response . Headers . Contains ( AuthenticateHeader ) )
97104 {
98105 var authenticateHeaders = response . Headers . GetValues ( AuthenticateHeader ) ;
99106 // need to support OnPrem returning multiple Authentication headers.
100- foreach ( var authenticateHeaderraw in authenticateHeaders )
107+ foreach ( var authenticateHeaderRaw in authenticateHeaders )
101108 {
102109 if ( details . Success )
103110 break ;
104111
105- string authenticateHeader = authenticateHeaderraw . Trim ( ) ;
112+ string authenticateHeader = authenticateHeaderRaw . Trim ( ) ;
106113
107114 // This also checks for cases like "BearerXXXX authorization_uri=...." and "Bearer" and "Bearer "
108115 if ( ! authenticateHeader . StartsWith ( Bearer , StringComparison . OrdinalIgnoreCase )
@@ -112,7 +119,8 @@ public async Task<AuthenticationDetails> ProbeForExpectedAuthentication(Uri endp
112119 if ( isOnPrem )
113120 continue ;
114121
115- LogError ( $ "Malformed 'Bearer' format: { authenticateHeader } ") ;
122+ details . ErrorMessage = $ "Malformed 'Bearer' format: { authenticateHeader } ";
123+ LogError ( details . ErrorMessage ) ;
116124 return details ;
117125 }
118126
@@ -126,15 +134,17 @@ public async Task<AuthenticationDetails> ProbeForExpectedAuthentication(Uri endp
126134 }
127135 catch ( ArgumentException )
128136 {
129- LogError ( $ "Malformed arguments in '{ AuthenticateHeader } : { authenticateHeader } ") ;
137+ details . ErrorMessage = $ "Malformed arguments in '{ AuthenticateHeader } : { authenticateHeader } ";
138+ LogError ( details . ErrorMessage ) ;
130139 return details ;
131140 }
132141
133142 if ( authenticateHeaderItems != null )
134143 {
135144 if ( ! authenticateHeaderItems . TryGetValue ( AuthorityKey , out var auth ) )
136145 {
137- LogError ( $ "Response header from { endpoint } is missing expected key/value for { AuthorityKey } ") ;
146+ details . ErrorMessage = $ "Response header from { endpoint } is missing expected key/value for { AuthorityKey } ";
147+ LogError ( details . ErrorMessage ) ;
138148 return details ;
139149 }
140150 details . Authority = new Uri (
@@ -143,7 +153,8 @@ public async Task<AuthenticationDetails> ProbeForExpectedAuthentication(Uri endp
143153
144154 if ( ! authenticateHeaderItems . TryGetValue ( ResourceKey , out var res ) )
145155 {
146- LogError ( $ "Response header from { endpoint } is missing expected key/value for { ResourceKey } ") ;
156+ details . ErrorMessage = $ "Response header from { endpoint } is missing expected key/value for { ResourceKey } ";
157+ LogError ( details . ErrorMessage ) ;
147158 return details ;
148159 }
149160 details . Resource = new Uri ( res ) ;
0 commit comments