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

Commit 24f4a3a

Browse files
committed
Update the OWIN/Katana projects to use the new logging stack and unify the log levels
1 parent af5c754 commit 24f4a3a

File tree

10 files changed

+61
-43
lines changed

10 files changed

+61
-43
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ public class OAuthIntrospectionHandler : AuthenticationHandler<OAuthIntrospectio
2525
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
2626
string header = Request.Headers[HeaderNames.Authorization];
2727
if (string.IsNullOrEmpty(header)) {
28-
return AuthenticateResult.Fail("Authentication failed because the bearer token " +
29-
"was missing from the 'Authorization' header.");
28+
Logger.LogInformation("Authentication was skipped because no bearer token was received.");
29+
30+
return AuthenticateResult.Skip();
3031
}
3132

3233
// Ensure that the authorization header contains the mandatory "Bearer" scheme.
3334
// See https://tools.ietf.org/html/rfc6750#section-2.1
3435
if (!header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) {
35-
return AuthenticateResult.Fail("Authentication failed because an invalid scheme " +
36-
"was used in the 'Authorization' header.");
36+
Logger.LogInformation("Authentication was skipped because an incompatible " +
37+
"scheme was used in the 'Authorization' header.");
38+
39+
return AuthenticateResult.Skip();
3740
}
3841

3942
var token = header.Substring("Bearer ".Length);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ public class OAuthValidationHandler : AuthenticationHandler<OAuthValidationOptio
1616
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
1717
string header = Request.Headers[HeaderNames.Authorization];
1818
if (string.IsNullOrEmpty(header)) {
19-
Logger.LogDebug("Authentication was skipped because no bearer token was received.");
19+
Logger.LogInformation("Authentication was skipped because no bearer token was received.");
2020

2121
return AuthenticateResult.Skip();
2222
}
2323

2424
// Ensure that the authorization header contains the mandatory "Bearer" scheme.
2525
// See https://tools.ietf.org/html/rfc6750#section-2.1
2626
if (!header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) {
27-
return AuthenticateResult.Fail("Authentication failed because an invalid scheme " +
28-
"was used in the 'Authorization' header.");
27+
Logger.LogInformation("Authentication was skipped because an incompatible " +
28+
"scheme was used in the 'Authorization' header.");
29+
30+
return AuthenticateResult.Skip();
2931
}
3032

3133
var token = header.Substring("Bearer ".Length);

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
using System.Text;
1515
using System.Threading.Tasks;
1616
using Microsoft.Extensions.Caching.Distributed;
17-
using Microsoft.Owin.Logging;
17+
using Microsoft.Extensions.Logging;
1818
using Microsoft.Owin.Security;
1919
using Microsoft.Owin.Security.Infrastructure;
2020
using Newtonsoft.Json.Linq;
@@ -24,25 +24,24 @@ public class OAuthIntrospectionHandler : AuthenticationHandler<OAuthIntrospectio
2424
protected override async Task<AuthenticationTicket> AuthenticateCoreAsync() {
2525
var header = Request.Headers.Get("Authorization");
2626
if (string.IsNullOrEmpty(header)) {
27-
Options.Logger.WriteError("Authentication failed because the bearer token " +
28-
"was missing from the 'Authorization' header.");
27+
Options.Logger.LogInformation("Authentication was skipped because no bearer token was received.");
2928

3029
return null;
3130
}
3231

3332
// Ensure that the authorization header contains the mandatory "Bearer" scheme.
3433
// See https://tools.ietf.org/html/rfc6750#section-2.1
3534
if (!header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) {
36-
Options.Logger.WriteError("Authentication failed because an invalid scheme " +
37-
"was used in the 'Authorization' header.");
35+
Options.Logger.LogInformation("Authentication was skipped because an incompatible " +
36+
"scheme was used in the 'Authorization' header.");
3837

3938
return null;
4039
}
4140

4241
var token = header.Substring("Bearer ".Length);
4342
if (string.IsNullOrWhiteSpace(token)) {
44-
Options.Logger.WriteError("Authentication failed because the bearer token " +
45-
"was missing from the 'Authorization' header.");
43+
Options.Logger.LogError("Authentication failed because the bearer token " +
44+
"was missing from the 'Authorization' header.");
4645

4746
return null;
4847
}
@@ -55,17 +54,17 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync() {
5554
// request failed or if the "active" claim was false.
5655
var payload = await GetIntrospectionPayloadAsync(token);
5756
if (payload == null || !payload.Value<bool>(OAuthIntrospectionConstants.Claims.Active)) {
58-
Options.Logger.WriteError("Authentication failed because the authorization " +
59-
"server rejected the access token.");
57+
Options.Logger.LogError("Authentication failed because the authorization " +
58+
"server rejected the access token.");
6059

6160
return null;
6261
}
6362

6463
// Ensure that the access token was issued
6564
// to be used with this resource server.
6665
if (!await ValidateAudienceAsync(payload)) {
67-
Options.Logger.WriteError("Authentication failed because the access token " +
68-
"was not valid for this resource server.");
66+
Options.Logger.LogError("Authentication failed because the access token " +
67+
"was not valid for this resource server.");
6968

7069
return null;
7170
}
@@ -81,7 +80,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync() {
8180
// Ensure that the authentication ticket is still valid.
8281
if (ticket.Properties.ExpiresUtc.HasValue &&
8382
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow) {
84-
Options.Logger.WriteError("Authentication failed because the access token was expired.");
83+
Options.Logger.LogError("Authentication failed because the access token was expired.");
8584

8685
return null;
8786
}
@@ -100,9 +99,11 @@ protected virtual async Task<string> ResolveIntrospectionEndpointAsync(string is
10099

101100
var response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Request.CallCancelled);
102101
if (!response.IsSuccessStatusCode) {
103-
Options.Logger.WriteError("An error occurred when retrieving the issuer metadata: the remote server " +
104-
$"returned a {response.StatusCode} response with the following payload: " +
105-
$"{response.Headers.ToString()} {await response.Content.ReadAsStringAsync()}.");
102+
Options.Logger.LogError("An error occurred when retrieving the issuer metadata: the remote server " +
103+
"returned a {Status} response with the following payload: {Headers} {Body}.",
104+
/* Status: */ response.StatusCode,
105+
/* Headers: */ response.Headers.ToString(),
106+
/* Body: */ await response.Content.ReadAsStringAsync());
106107

107108
return null;
108109
}
@@ -143,9 +144,11 @@ protected virtual async Task<JObject> GetIntrospectionPayloadAsync(string token)
143144

144145
var response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Request.CallCancelled);
145146
if (!response.IsSuccessStatusCode) {
146-
Options.Logger.WriteError("An error occurred when validating an access token: the remote server " +
147-
$"returned a {response.StatusCode} response with the following payload: " +
148-
$"{response.Headers.ToString()} {await response.Content.ReadAsStringAsync()}.");
147+
Options.Logger.LogError("An error occurred when validating an access token: the remote server " +
148+
"returned a {Status} response with the following payload: {Headers} {Body}.",
149+
/* Status: */ response.StatusCode,
150+
/* Headers: */ response.Headers.ToString(),
151+
/* Body: */ await response.Content.ReadAsStringAsync());
149152

150153
return null;
151154
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66

77
using System;
88
using System.Net.Http;
9+
using JetBrains.Annotations;
910
using Microsoft.Extensions.Caching.Distributed;
1011
using Microsoft.Extensions.Caching.Memory;
12+
using Microsoft.Extensions.Logging;
1113
using Microsoft.Owin;
12-
using Microsoft.Owin.Logging;
1314
using Microsoft.Owin.Security.Infrastructure;
1415

1516
namespace Owin.Security.OAuth.Introspection {
1617
public class OAuthIntrospectionMiddleware : AuthenticationMiddleware<OAuthIntrospectionOptions> {
17-
public OAuthIntrospectionMiddleware(OwinMiddleware next, IAppBuilder app, OAuthIntrospectionOptions options)
18+
public OAuthIntrospectionMiddleware(
19+
[NotNull] OwinMiddleware next,
20+
[NotNull] IAppBuilder app,
21+
[NotNull] OAuthIntrospectionOptions options)
1822
: base(next, options) {
1923
if (string.IsNullOrEmpty(options.Authority) &&
2024
string.IsNullOrEmpty(options.IntrospectionEndpoint)) {
@@ -33,7 +37,7 @@ public OAuthIntrospectionMiddleware(OwinMiddleware next, IAppBuilder app, OAuthI
3337
}
3438

3539
if (options.Logger == null) {
36-
options.Logger = app.CreateLogger<OAuthIntrospectionMiddleware>();
40+
options.Logger = new LoggerFactory().CreateLogger<OAuthIntrospectionMiddleware>();
3741
}
3842

3943
if (options.HttpClient == null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using System.Collections.Generic;
88
using System.Net.Http;
99
using Microsoft.Extensions.Caching.Distributed;
10+
using Microsoft.Extensions.Logging;
1011
using Microsoft.Owin.Infrastructure;
11-
using Microsoft.Owin.Logging;
1212
using Microsoft.Owin.Security;
1313
using Microsoft.Owin.Security.DataHandler.Serializer;
1414

src/Owin.Security.OAuth.Introspection/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dependencies": {
3131
"JetBrains.Annotations": { "type": "build", "version": "10.1.2-eap" },
3232
"Microsoft.Extensions.Caching.Memory": "1.0.0-*",
33+
"Microsoft.Extensions.Logging": "1.0.0-*",
3334
"Microsoft.Owin.Security": "3.0.1",
3435
"Newtonsoft.Json": "8.0.3"
3536
},

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System;
88
using System.Linq;
99
using System.Threading.Tasks;
10-
using Microsoft.Owin.Logging;
10+
using Microsoft.Extensions.Logging;
1111
using Microsoft.Owin.Security;
1212
using Microsoft.Owin.Security.Infrastructure;
1313

@@ -16,24 +16,24 @@ public class OAuthValidationHandler : AuthenticationHandler<OAuthValidationOptio
1616
protected override async Task<AuthenticationTicket> AuthenticateCoreAsync() {
1717
var header = Request.Headers.Get("Authorization");
1818
if (string.IsNullOrEmpty(header)) {
19-
Options.Logger.WriteVerbose("Authentication was skipped because no bearer token was received.");
19+
Options.Logger.LogInformation("Authentication was skipped because no bearer token was received.");
2020

2121
return null;
2222
}
2323

2424
// Ensure that the authorization header contains the mandatory "Bearer" scheme.
2525
// See https://tools.ietf.org/html/rfc6750#section-2.1
2626
if (!header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) {
27-
Options.Logger.WriteWarning("Authentication failed because an invalid scheme " +
28-
"was used in the 'Authorization' header.");
27+
Options.Logger.LogInformation("Authentication was skipped because an incompatible " +
28+
"scheme was used in the 'Authorization' header.");
2929

3030
return null;
3131
}
3232

3333
var token = header.Substring("Bearer ".Length);
3434
if (string.IsNullOrWhiteSpace(token)) {
35-
Options.Logger.WriteWarning("Authentication failed because the bearer token " +
36-
"was missing from the 'Authorization' header.");
35+
Options.Logger.LogError("Authentication failed because the bearer token " +
36+
"was missing from the 'Authorization' header.");
3737

3838
return null;
3939
}
@@ -42,24 +42,24 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync() {
4242
// if the ticket can't be decrypted or validated.
4343
var ticket = Options.AccessTokenFormat.Unprotect(token);
4444
if (ticket == null) {
45-
Options.Logger.WriteWarning("Authentication failed because the access token was invalid.");
45+
Options.Logger.LogError("Authentication failed because the access token was invalid.");
4646

4747
return null;
4848
}
4949

5050
// Ensure that the access token was issued
5151
// to be used with this resource server.
5252
if (!await ValidateAudienceAsync(ticket)) {
53-
Options.Logger.WriteWarning("Authentication failed because the access token " +
54-
"was not valid for this resource server.");
53+
Options.Logger.LogError("Authentication failed because the access token " +
54+
"was not valid for this resource server.");
5555

5656
return null;
5757
}
5858

5959
// Ensure that the authentication ticket is still valid.
6060
if (ticket.Properties.ExpiresUtc.HasValue &&
6161
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow) {
62-
Options.Logger.WriteWarning("Authentication failed because the access token was expired.");
62+
Options.Logger.LogError("Authentication failed because the access token was expired.");
6363

6464
return null;
6565
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
*/
66

77
using System;
8+
using JetBrains.Annotations;
89
using Microsoft.AspNetCore.DataProtection;
910
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Logging;
1012
using Microsoft.Owin;
1113
using Microsoft.Owin.BuilderProperties;
12-
using Microsoft.Owin.Logging;
1314
using Microsoft.Owin.Security.Infrastructure;
1415
using Microsoft.Owin.Security.Interop;
1516

1617
namespace Owin.Security.OAuth.Validation {
1718
public class OAuthValidationMiddleware : AuthenticationMiddleware<OAuthValidationOptions> {
18-
public OAuthValidationMiddleware(OwinMiddleware next, IAppBuilder app, OAuthValidationOptions options)
19+
public OAuthValidationMiddleware(
20+
[NotNull] OwinMiddleware next,
21+
[NotNull] IAppBuilder app,
22+
[NotNull] OAuthValidationOptions options)
1923
: base(next, options) {
2024
if (options.DataProtectionProvider == null) {
2125
// Create a new DI container and register
@@ -51,7 +55,7 @@ public OAuthValidationMiddleware(OwinMiddleware next, IAppBuilder app, OAuthVali
5155
}
5256

5357
if (options.Logger == null) {
54-
options.Logger = app.CreateLogger<OAuthValidationMiddleware>();
58+
options.Logger = new LoggerFactory().CreateLogger<OAuthValidationMiddleware>();
5559
}
5660
}
5761

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
using System.Collections.Generic;
88
using Microsoft.AspNetCore.DataProtection;
9+
using Microsoft.Extensions.Logging;
910
using Microsoft.Owin.Infrastructure;
10-
using Microsoft.Owin.Logging;
1111
using Microsoft.Owin.Security;
1212

1313
namespace Owin.Security.OAuth.Validation {

src/Owin.Security.OAuth.Validation/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
"dependencies": {
3131
"JetBrains.Annotations": { "type": "build", "version": "10.1.2-eap" },
32+
"Microsoft.Extensions.Logging": "1.0.0-*",
3233
"Microsoft.Owin.Security.Interop": "1.0.0-*"
3334
},
3435

0 commit comments

Comments
 (0)