@ -2,11 +2,15 @@
using System.IdentityModel.Tokens.Jwt ;
using System.Reflection ;
using AspNet.Security.OpenIdConnect.Primitives ;
using AspNet.Security.OpenIdConnect.Server ;
using Microsoft.AspNetCore.Builder ;
using Microsoft.AspNetCore.Builder.Internal ;
using Microsoft.AspNetCore.DataProtection ;
using Microsoft.AspNetCore.Hosting ;
using Microsoft.AspNetCore.Hosting.Internal ;
using Microsoft.AspNetCore.Http ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.DependencyInjection.Extensions ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Options ;
using Microsoft.IdentityModel.Tokens ;
using Moq ;
@ -16,199 +20,36 @@ namespace OpenIddict.Tests
{
public class OpenIddictExtensionsTests
{
[Fact]
public void UseOpenIddict_ThrowsAnExceptionWhenServicesAreNotRegistered ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
var builder = new ApplicationBuilder ( services . BuildServiceProvider ( ) ) ;
// Act and assert
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > builder . UseOpenIddict ( ) ) ;
Assert . Equal ( "The OpenIddict services cannot be resolved from the dependency injection container. " +
"Make sure 'services.AddOpenIddict()' is correctly called from 'ConfigureServices()'." , exception . Message ) ;
}
[Fact]
public void UseOpenIddict_ThrowsAnExceptionWhenNoDistributedCacheIsRegisteredIfRequestCachingIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOpenIddict ( )
. EnableRequestCaching ( ) ;
var builder = new ApplicationBuilder ( services . BuildServiceProvider ( ) ) ;
// Act and assert
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > builder . UseOpenIddict ( ) ) ;
Assert . Equal ( "A distributed cache implementation must be registered in the OpenIddict options " +
"or in the dependency injection container when enabling request caching support." , exception . Message ) ;
}
[Fact]
public void UseOpenIddict_ThrowsAnExceptionWhenNoFlowIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOpenIddict ( ) ;
var builder = new ApplicationBuilder ( services . BuildServiceProvider ( ) ) ;
// Act and assert
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > builder . UseOpenIddict ( ) ) ;
Assert . Equal ( "At least one OAuth2/OpenID Connect flow must be enabled." , exception . Message ) ;
}
[Theory]
[InlineData(OpenIdConnectConstants.GrantTypes.AuthorizationCode)]
[InlineData(OpenIdConnectConstants.GrantTypes.Implicit)]
public void UseOpenIddict_ThrowsAnExceptionWhenAuthorizationEndpointIsDisabled ( string flow )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOpenIddict ( )
. Configure ( options = > options . GrantTypes . Add ( flow ) )
. Configure ( options = > options . AuthorizationEndpointPath = PathString . Empty ) ;
var builder = new ApplicationBuilder ( services . BuildServiceProvider ( ) ) ;
// Act and assert
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > builder . UseOpenIddict ( ) ) ;
Assert . Equal ( "The authorization endpoint must be enabled to use " +
"the authorization code and implicit flows." , exception . Message ) ;
}
[Theory]
[InlineData(OpenIdConnectConstants.GrantTypes.AuthorizationCode)]
[InlineData(OpenIdConnectConstants.GrantTypes.ClientCredentials)]
[InlineData(OpenIdConnectConstants.GrantTypes.Password)]
[InlineData(OpenIdConnectConstants.GrantTypes.RefreshToken)]
public void UseOpenIddict_ThrowsAnExceptionWhenTokenEndpointIsDisabled ( string flow )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOpenIddict ( )
. EnableAuthorizationEndpoint ( "/connect/authorize" )
. Configure ( options = > options . GrantTypes . Add ( flow ) )
. Configure ( options = > options . TokenEndpointPath = PathString . Empty ) ;
var builder = new ApplicationBuilder ( services . BuildServiceProvider ( ) ) ;
// Act and assert
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > builder . UseOpenIddict ( ) ) ;
Assert . Equal ( "The token endpoint must be enabled to use the authorization code, " +
"client credentials, password and refresh token flows." , exception . Message ) ;
}
[Fact]
public void UseOpenIddict_ThrowsAnExceptionWhenTokenRevocationIsDisabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOpenIddict ( )
. EnableAuthorizationEndpoint ( "/connect/authorize" )
. EnableRevocationEndpoint ( "/connect/revocation" )
. AllowImplicitFlow ( )
. DisableTokenRevocation ( ) ;
var builder = new ApplicationBuilder ( services . BuildServiceProvider ( ) ) ;
// Act and assert
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > builder . UseOpenIddict ( ) ) ;
Assert . Equal ( "The revocation endpoint cannot be enabled when token revocation is disabled." , exception . Message ) ;
}
[Fact]
public void UseOpenIddict_ThrowsAnExceptionWhenNoSigningKeyIsRegisteredIfTheImplicitFlowIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOpenIddict ( )
. EnableAuthorizationEndpoint ( "/connect/authorize" )
. AllowImplicitFlow ( ) ;
var builder = new ApplicationBuilder ( services . BuildServiceProvider ( ) ) ;
// Act and assert
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > builder . UseOpenIddict ( ) ) ;
Assert . Equal ( "At least one asymmetric signing key must be registered when enabling the implicit flow. " +
"Consider registering a X.509 certificate using 'services.AddOpenIddict().AddSigningCertificate()' " +
"or call 'services.AddOpenIddict().AddEphemeralSigningKey()' to use an ephemeral key." , exception . Message ) ;
}
[Fact]
public void Configure_OptionsAreCorrectlyAmended ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . Configure ( configuration = > configuration . Description . DisplayName = "OpenIddict" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
// Assert
Assert . Equal ( "OpenIddict" , options . Value . Description . DisplayName ) ;
}
[Fact]
public void UseOpenIddict_OpenIdConnectServerMiddlewareIsRegistered ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
builder . Configure ( configuration = > configuration . AccessTokenLifetime = TimeSpan . FromDays ( 1 ) ) ;
services . AddOpenIddict ( )
. AddSigningCertificate (
assembly : typeof ( OpenIddictProviderTests ) . GetTypeInfo ( ) . Assembly ,
resource : "OpenIddict.Tests.Certificate.pfx" ,
password : "OpenIddict" )
. AllowImplicitFlow ( )
. EnableAuthorizationEndpoint ( "/connect/authorize" ) ;
var builder = new Mock < IApplicationBuilder > ( ) ;
builder . SetupGet ( mock = > mock . ApplicationServices )
. Returns ( services . BuildServiceProvider ( ) ) ;
// Act
builder . Object . UseOpenIddict ( ) ;
var options = GetOptions ( services ) ;
// Assert
builder . Verify ( mock = > mock . Use ( It . IsAny < Func < RequestDelegate , RequestDelegate > > ( ) ) , Times . Once ( ) ) ;
Assert . Equal ( TimeSpan . FromDays ( 1 ) , options . AccessTokenLifetime ) ;
}
[Fact]
public void AddEphemeralSigningKey_SigningKeyIsCorrectlyAdded ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . AddEphemeralSigningKey ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( 1 , options . Value . SigningCredentials . Count ) ;
Assert . Equal ( 1 , options . SigningCredentials . Count ) ;
}
[Theory]
@ -223,17 +64,14 @@ namespace OpenIddict.Tests
public void AddEphemeralSigningKey_SigningCredentialsUseSpecifiedAlgorithm ( string algorithm )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . AddEphemeralSigningKey ( algorithm ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var credentials = options . Value . SigningCredentials [ 0 ] ;
var options = GetOptions ( services ) ;
var credentials = options . SigningCredentials [ 0 ] ;
// Assert
Assert . Equal ( algorithm , credentials . Algorithm ) ;
@ -250,9 +88,7 @@ namespace OpenIddict.Tests
public void AddSigningKey_SigningKeyIsCorrectlyAdded ( string algorithm )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
var factory = Mock . Of < CryptoProviderFactory > ( mock = >
@ -263,20 +99,17 @@ namespace OpenIddict.Tests
// Act
builder . AddSigningKey ( key ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Same ( key , options . Value . SigningCredentials [ 0 ] . Key ) ;
Assert . Same ( key , options . SigningCredentials [ 0 ] . Key ) ;
}
[Fact]
public void AddSigningCertificate_SigningKeyIsCorrectlyAdded ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
@ -285,486 +118,432 @@ namespace OpenIddict.Tests
resource : "OpenIddict.Tests.Certificate.pfx" ,
password : "OpenIddict" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . IsType ( typeof ( X509SecurityKey ) , options . Value . SigningCredentials [ 0 ] . Key ) ;
Assert . IsType < X509SecurityKey > ( options . SigningCredentials [ 0 ] . Key ) ;
}
[Fact]
public void AllowAuthorizationCodeFlow_CodeFlowIsAddedToGrantTypes ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . AllowAuthorizationCodeFlow ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Contains ( OpenIdConnectConstants . GrantTypes . AuthorizationCode , options . Value . GrantTypes ) ;
Assert . Contains ( OpenIdConnectConstants . GrantTypes . AuthorizationCode , options . GrantTypes ) ;
}
[Fact]
public void AllowClientCredentialsFlow_ClientCredentialsFlowIsAddedToGrantTypes ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . AllowClientCredentialsFlow ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Contains ( OpenIdConnectConstants . GrantTypes . ClientCredentials , options . Value . GrantTypes ) ;
Assert . Contains ( OpenIdConnectConstants . GrantTypes . ClientCredentials , options . GrantTypes ) ;
}
[Fact]
public void AllowCustomFlow_CustomFlowIsAddedToGrantTypes ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . AllowCustomFlow ( "urn:ietf:params:oauth:grant-type:custom_grant" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Contains ( "urn:ietf:params:oauth:grant-type:custom_grant" , options . Value . GrantTypes ) ;
Assert . Contains ( "urn:ietf:params:oauth:grant-type:custom_grant" , options . GrantTypes ) ;
}
[Fact]
public void AllowImplicitFlow_ImplicitFlowIsAddedToGrantTypes ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . AllowImplicitFlow ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Contains ( OpenIdConnectConstants . GrantTypes . Implicit , options . Value . GrantTypes ) ;
Assert . Contains ( OpenIdConnectConstants . GrantTypes . Implicit , options . GrantTypes ) ;
}
[Fact]
public void AllowPasswordFlow_PasswordFlowIsAddedToGrantTypes ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . AllowPasswordFlow ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Contains ( OpenIdConnectConstants . GrantTypes . Password , options . Value . GrantTypes ) ;
Assert . Contains ( OpenIdConnectConstants . GrantTypes . Password , options . GrantTypes ) ;
}
[Fact]
public void AllowRefreshTokenFlow_RefreshTokenFlowIsAddedToGrantTypes ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . AllowRefreshTokenFlow ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Contains ( OpenIdConnectConstants . GrantTypes . RefreshToken , options . Value . GrantTypes ) ;
Assert . Contains ( OpenIdConnectConstants . GrantTypes . RefreshToken , options . GrantTypes ) ;
}
[Fact]
public void DisableConfigurationEndpoint_ConfigurationEndpointIsDisabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . DisableConfigurationEndpoint ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( PathString . Empty , options . Value . ConfigurationEndpointPath ) ;
Assert . Equal ( PathString . Empty , options . ConfigurationEndpointPath ) ;
}
[Fact]
public void DisableCryptographyEndpoint_CryptographyEndpointIsDisabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . DisableCryptographyEndpoint ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( PathString . Empty , options . Value . CryptographyEndpointPath ) ;
Assert . Equal ( PathString . Empty , options . CryptographyEndpointPath ) ;
}
[Fact]
public void DisableSlidingExpiration_SlidingExpirationIsDisabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . DisableSlidingExpiration ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . False ( options . Value . UseSlidingExpiration ) ;
Assert . False ( options . UseSlidingExpiration ) ;
}
[Fact]
public void DisableTokenRevocation_TokenRevocationIsDisabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . DisableTokenRevocation ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . True ( options . Value . DisableTokenRevocation ) ;
Assert . True ( options . DisableTokenRevocation ) ;
}
[Fact]
public void EnableAuthorizationEndpoint_AuthorizationEndpointIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . EnableAuthorizationEndpoint ( "/endpoint-path" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( "/endpoint-path" , options . Value . AuthorizationEndpointPath ) ;
Assert . Equal ( "/endpoint-path" , options . AuthorizationEndpointPath ) ;
}
[Fact]
public void EnableIntrospectionEndpoint_IntrospectionEndpointIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . EnableIntrospectionEndpoint ( "/endpoint-path" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( "/endpoint-path" , options . Value . IntrospectionEndpointPath ) ;
Assert . Equal ( "/endpoint-path" , options . IntrospectionEndpointPath ) ;
}
[Fact]
public void EnableLogoutEndpoint_LogoutEndpointIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . EnableLogoutEndpoint ( "/endpoint-path" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( "/endpoint-path" , options . Value . LogoutEndpointPath ) ;
Assert . Equal ( "/endpoint-path" , options . LogoutEndpointPath ) ;
}
[Fact]
public void EnableRequestCaching_RequestCachingIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . EnableRequestCaching ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . True ( options . Value . EnableRequestCaching ) ;
Assert . True ( options . EnableRequestCaching ) ;
}
[Fact]
public void EnableRevocationEndpoint_RevocationEndpointIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . EnableRevocationEndpoint ( "/endpoint-path" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( "/endpoint-path" , options . Value . RevocationEndpointPath ) ;
Assert . Equal ( "/endpoint-path" , options . RevocationEndpointPath ) ;
}
[Fact]
public void EnableTokenEndpoint_TokenEndpointIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . EnableTokenEndpoint ( "/endpoint-path" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( "/endpoint-path" , options . Value . TokenEndpointPath ) ;
Assert . Equal ( "/endpoint-path" , options . TokenEndpointPath ) ;
}
[Fact]
public void EnableUserinfoEndpoint_UserinfoEndpointIsEnabled ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . EnableUserinfoEndpoint ( "/endpoint-path" ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( "/endpoint-path" , options . Value . UserinfoEndpointPath ) ;
Assert . Equal ( "/endpoint-path" , options . UserinfoEndpointPath ) ;
}
[Fact]
public void RequireClientIdentification_ClientIdentificationIsEnforced ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . RequireClientIdentification ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . True ( options . Value . RequireClientIdentification ) ;
Assert . True ( options . RequireClientIdentification ) ;
}
[Fact]
public void SetAccessTokenLifetime_DefaultAccessTokenLifetimeIsReplaced ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . SetAccessTokenLifetime ( TimeSpan . FromMinutes ( 4 2 ) ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( TimeSpan . FromMinutes ( 4 2 ) , options . Value . AccessTokenLifetime ) ;
Assert . Equal ( TimeSpan . FromMinutes ( 4 2 ) , options . AccessTokenLifetime ) ;
}
[Fact]
public void SetAuthorizationCodeLifetime_DefaultAuthorizationCodeLifetimeIsReplaced ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . SetAuthorizationCodeLifetime ( TimeSpan . FromMinutes ( 4 2 ) ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( TimeSpan . FromMinutes ( 4 2 ) , options . Value . AuthorizationCodeLifetime ) ;
Assert . Equal ( TimeSpan . FromMinutes ( 4 2 ) , options . AuthorizationCodeLifetime ) ;
}
[Fact]
public void SetIdentityTokenLifetime_DefaultIdentityTokenLifetimeIsReplaced ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . SetIdentityTokenLifetime ( TimeSpan . FromMinutes ( 4 2 ) ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( TimeSpan . FromMinutes ( 4 2 ) , options . Value . IdentityTokenLifetime ) ;
Assert . Equal ( TimeSpan . FromMinutes ( 4 2 ) , options . IdentityTokenLifetime ) ;
}
[Fact]
public void SetRefreshTokenLifetime_DefaultRefreshTokenLifetimeIsReplaced ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . SetRefreshTokenLifetime ( TimeSpan . FromMinutes ( 4 2 ) ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( TimeSpan . FromMinutes ( 4 2 ) , options . Value . RefreshTokenLifetime ) ;
Assert . Equal ( TimeSpan . FromMinutes ( 4 2 ) , options . RefreshTokenLifetime ) ;
}
[Fact]
public void SetIssuer_AddressIsReplaced ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . SetIssuer ( new Uri ( "http://www.fabrikam.com/" ) ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . Equal ( new Uri ( "http://www.fabrikam.com/" ) , options . Value . Issuer ) ;
Assert . Equal ( new Uri ( "http://www.fabrikam.com/" ) , options . Issuer ) ;
}
[Fact]
public void UseDataProtectionProvider_DefaultProviderIsReplaced ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . UseDataProtectionProvider ( new EphemeralDataProtectionProvider ( ) ) ;
builder . UseDataProtectionProvider ( new EphemeralDataProtectionProvider ( new LoggerFactory ( ) ) ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . IsType ( typeof ( EphemeralDataProtectionProvider ) , options . Value . DataProtectionProvider ) ;
Assert . IsType < EphemeralDataProtectionProvider > ( options . DataProtectionProvider ) ;
}
[Fact]
public void UseJsonWebTokens_AccessTokenHandlerIsCorrectlySet ( )
{
// Arrange
var services = new ServiceCollection ( ) ;
services . AddOptions ( ) ;
var services = CreateServices ( ) ;
var builder = new OpenIddictBuilder ( services ) ;
// Act
builder . UseJsonWebTokens ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptions < OpenIddictOptions > > ( ) ;
var options = GetOptions ( services ) ;
// Assert
Assert . IsType ( typeof ( JwtSecurityTokenHandler ) , options . Value . AccessTokenHandler ) ;
Assert . IsType < JwtSecurityTokenHandler > ( options . AccessTokenHandler ) ;
}
private static IServiceCollection CreateServices ( )
{
var services = new ServiceCollection ( ) ;
services . AddAuthentication ( ) ;
services . AddDistributedMemoryCache ( ) ;
services . AddLogging ( ) ;
services . AddSingleton < IHostingEnvironment , HostingEnvironment > ( ) ;
return services ;
}
private static OpenIddictOptions GetOptions ( IServiceCollection services )
{
services . RemoveAll < IPostConfigureOptions < OpenIdConnectServerOptions > > ( ) ;
services . RemoveAll < IPostConfigureOptions < OpenIddictOptions > > ( ) ;
var provider = services . BuildServiceProvider ( ) ;
var options = provider . GetRequiredService < IOptionsSnapshot < OpenIddictOptions > > ( ) ;
return options . Get ( OpenIdConnectServerDefaults . AuthenticationScheme ) ;
}
}
}