Skip to content
This repository was archived by the owner on Dec 24, 2020. It is now read-only.

Commit fb3929b

Browse files
committed
Simplify the logging calls in the OWIN version
1 parent 58a0c83 commit fb3929b

2 files changed

Lines changed: 46 additions & 42 deletions

File tree

src/Owin.Security.OAuth.Introspection/OAuthIntrospectionHandler.cs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
3636
// indicate that authentication was rejected by application code.
3737
if (context.Ticket == null)
3838
{
39-
Options.Logger.LogInformation("Authentication was stopped by application code.");
39+
Logger.LogInformation("Authentication was stopped by application code.");
4040

4141
return null;
4242
}
@@ -46,7 +46,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
4646

4747
else if (context.Skipped)
4848
{
49-
Options.Logger.LogInformation("Authentication was skipped by application code.");
49+
Logger.LogInformation("Authentication was skipped by application code.");
5050

5151
return null;
5252
}
@@ -59,7 +59,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
5959
var header = Request.Headers[OAuthIntrospectionConstants.Headers.Authorization];
6060
if (string.IsNullOrEmpty(header))
6161
{
62-
Options.Logger.LogDebug("Authentication was skipped because no bearer token was received.");
62+
Logger.LogDebug("Authentication was skipped because no bearer token was received.");
6363

6464
return null;
6565
}
@@ -68,8 +68,8 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
6868
// See https://tools.ietf.org/html/rfc6750#section-2.1
6969
if (!header.StartsWith(OAuthIntrospectionConstants.Schemes.Bearer + ' ', StringComparison.OrdinalIgnoreCase))
7070
{
71-
Options.Logger.LogDebug("Authentication was skipped because an incompatible " +
72-
"scheme was used in the 'Authorization' header.");
71+
Logger.LogDebug("Authentication was skipped because an incompatible " +
72+
"scheme was used in the 'Authorization' header.");
7373

7474
return null;
7575
}
@@ -79,8 +79,8 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
7979

8080
if (string.IsNullOrEmpty(token))
8181
{
82-
Options.Logger.LogDebug("Authentication was skipped because the bearer token " +
83-
"was missing from the 'Authorization' header.");
82+
Logger.LogDebug("Authentication was skipped because the bearer token " +
83+
"was missing from the 'Authorization' header.");
8484

8585
return null;
8686
}
@@ -96,8 +96,8 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
9696
var payload = await GetIntrospectionPayloadAsync(token);
9797
if (payload == null || !payload.Value<bool>(OAuthIntrospectionConstants.Claims.Active))
9898
{
99-
Options.Logger.LogError("Authentication failed because the authorization " +
100-
"server rejected the access token.");
99+
Logger.LogError("Authentication failed because the authorization " +
100+
"server rejected the access token.");
101101

102102
Context.Set(typeof(OAuthIntrospectionError).FullName, new OAuthIntrospectionError
103103
{
@@ -120,7 +120,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
120120
if (ticket.Properties.ExpiresUtc.HasValue &&
121121
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow)
122122
{
123-
Options.Logger.LogError("Authentication failed because the access token was expired.");
123+
Logger.LogError("Authentication failed because the access token was expired.");
124124

125125
Context.Set(typeof(OAuthIntrospectionError).FullName, new OAuthIntrospectionError
126126
{
@@ -135,8 +135,8 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
135135
// to be used with this resource server.
136136
if (!ValidateAudience(ticket))
137137
{
138-
Options.Logger.LogError("Authentication failed because the access token " +
139-
"was not valid for this resource server.");
138+
Logger.LogError("Authentication failed because the access token " +
139+
"was not valid for this resource server.");
140140

141141
Context.Set(typeof(OAuthIntrospectionError).FullName, new OAuthIntrospectionError
142142
{
@@ -156,7 +156,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
156156
// indicate that authentication was rejected by application code.
157157
if (notification.Ticket == null)
158158
{
159-
Options.Logger.LogInformation("Authentication was stopped by application code.");
159+
Logger.LogInformation("Authentication was stopped by application code.");
160160

161161
return null;
162162
}
@@ -166,7 +166,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
166166

167167
else if (notification.Skipped)
168168
{
169-
Options.Logger.LogInformation("Authentication was skipped by application code.");
169+
Logger.LogInformation("Authentication was skipped by application code.");
170170

171171
return null;
172172
}
@@ -405,11 +405,11 @@ protected virtual async Task<JObject> GetIntrospectionPayloadAsync(string token)
405405
var response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Request.CallCancelled);
406406
if (!response.IsSuccessStatusCode)
407407
{
408-
Options.Logger.LogError("An error occurred while validating an access token: the remote server " +
409-
"returned a {Status} response with the following payload: {Headers} {Body}.",
410-
/* Status: */ response.StatusCode,
411-
/* Headers: */ response.Headers.ToString(),
412-
/* Body: */ await response.Content.ReadAsStringAsync());
408+
Logger.LogError("An error occurred while validating an access token: the remote server " +
409+
"returned a {Status} response with the following payload: {Headers} {Body}.",
410+
/* Status: */ response.StatusCode,
411+
/* Headers: */ response.Headers.ToString(),
412+
/* Body: */ await response.Content.ReadAsStringAsync());
413413

414414
return null;
415415
}
@@ -425,7 +425,7 @@ protected virtual async Task<JObject> GetIntrospectionPayloadAsync(string token)
425425
{
426426
var payload = JObject.Load(reader);
427427

428-
Options.Logger.LogInformation("The introspection response was successfully extracted: {Response}.", payload);
428+
Logger.LogInformation("The introspection response was successfully extracted: {Response}.", payload);
429429

430430
return payload;
431431
}
@@ -437,8 +437,8 @@ exception is InvalidCastException ||
437437
exception is JsonReaderException ||
438438
exception is JsonSerializationException)
439439
{
440-
Options.Logger.LogError("An error occurred while deserializing the " +
441-
"introspection response: {Exception}.", exception);
440+
Logger.LogError("An error occurred while deserializing the " +
441+
"introspection response: {Exception}.", exception);
442442

443443
return null;
444444
}
@@ -489,7 +489,7 @@ protected virtual async Task<AuthenticationTicket> CreateTicketAsync(string toke
489489
// Always exclude null values, as they can't be represented as valid claims.
490490
if (property.Value.Type == JTokenType.None || property.Value.Type == JTokenType.Null)
491491
{
492-
Options.Logger.LogInformation("The '{Claim}' claim was excluded because it was null.", property.Name);
492+
Logger.LogInformation("The '{Claim}' claim was excluded because it was null.", property.Name);
493493

494494
continue;
495495
}
@@ -511,7 +511,7 @@ protected virtual async Task<AuthenticationTicket> CreateTicketAsync(string toke
511511
// and https://tools.ietf.org/html/rfc7519#section-4.1.6 for more information.
512512
if (property.Value.Type != JTokenType.Float && property.Value.Type != JTokenType.Integer)
513513
{
514-
Options.Logger.LogWarning("The 'iat' claim was ignored because it was not a decimal value.");
514+
Logger.LogWarning("The 'iat' claim was ignored because it was not a decimal value.");
515515

516516
continue;
517517
}
@@ -529,7 +529,7 @@ protected virtual async Task<AuthenticationTicket> CreateTicketAsync(string toke
529529
// and https://tools.ietf.org/html/rfc7519#section-4.1.4 for more information.
530530
if (property.Value.Type != JTokenType.Float && property.Value.Type != JTokenType.Integer)
531531
{
532-
Options.Logger.LogWarning("The 'exp' claim was ignored because it was not a decimal value.");
532+
Logger.LogWarning("The 'exp' claim was ignored because it was not a decimal value.");
533533

534534
continue;
535535
}
@@ -547,7 +547,7 @@ protected virtual async Task<AuthenticationTicket> CreateTicketAsync(string toke
547547
// and https://tools.ietf.org/html/rfc7519#section-4.1.7 for more information.
548548
if (property.Value.Type != JTokenType.String)
549549
{
550-
Options.Logger.LogWarning("The 'jti' claim was ignored because it was not a string value.");
550+
Logger.LogWarning("The 'jti' claim was ignored because it was not a string value.");
551551

552552
continue;
553553
}
@@ -564,7 +564,7 @@ protected virtual async Task<AuthenticationTicket> CreateTicketAsync(string toke
564564
// and https://tools.ietf.org/html/rfc7519#section-4.1.7 for more information.
565565
if (property.Value.Type != JTokenType.String)
566566
{
567-
Options.Logger.LogWarning("The 'scope' claim was ignored because it was not a string value.");
567+
Logger.LogWarning("The 'scope' claim was ignored because it was not a string value.");
568568

569569
continue;
570570
}
@@ -608,7 +608,7 @@ protected virtual async Task<AuthenticationTicket> CreateTicketAsync(string toke
608608
var audiences = (JArray) property.Value;
609609
if (audiences.Any(audience => audience.Type != JTokenType.String))
610610
{
611-
Options.Logger.LogWarning("The 'aud' claim was ignored because it was not an array of strings.");
611+
Logger.LogWarning("The 'aud' claim was ignored because it was not an array of strings.");
612612

613613
continue;
614614
}
@@ -619,7 +619,7 @@ protected virtual async Task<AuthenticationTicket> CreateTicketAsync(string toke
619619
continue;
620620
}
621621

622-
Options.Logger.LogWarning("The 'aud' claim was ignored because it was not a string nor an array.");
622+
Logger.LogWarning("The 'aud' claim was ignored because it was not a string nor an array.");
623623

624624
continue;
625625
}
@@ -751,5 +751,7 @@ protected virtual async Task<AuthenticationTicket> RetrieveTicketAsync(string to
751751

752752
return Options.AccessTokenFormat.Unprotect(Encoding.UTF8.GetString(bytes));
753753
}
754+
755+
public ILogger Logger => Options.Logger;
754756
}
755757
}

src/Owin.Security.OAuth.Validation/OAuthValidationHandler.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
2828
// indicate that authentication was rejected by application code.
2929
if (context.Ticket == null)
3030
{
31-
Options.Logger.LogInformation("Authentication was stopped by application code.");
31+
Logger.LogInformation("Authentication was stopped by application code.");
3232

3333
return null;
3434
}
@@ -38,7 +38,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
3838

3939
else if (context.Skipped)
4040
{
41-
Options.Logger.LogInformation("Authentication was skipped by application code.");
41+
Logger.LogInformation("Authentication was skipped by application code.");
4242

4343
return null;
4444
}
@@ -51,7 +51,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
5151
var header = Request.Headers[OAuthValidationConstants.Headers.Authorization];
5252
if (string.IsNullOrEmpty(header))
5353
{
54-
Options.Logger.LogDebug("Authentication was skipped because no bearer token was received.");
54+
Logger.LogDebug("Authentication was skipped because no bearer token was received.");
5555

5656
return null;
5757
}
@@ -60,8 +60,8 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
6060
// See https://tools.ietf.org/html/rfc6750#section-2.1
6161
if (!header.StartsWith(OAuthValidationConstants.Schemes.Bearer + ' ', StringComparison.OrdinalIgnoreCase))
6262
{
63-
Options.Logger.LogDebug("Authentication was skipped because an incompatible " +
64-
"scheme was used in the 'Authorization' header.");
63+
Logger.LogDebug("Authentication was skipped because an incompatible " +
64+
"scheme was used in the 'Authorization' header.");
6565

6666
return null;
6767
}
@@ -71,8 +71,8 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
7171

7272
if (string.IsNullOrEmpty(token))
7373
{
74-
Options.Logger.LogDebug("Authentication was skipped because the bearer token " +
75-
"was missing from the 'Authorization' header.");
74+
Logger.LogDebug("Authentication was skipped because the bearer token " +
75+
"was missing from the 'Authorization' header.");
7676

7777
return null;
7878
}
@@ -83,7 +83,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
8383
var ticket = await CreateTicketAsync(token);
8484
if (ticket == null)
8585
{
86-
Options.Logger.LogError("Authentication failed because the access token was invalid.");
86+
Logger.LogError("Authentication failed because the access token was invalid.");
8787

8888
Context.Set(typeof(OAuthValidationError).FullName, new OAuthValidationError
8989
{
@@ -98,8 +98,8 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
9898
// to be used with this resource server.
9999
if (!ValidateAudience(ticket))
100100
{
101-
Options.Logger.LogError("Authentication failed because the access token " +
102-
"was not valid for this resource server.");
101+
Logger.LogError("Authentication failed because the access token " +
102+
"was not valid for this resource server.");
103103

104104
Context.Set(typeof(OAuthValidationError).FullName, new OAuthValidationError
105105
{
@@ -114,7 +114,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
114114
if (ticket.Properties.ExpiresUtc.HasValue &&
115115
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow)
116116
{
117-
Options.Logger.LogError("Authentication failed because the access token was expired.");
117+
Logger.LogError("Authentication failed because the access token was expired.");
118118

119119
Context.Set(typeof(OAuthValidationError).FullName, new OAuthValidationError
120120
{
@@ -134,7 +134,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
134134
// indicate that authentication was rejected by application code.
135135
if (notification.Ticket == null)
136136
{
137-
Options.Logger.LogInformation("Authentication was stopped by application code.");
137+
Logger.LogInformation("Authentication was stopped by application code.");
138138

139139
return null;
140140
}
@@ -144,7 +144,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
144144

145145
else if (notification.Skipped)
146146
{
147-
Options.Logger.LogInformation("Authentication was skipped by application code.");
147+
Logger.LogInformation("Authentication was skipped by application code.");
148148

149149
return null;
150150
}
@@ -403,5 +403,7 @@ protected virtual async Task<AuthenticationTicket> CreateTicketAsync(string toke
403403

404404
return notification.Ticket;
405405
}
406+
407+
public ILogger Logger => Options.Logger;
406408
}
407409
}

0 commit comments

Comments
 (0)