public async Task Ctor_EnvDisable()
        {
            // Arrange
            Environment.SetEnvironmentVariable("WEBSITE_AUTH_ENABLED", null);
            var context = new DefaultHttpContext();
            var options = new AzureAppServiceAuthenticationOptions();
            var handler = await GetInitializedHandler(context, options);

            // Act
            bool isEnabled = handler.IsEnabled;

            // Assert
            Assert.False(isEnabled);
        }
        public async Task HandleAuthenticate_IfDisabled_NoResult()
        {
            // Arrange
            Environment.SetEnvironmentVariable("WEBSITE_AUTH_ENABLED", null);
            var context = new DefaultHttpContext();
            var options = new AzureAppServiceAuthenticationOptions();
            var handler = await GetInitializedHandler(context, options);

            // Act
            var result = await handler.AuthenticateAsync().ConfigureAwait(false);

            // Assert
            Assert.True(result.None);
        }
        public async Task HandleAuthenticate_IfNoToken_NoResult()
        {
            // Arrange
            var options = new AzureAppServiceAuthenticationOptions()
            {
                ForceEnable = true
            };
            var context = new DefaultHttpContext();
            var handler = await GetInitializedHandler(context, options);

            // Act
            var result = await handler.AuthenticateAsync().ConfigureAwait(false);

            // Assert
            Assert.True(result.None);
        }
        public async Task Ctor_ForceEnable()
        {
            // Arrange
            var context = new DefaultHttpContext();
            var options = new AzureAppServiceAuthenticationOptions()
            {
                ForceEnable = true
            };
            var handler = await GetInitializedHandler(context, options);

            // Act
            bool isEnabled = handler.IsEnabled;

            // Assert
            Assert.True(isEnabled);
        }
        public async Task HandleAuthenticate_IfNoName_Fail()
        {
            // Arrange
            var options = new AzureAppServiceAuthenticationOptions()
            {
                ForceEnable = true
            };
            var context = new DefaultHttpContext();

            SetAuthToken(context, "aad", AadTokenWithNoName);
            var handler = await GetInitializedHandler(context, options);

            // Act
            var result = await handler.AuthenticateAsync().ConfigureAwait(false);

            // Assert
            Assert.NotNull(result.Failure);
            Assert.False(result.Succeeded);
        }
        public async Task HandleAuthenticate_GeneratesPrincipal()
        {
            // Arrange
            var options = new AzureAppServiceAuthenticationOptions()
            {
                ForceEnable = true
            };
            var context = new DefaultHttpContext();

            SetAuthToken(context, "aad", AadValidToken);
            var handler = await GetInitializedHandler(context, options);

            // Act
            var result = await handler.AuthenticateAsync().ConfigureAwait(false);

            // Assert
            Assert.True(result.Succeeded);
            Assert.NotNull(context.User);
            Assert.True(context.User.Identity.IsAuthenticated);
            Assert.Equal("*****@*****.**", context.User.Identity.Name);
            Assert.Equal("aad", context.User.Identity.AuthenticationType);
        }
Example #7
0
 /// <summary>
 /// Adds the <see cref="AzureAppServiceAuthenticationMiddleware"/> middleware to the specified
 /// <see cref="IApplicationBuilder"/>, which enables authorization of the JWT-based Azure App
 /// Service authentication.
 /// </summary>
 /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
 /// <param name="options">The <see cref="AzureAppServiceAuthenticationOptions"/> that specified options for the middleware.</param>
 /// <returns>A reference to this instance after the operation has completed.</returns>
 public static IApplicationBuilder UseAzureAppServiceAuthentication(this IApplicationBuilder app, AzureAppServiceAuthenticationOptions options)
 {
     if (app == null)
     {
         throw new ArgumentNullException(nameof(app));
     }
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
     return(app.UseMiddleware <AzureAppServiceAuthenticationMiddleware>(Options.Create(options)));
 }
        private static async Task <AzureAppServiceAuthenticationHandler> GetInitializedHandler(HttpContext context, AzureAppServiceAuthenticationOptions options = null)
        {
            var loggerFactory  = GetMockLoggerFactory();
            var encoder        = new Mock <UrlEncoder>();
            var clock          = new Mock <ISystemClock>();
            var optionsMonitor = GetMockOptionsMonitor(options ?? new AzureAppServiceAuthenticationOptions());
            var authScheme     = new AuthenticationScheme(AzureAppServiceAuthentication.AuthenticationScheme, AzureAppServiceAuthentication.DisplayName, typeof(AzureAppServiceAuthenticationHandler));
            var handler        = new AzureAppServiceAuthenticationHandler(optionsMonitor, loggerFactory, encoder.Object, clock.Object);
            await handler.InitializeAsync(authScheme, context).ConfigureAwait(false);

            return(handler);
        }
        private static IOptionsMonitor <AzureAppServiceAuthenticationOptions> GetMockOptionsMonitor(AzureAppServiceAuthenticationOptions options)
        {
            var monitor = new Mock <IOptionsMonitor <AzureAppServiceAuthenticationOptions> >();

            monitor.Setup(x => x.Get(AzureAppServiceAuthentication.AuthenticationScheme)).Returns(options);
            return(monitor.Object);
        }