public async Task DontCallAProviderIfNotProviderIsRegistered() { // Arrange System.Environment.SetEnvironmentVariable("APPSETTING_WEBSITE_AUTH_ENABLED", "True"); System.Environment.SetEnvironmentVariable("APPSETTING_WEBSITE_AUTH_UNAUTHENTICATED_ACTION", "RedirectToLoginPage"); var configBuilder = new ConfigurationBuilder(); configBuilder.AddEnvironmentVariables(); var config = configBuilder.Build(); var options = new EasyAuthAuthenticationOptions(); options.AddProviderOptions(new ProviderOptions("TestProvider") { Enabled = false }); var services = new ServiceCollection().AddOptions() .AddSingleton <IOptionsFactory <EasyAuthAuthenticationOptions>, OptionsFactory <EasyAuthAuthenticationOptions> >() .Configure <EasyAuthAuthenticationOptions>(EasyAuthAuthenticationDefaults.AuthenticationScheme, o => o.ProviderOptions = options.ProviderOptions) .BuildServiceProvider(); var monitor = services.GetRequiredService <IOptionsMonitor <EasyAuthAuthenticationOptions> >(); var handler = new EasyAuthAuthenticationHandler(monitor, new List <IEasyAuthAuthentificationService>(), this.loggerFactory, this.urlEncoder, this.clock, config); var schema = new AuthenticationScheme(EasyAuthAuthenticationDefaults.AuthenticationScheme, EasyAuthAuthenticationDefaults.DisplayName, typeof(EasyAuthAuthenticationHandler)); var context = new DefaultHttpContext(); // Act await handler.InitializeAsync(schema, context); var result = await handler.AuthenticateAsync(); // Assert Assert.False(result.Succeeded); }
public async Task UseEasyAuthProviderIfAuthIsDisabled() { // Arrange System.Environment.SetEnvironmentVariable("APPSETTING_WEBSITE_AUTH_ENABLED", "False"); var configBuilder = new ConfigurationBuilder(); configBuilder.AddEnvironmentVariables(); var config = configBuilder.Build(); var options = new EasyAuthAuthenticationOptions(); options.AddProviderOptions(new ProviderOptions("TestProvider") { Enabled = false }); var services = new ServiceCollection().AddOptions() .AddSingleton <IOptionsFactory <EasyAuthAuthenticationOptions>, OptionsFactory <EasyAuthAuthenticationOptions> >() .Configure <EasyAuthAuthenticationOptions>(EasyAuthAuthenticationDefaults.AuthenticationScheme, o => o.ProviderOptions = options.ProviderOptions) .BuildServiceProvider(); var monitor = services.GetRequiredService <IOptionsMonitor <EasyAuthAuthenticationOptions> >(); var handler = new EasyAuthAuthenticationHandler(monitor, new List <IEasyAuthAuthentificationService>(), this.loggerFactory, this.urlEncoder, this.clock, config); var schema = new AuthenticationScheme(EasyAuthAuthenticationDefaults.AuthenticationScheme, EasyAuthAuthenticationDefaults.DisplayName, typeof(EasyAuthAuthenticationHandler)); var context = new DefaultHttpContext(); // Act await handler.InitializeAsync(schema, context); var result = await handler.AuthenticateAsync(); // Assert Assert.False(result.Succeeded); // The EasyAuth me service is currently hard to test, so we only can check if it's fails Assert.NotNull(result.Failure); Assert.Equal("An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.", result.Failure.Message); }
public async Task IfTheUserIsAlreadyAuthorizedTheAuthResultIsSuccess() { // Arrange System.Environment.SetEnvironmentVariable("APPSETTING_WEBSITE_AUTH_ENABLED", "True"); System.Environment.SetEnvironmentVariable("APPSETTING_WEBSITE_AUTH_UNAUTHENTICATED_ACTION", "RedirectToLoginPage"); var configBuilder = new ConfigurationBuilder(); configBuilder.AddEnvironmentVariables(); var config = configBuilder.Build(); var options = new EasyAuthAuthenticationOptions(); options.AddProviderOptions(new ProviderOptions("TestProvider") { Enabled = false }); var services = new ServiceCollection().AddOptions() .AddSingleton <IOptionsFactory <EasyAuthAuthenticationOptions>, OptionsFactory <EasyAuthAuthenticationOptions> >() .Configure <EasyAuthAuthenticationOptions>(EasyAuthAuthenticationDefaults.AuthenticationScheme, o => o.ProviderOptions = options.ProviderOptions) .BuildServiceProvider(); var monitor = services.GetRequiredService <IOptionsMonitor <EasyAuthAuthenticationOptions> >(); var handler = new EasyAuthAuthenticationHandler(monitor, this.providers, this.loggerFactory, this.urlEncoder, this.clock, config); var schema = new AuthenticationScheme(EasyAuthAuthenticationDefaults.AuthenticationScheme, EasyAuthAuthenticationDefaults.DisplayName, typeof(EasyAuthAuthenticationHandler)); var context = new DefaultHttpContext(); // If this header is set the fallback with the local authme.json isn't used. context.Request.Headers.Add("X-MS-TOKEN-AAD-ID-TOKEN", "test"); var authResult = new TestProvider().AuthUser(context); context.User = authResult.Principal; // Act await handler.InitializeAsync(schema, context); var result = await handler.AuthenticateAsync(); // Assert Assert.False(result.Succeeded); Assert.True(context.User.Identity.IsAuthenticated); }
public async Task IfAnProviderIsEnabledUseEnabledProvider() { // Arrange System.Environment.SetEnvironmentVariable("APPSETTING_WEBSITE_AUTH_ENABLED", "True"); System.Environment.SetEnvironmentVariable("APPSETTING_WEBSITE_AUTH_UNAUTHENTICATED_ACTION", "RedirectToLoginPage"); var configBuilder = new ConfigurationBuilder(); configBuilder.AddEnvironmentVariables(); var config = configBuilder.Build(); var options = new EasyAuthAuthenticationOptions(); options.AddProviderOptions(new ProviderOptions("TestProvider") { Enabled = true }); var services = new ServiceCollection().AddOptions() .AddSingleton <IOptionsFactory <EasyAuthAuthenticationOptions>, OptionsFactory <EasyAuthAuthenticationOptions> >() .Configure <EasyAuthAuthenticationOptions>(EasyAuthAuthenticationDefaults.AuthenticationScheme, o => o.ProviderOptions = options.ProviderOptions) .BuildServiceProvider(); var monitor = services.GetRequiredService <IOptionsMonitor <EasyAuthAuthenticationOptions> >(); var handler = new EasyAuthAuthenticationHandler(monitor, this.providers, this.loggerFactory, this.urlEncoder, this.clock, config); var schema = new AuthenticationScheme(EasyAuthAuthenticationDefaults.AuthenticationScheme, EasyAuthAuthenticationDefaults.DisplayName, typeof(EasyAuthAuthenticationHandler)); var httpContext = new DefaultHttpContext(); await handler.InitializeAsync(schema, httpContext); // Act var result = await handler.AuthenticateAsync(); // Assert Assert.Equal("testName", result.Principal.Identity.Name); Assert.True(result.Succeeded); Assert.Equal("testType", result.Principal.Identity.AuthenticationType); Assert.True(result.Principal.Identity.IsAuthenticated); }