/// <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="ScopeRequirementMiddleware"/> class. /// </summary> /// <param name="next">The next midleware.</param> /// <param name="options">The options.</param> public ScopeRequirementMiddleware(AppFunc next, ScopeRequirementOptions options) { _next = next; _options = options; }