/// <summary>
        /// Initializes a new instance of the <see cref="IdentityServerBearerTokenValidationMiddleware" /> class.
        /// </summary>
        /// <param name="next">The next middleware.</param>
        /// <param name="options">The options.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        public IdentityServerBearerTokenValidationMiddleware(AppFunc next, IdentityServerOAuthBearerAuthenticationOptions options, ILoggerFactory loggerFactory)
        {
            _next = next;
            _options = options;
            _logger = loggerFactory.Create(this.GetType().FullName);

            if (options.LocalValidationOptions != null)
            {
                var localBuilder = new AppBuilder();
                options.OnValidationAppBuilderCreated?.Invoke(localBuilder);
                localBuilder.UseOAuthBearerAuthentication(options.LocalValidationOptions);
                localBuilder.Run(ctx => next(ctx.Environment));
                _localValidationFunc = localBuilder.Build();
            }

            if (options.EndpointValidationOptions != null)
            {
                var endpointBuilder = new AppBuilder();
                options.OnValidationAppBuilderCreated?.Invoke(endpointBuilder);
                endpointBuilder.Properties["host.AppName"] = "foobar";

                endpointBuilder.UseOAuthBearerAuthentication(options.EndpointValidationOptions);
                endpointBuilder.Run(ctx => next(ctx.Environment));
                _endpointValidationFunc = endpointBuilder.Build();
            }
        }
        /// <summary>
        /// Add identity server token authentication to the pipeline.
        /// </summary>
        /// <param name="app">The application.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static IAppBuilder UseIdentityServerBearerTokenAuthentication(this IAppBuilder app, IdentityServerBearerTokenAuthenticationOptions options)
        {
            if (app == null) throw new ArgumentNullException("app");
            if (options == null) throw new ArgumentNullException("options");
            if (string.IsNullOrEmpty(options.Authority)) throw new ArgumentException("Authority must be set", "authority");

            var loggerFactory = app.GetLoggerFactory();
            var middlewareOptions = new IdentityServerOAuthBearerAuthenticationOptions();

            if (options.ValidationMode == ValidationMode.Both ||
                options.ValidationMode == ValidationMode.Local)
            {
                middlewareOptions.LocalValidationOptions = ConfigureLocalValidation(options, loggerFactory);
            }
            
            if (options.ValidationMode == ValidationMode.Both ||
                options.ValidationMode == ValidationMode.ValidationEndpoint)
            {
                middlewareOptions.EndpointValidationOptions = ConfigureEndpointValidation(options, loggerFactory);
            }

            if (options.TokenProvider != null)
            {
                middlewareOptions.TokenProvider = options.TokenProvider;
            }

            app.Use<IdentityServerBearerTokenValidationMiddleware>(middlewareOptions);

            if (options.RequiredScopes.Any())
            {
                app.Use<ScopeRequirementMiddleware>(options.RequiredScopes);
            }

            return app;
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IdentityServerBearerTokenValidationMiddleware" /> class.
        /// </summary>
        /// <param name="next">The next middleware.</param>
        /// <param name="app">The app builder.</param>
        /// <param name="options">The options.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        public IdentityServerBearerTokenValidationMiddleware(AppFunc next, IAppBuilder app, IdentityServerOAuthBearerAuthenticationOptions options, ILoggerFactory loggerFactory)
        {
            _next    = next;
            _options = options;
            _logger  = loggerFactory.Create(this.GetType().FullName);

            if (options.LocalValidationOptions != null)
            {
                _localValidationFunc = new Lazy <AppFunc>(() =>
                {
                    var localBuilder = app.New();
                    localBuilder.UseOAuthBearerAuthentication(options.LocalValidationOptions.Value);
                    localBuilder.Run(ctx => next(ctx.Environment));
                    return(localBuilder.Build());
                }, LazyThreadSafetyMode.PublicationOnly);
            }

            if (options.EndpointValidationOptions != null)
            {
                _endpointValidationFunc = new Lazy <AppFunc>(() =>
                {
                    var endpointBuilder = app.New();
                    endpointBuilder.Properties["host.AppName"] = "foobar";

                    endpointBuilder.UseOAuthBearerAuthentication(options.EndpointValidationOptions.Value);
                    endpointBuilder.Run(ctx => next(ctx.Environment));
                    return(endpointBuilder.Build());
                }, true);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="IdentityServerBearerTokenValidationMiddleware" /> class.
        /// </summary>
        /// <param name="next">The next middleware.</param>
        /// <param name="app">The app builder.</param>
        /// <param name="options">The options.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        public IdentityServerBearerTokenValidationMiddleware(AppFunc next, IAppBuilder app, IdentityServerOAuthBearerAuthenticationOptions options, ILoggerFactory loggerFactory)
        {
            _next = next;
            _options = options;
            _logger = loggerFactory.Create(this.GetType().FullName);

            if (options.LocalValidationOptions != null)
            {
                _localValidationFunc = new Lazy<AppFunc>(() => 
                {
                    var localBuilder = app.New();
                    localBuilder.UseOAuthBearerAuthentication(options.LocalValidationOptions.Value);
                    localBuilder.Run(ctx => next(ctx.Environment));
                    return localBuilder.Build();

                }, true);
            }

            if (options.EndpointValidationOptions != null)
            {
                _endpointValidationFunc = new Lazy<AppFunc>(() => 
                {
                    var endpointBuilder = app.New();
                    endpointBuilder.Properties["host.AppName"] = "foobar";

                    endpointBuilder.UseOAuthBearerAuthentication(options.EndpointValidationOptions.Value);
                    endpointBuilder.Run(ctx => next(ctx.Environment));
                    return endpointBuilder.Build();

                }, true);
            }
        }
        /// <summary>
        /// Add identity server token authentication to the pipeline.
        /// </summary>
        /// <param name="app">The application.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static IAppBuilder UseIdentityServerBearerTokenAuthentication(this IAppBuilder app, IdentityServerBearerTokenAuthenticationOptions options)
        {
            if (app == null) throw new ArgumentNullException("app");
            if (options == null) throw new ArgumentNullException("options");

            var loggerFactory = app.GetLoggerFactory();
            var middlewareOptions = new IdentityServerOAuthBearerAuthenticationOptions();

            switch (options.ValidationMode)
            {
                case ValidationMode.Local:
                    middlewareOptions.LocalValidationOptions = ConfigureLocalValidation(options, loggerFactory);
                    break;
                case ValidationMode.ValidationEndpoint:
                    middlewareOptions.EndpointValidationOptions = ConfigureEndpointValidation(options, loggerFactory);
                    break;
                case ValidationMode.Both:
                    middlewareOptions.LocalValidationOptions = ConfigureLocalValidation(options, loggerFactory);
                    middlewareOptions.EndpointValidationOptions = ConfigureEndpointValidation(options, loggerFactory);
                    break;
                default:
                    throw new Exception("ValidationMode has invalid value");
            }

            if (options.TokenProvider != null)
            {
                middlewareOptions.TokenProvider = options.TokenProvider;
            }

            app.Use<IdentityServerBearerTokenValidationMiddleware>(app, middlewareOptions, loggerFactory);

            if (options.RequiredScopes.Any())
            {
                var scopeOptions = new ScopeRequirementOptions
                {
                    AuthenticationType = options.AuthenticationType,
                    RequiredScopes = options.RequiredScopes
                };

                app.Use<ScopeRequirementMiddleware>(scopeOptions);
            }

            if (options.PreserveAccessToken)
            {
                app.Use<PreserveAccessTokenMiddleware>();
            }

            return app;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="IdentityServerBearerTokenValidationMiddleware"/> class.
        /// </summary>
        /// <param name="next">The next middleware.</param>
        /// <param name="options">The options.</param>
        public IdentityServerBearerTokenValidationMiddleware(AppFunc next, IdentityServerOAuthBearerAuthenticationOptions options)
        {
            _next = next;
            _options = options;

            if (options.LocalValidationOptions != null)
            {
                var localBuilder = new AppBuilder();
                localBuilder.UseOAuthBearerAuthentication(options.LocalValidationOptions);
                localBuilder.Run(ctx => next(ctx.Environment));
                _localValidationFunc = localBuilder.Build();
            }

            if (options.EndpointValidationOptions != null)
            {
                var endpointBuilder = new AppBuilder();
                endpointBuilder.Properties["host.AppName"] = "foobar";

                endpointBuilder.UseOAuthBearerAuthentication(options.EndpointValidationOptions);
                endpointBuilder.Run(ctx => next(ctx.Environment));
                _endpointValidationFunc = endpointBuilder.Build();
            }
        }
示例#7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IdentityServerBearerTokenValidationMiddleware"/> class.
        /// </summary>
        /// <param name="next">The next middleware.</param>
        /// <param name="options">The options.</param>
        public IdentityServerBearerTokenValidationMiddleware(AppFunc next, IdentityServerOAuthBearerAuthenticationOptions options)
        {
            _next    = next;
            _options = options;

            if (options.LocalValidationOptions != null)
            {
                var localBuilder = new AppBuilder();
                localBuilder.UseOAuthBearerAuthentication(options.LocalValidationOptions);
                localBuilder.Run(ctx => next(ctx.Environment));
                _localValidationFunc = localBuilder.Build();
            }

            if (options.EndpointValidationOptions != null)
            {
                var endpointBuilder = new AppBuilder();
                endpointBuilder.Properties["host.AppName"] = "foobar";

                endpointBuilder.UseOAuthBearerAuthentication(options.EndpointValidationOptions);
                endpointBuilder.Run(ctx => next(ctx.Environment));
                _endpointValidationFunc = endpointBuilder.Build();
            }
        }