Permalink
Browse files

Fix the deserialization methods to avoid throwing an exception when H…

…andleResponse() is called with a null authentication ticket
  • Loading branch information...
PinpointTownes committed Sep 22, 2017
1 parent a499f11 commit c734c6f09952f89906398fd6d0e0d7c311084e31
@@ -542,7 +542,7 @@ private async Task<AuthenticationTicket> DeserializeAuthorizationCodeAsync(strin
if (notification.HandledResponse || notification.Ticket != null)
{
notification.Ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AuthorizationCode);
notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AuthorizationCode);
return notification.Ticket;
}
@@ -601,7 +601,7 @@ private async Task<AuthenticationTicket> DeserializeAccessTokenAsync(string toke
if (notification.HandledResponse || notification.Ticket != null)
{
notification.Ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AccessToken);
notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AccessToken);
return notification.Ticket;
}
@@ -718,7 +718,7 @@ private async Task<AuthenticationTicket> DeserializeIdentityTokenAsync(string to
if (notification.HandledResponse || notification.Ticket != null)
{
notification.Ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.IdToken);
notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.IdToken);
return notification.Ticket;
}
@@ -797,7 +797,7 @@ private async Task<AuthenticationTicket> DeserializeRefreshTokenAsync(string tok
if (notification.HandledResponse || notification.Ticket != null)
{
notification.Ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.RefreshToken);
notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.RefreshToken);
return notification.Ticket;
}
@@ -522,7 +522,7 @@ private async Task<AuthenticationTicket> DeserializeAuthorizationCodeAsync(strin
if (notification.HandledResponse || notification.Ticket != null)
{
notification.Ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AuthorizationCode);
notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AuthorizationCode);
return notification.Ticket;
}
@@ -581,7 +581,7 @@ private async Task<AuthenticationTicket> DeserializeAccessTokenAsync(string toke
if (notification.HandledResponse || notification.Ticket != null)
{
notification.Ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AccessToken);
notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AccessToken);
return notification.Ticket;
}
@@ -698,7 +698,7 @@ private async Task<AuthenticationTicket> DeserializeIdentityTokenAsync(string to
if (notification.HandledResponse || notification.Ticket != null)
{
notification.Ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.IdToken);
notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.IdToken);
return notification.Ticket;
}
@@ -777,7 +777,7 @@ private async Task<AuthenticationTicket> DeserializeRefreshTokenAsync(string tok
if (notification.HandledResponse || notification.Ticket != null)
{
notification.Ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.RefreshToken);
notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.RefreshToken);
return notification.Ticket;
}
@@ -2480,7 +2480,7 @@ public async Task SerializeRefreshTokenAsync_UsesRefreshTokenFormat()
}
[Fact]
public async Task DeserializeAuthorizationCodeAsync_AllowsHandlingSerialization()
public async Task DeserializeAuthorizationCodeAsync_AllowsHandlingDeserialization()
{
// Arrange
var server = CreateAuthorizationServer(options =>
@@ -2522,7 +2522,45 @@ public async Task DeserializeAuthorizationCodeAsync_AllowsHandlingSerialization(
}
[Fact]
public async Task DeserializeAuthorizationCodeAsync_AllowsSkippingSerialization()
public async Task DeserializeAuthorizationCodeAsync_AllowsReturningNullTicket()
{
// Arrange
var server = CreateAuthorizationServer(options =>
{
options.Provider.OnDeserializeAuthorizationCode = context =>
{
// Assert
Assert.Equal("authorization_code", context.AuthorizationCode);
context.Ticket = null;
context.HandleResponse();
return Task.FromResult(0);
};
options.Provider.OnValidateIntrospectionRequest = context =>
{
context.Skip();
return Task.FromResult(0);
};
});
var client = new OpenIdConnectClient(server.CreateClient());
// Act
var response = await client.PostAsync(IntrospectionEndpoint, new OpenIdConnectRequest
{
Token = "authorization_code",
TokenTypeHint = OpenIdConnectConstants.TokenTypeHints.AuthorizationCode
});
// Assert
Assert.False((bool) response[OpenIdConnectConstants.Claims.Active]);
}
[Fact]
public async Task DeserializeAuthorizationCodeAsync_AllowsSkippingDeserialization()
{
// Arrange
var server = CreateAuthorizationServer(options =>
@@ -2635,7 +2673,7 @@ public async Task DeserializeAuthorizationCodeAsync_UsesAuthorizationCodeFormat(
}
[Fact]
public async Task DeserializeAccessTokenAsync_AllowsHandlingSerialization()
public async Task DeserializeAccessTokenAsync_AllowsHandlingDeserialization()
{
// Arrange
var server = CreateAuthorizationServer(options =>
@@ -2677,7 +2715,45 @@ public async Task DeserializeAccessTokenAsync_AllowsHandlingSerialization()
}
[Fact]
public async Task DeserializeAccessTokenAsync_AllowsSkippingSerialization()
public async Task DeserializeAccessTokenAsync_AllowsReturningNullTicket()
{
// Arrange
var server = CreateAuthorizationServer(options =>
{
options.Provider.OnDeserializeAccessToken = context =>
{
// Assert
Assert.Equal("access_token", context.AccessToken);
context.Ticket = null;
context.HandleResponse();
return Task.FromResult(0);
};
options.Provider.OnValidateIntrospectionRequest = context =>
{
context.Skip();
return Task.FromResult(0);
};
});
var client = new OpenIdConnectClient(server.CreateClient());
// Act
var response = await client.PostAsync(IntrospectionEndpoint, new OpenIdConnectRequest
{
Token = "access_token",
TokenTypeHint = OpenIdConnectConstants.TokenTypeHints.AccessToken
});
// Assert
Assert.False((bool) response[OpenIdConnectConstants.Claims.Active]);
}
[Fact]
public async Task DeserializeAccessTokenAsync_AllowsSkippingDeserialization()
{
// Arrange
var server = CreateAuthorizationServer(options =>
@@ -2893,7 +2969,7 @@ public async Task DeserializeAccessTokenAsync_ReturnsNullForInvalidTokenType()
}
[Fact]
public async Task DeserializeIdentityTokenAsync_AllowsHandlingSerialization()
public async Task DeserializeIdentityTokenAsync_AllowsHandlingDeserialization()
{
// Arrange
var server = CreateAuthorizationServer(options =>
@@ -2935,7 +3011,45 @@ public async Task DeserializeIdentityTokenAsync_AllowsHandlingSerialization()
}
[Fact]
public async Task DeserializeIdentityTokenAsync_AllowsSkippingSerialization()
public async Task DeserializeIdentityTokenAsync_AllowsReturningNullTicket()
{
// Arrange
var server = CreateAuthorizationServer(options =>
{
options.Provider.OnDeserializeIdentityToken = context =>
{
// Assert
Assert.Equal("id_token", context.IdentityToken);
context.Ticket = null;
context.HandleResponse();
return Task.FromResult(0);
};
options.Provider.OnValidateIntrospectionRequest = context =>
{
context.Skip();
return Task.FromResult(0);
};
});
var client = new OpenIdConnectClient(server.CreateClient());
// Act
var response = await client.PostAsync(IntrospectionEndpoint, new OpenIdConnectRequest
{
Token = "id_token",
TokenTypeHint = OpenIdConnectConstants.TokenTypeHints.IdToken
});
// Assert
Assert.False((bool) response[OpenIdConnectConstants.Claims.Active]);
}
[Fact]
public async Task DeserializeIdentityTokenAsync_AllowsSkippingDeserialization()
{
// Arrange
var server = CreateAuthorizationServer(options =>
@@ -3116,7 +3230,7 @@ public async Task DeserializeIdentityTokenAsync_ReturnsNullForInvalidTokenType()
}
[Fact]
public async Task DeserializeRefreshTokenAsync_AllowsHandlingSerialization()
public async Task DeserializeRefreshTokenAsync_AllowsHandlingDeserialization()
{
// Arrange
var server = CreateAuthorizationServer(options =>
@@ -3158,7 +3272,45 @@ public async Task DeserializeRefreshTokenAsync_AllowsHandlingSerialization()
}
[Fact]
public async Task DeserializeRefreshTokenAsync_AllowsSkippingSerialization()
public async Task DeserializeRefreshTokenAsync_AllowsReturningNullTicket()
{
// Arrange
var server = CreateAuthorizationServer(options =>
{
options.Provider.OnDeserializeRefreshToken = context =>
{
// Assert
Assert.Equal("refresh_token", context.RefreshToken);
context.Ticket = null;
context.HandleResponse();
return Task.FromResult(0);
};
options.Provider.OnValidateIntrospectionRequest = context =>
{
context.Skip();
return Task.FromResult(0);
};
});
var client = new OpenIdConnectClient(server.CreateClient());
// Act
var response = await client.PostAsync(IntrospectionEndpoint, new OpenIdConnectRequest
{
Token = "refresh_token",
TokenTypeHint = OpenIdConnectConstants.TokenTypeHints.RefreshToken
});
// Assert
Assert.False((bool) response[OpenIdConnectConstants.Claims.Active]);
}
[Fact]
public async Task DeserializeRefreshTokenAsync_AllowsSkippingDeserialization()
{
// Arrange
var server = CreateAuthorizationServer(options =>
Oops, something went wrong.

0 comments on commit c734c6f

Please sign in to comment.