/// <summary>
        /// AddSecurity.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="configuration"></param>
        private static void AddSecurity(AspNetCoreOpenApiDocumentGeneratorSettings document, IConfigurationRoot configuration)
        {
            var scope = new Dictionary <string, string>
            {
                { configuration[ConstantsConfiguration.KEY_HANDLER], configuration[ConstantsConfiguration.KEY_VALUE] }
            };
            var globalScopeNames = new List <string>
            {
                configuration[ConstantsConfiguration.KEY_HANDLER]
            };

            document.DocumentProcessors.Add(new SecurityDefinitionAppender(configuration[ConstantsConfiguration.KEY_HANDLER], new OpenApiSecurityScheme
            {
                Type        = OpenApiSecuritySchemeType.ApiKey,
                Name        = configuration[ConstantsConfiguration.KEY_HANDLER],
                Description = configuration[ConstantsConfiguration.KEY_VALUE],
                In          = OpenApiSecurityApiKeyLocation.Header,
                Scopes      = scope
            }));

            document.AddSecurity(configuration[ConstantsConfiguration.KEY_HANDLER], globalScopeNames, new OpenApiSecurityScheme
            {
                Type        = OpenApiSecuritySchemeType.ApiKey,
                Name        = configuration[ConstantsConfiguration.KEY_HANDLER],
                Description = configuration[ConstantsConfiguration.KEY_VALUE],
                In          = OpenApiSecurityApiKeyLocation.Header,
                Scopes      = scope
            });
        }
示例#2
0
        internal static void AddSecurity(this AspNetCoreOpenApiDocumentGeneratorSettings @this, IConfiguration configuration)
        {
            var authConfig = configuration
                             .GetSection("authentication")
                             .Get <AuthConfiguration>();

            if (!authConfig.IsEnabled())
            {
                return;
            }

            @this.AddSecurity("bearer", Empty <string>(), new OpenApiSecurityScheme
            {
                Type        = OAuth2,
                Description = "Severino",
                Flow        = Password,
                Flows       = new OpenApiOAuthFlows
                {
                    Password = new OpenApiOAuthFlow
                    {
                        TokenUrl = Combine(authConfig.Authority?.Uri, "connect/token"),
                        Scopes   = new Dictionary <string, string>
                        {
                            { "hawk", "Admin" },
                            { "hawk.readonly", "Read-only" },
                        },
                    },
                },
            });

            @this.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));
        }
示例#3
0
文件: Startup.cs 项目: YzYpYzY/koudou
        private void ConfigureSwagger(AspNetCoreOpenApiDocumentGeneratorSettings config)
        {
            config.AddSecurity("bearer", Enumerable.Empty <string>(), new OpenApiSecurityScheme
            {
                Type        = OpenApiSecuritySchemeType.OAuth2,
                Description = "Koudou Authentication",
                Flow        = OpenApiOAuth2Flow.Password,
                Flows       = new OpenApiOAuthFlows()
                {
                    Password = new OpenApiOAuthFlow()
                    {
                        Scopes = new Dictionary <string, string>
                        {
                            { "koudou-api", "Koudou WebAPI" }
                        },
                        TokenUrl         = "/api/v1/auth/Token",
                        AuthorizationUrl = "/api/v1/auth/Token",
                    }
                }
            });
            config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));

            config.PostProcess = document =>
            {
                document.Info.Title       = "Koudou api";
                document.Info.Description = "API description";
                document.Info.Version     = "Version 1.0";
            };

            config.GenerateEnumMappingDescription = true;
        }
        public static AspNetCoreOpenApiDocumentGeneratorSettings AddCustomSecurity(this AspNetCoreOpenApiDocumentGeneratorSettings settings, IConfiguration configuration)
        {
            settings.AddSecurity("oauth2", new OpenApiSecurityScheme
            {
                Type        = OpenApiSecuritySchemeType.OAuth2,
                Description = "AAD B2C Authentication",
                Flow        = OpenApiOAuth2Flow.Implicit,
                Flows       = new OpenApiOAuthFlows
                {
                    Implicit = new OpenApiOAuthFlow
                    {
                        TokenUrl         = $"https://together2.b2clogin.com/{configuration["AzureAdB2C:Tenant"]}/{configuration["AzureAdB2C:Policy"]}/oauth2/v2.0/token",
                        AuthorizationUrl = $"https://together2.b2clogin.com/{configuration["AzureAdB2C:Tenant"]}/{configuration["AzureAdB2C:Policy"]}/oauth2/v2.0/authorize",
                        Scopes           = new Dictionary <string, string>
                        {
                            { "https://together2.onmicrosoft.com/activityapi/user_impersonation", "Access the api as the signed-in user" }
                        }
                    }
                }
            });

            settings.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("oauth2"));

            return(settings);
        }
示例#5
0
        public override void Apply(AspNetCoreOpenApiDocumentGeneratorSettings document)
        {
            var _in = EnumEx.Parse <OpenApiSecurityApiKeyLocation>(this?.Location.ToString());

            document.AddSecurity(this?.SecurityName ?? SwaggerNSwagExtensions.DefaultSecurityName /*, new[] { "skoruba_identity_admin_api" }*/, new NSwag.OpenApiSecurityScheme
            {
                Description = this?.Description,
                Name        = this?.Name,
                Type        = NSwag.OpenApiSecuritySchemeType.Basic
            });
        }
示例#6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="configure"></param>
        internal static void AddJWTSecurity(this AspNetCoreOpenApiDocumentGeneratorSettings configure)
        {
            configure.AddSecurity("JWT", Enumerable.Empty <string>(), new OpenApiSecurityScheme
            {
                Type        = OpenApiSecuritySchemeType.ApiKey,
                Name        = "Authorization",
                In          = OpenApiSecurityApiKeyLocation.Header,
                Description = "Type into the textbox: Bearer {your JWT token}."
            });

            configure.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
        }
示例#7
0
 private void ConfigureOpenApiDocument(IConfiguration configuration, AspNetCoreOpenApiDocumentGeneratorSettings settings)
 {
     settings.DocumentName = "v1";
     settings.Version      = "v1";
     settings.Title        = this.GetType().Namespace;
     settings.AddSecurity(
         "bearer",
         Enumerable.Empty <string>(),
         new OpenApiSecurityScheme
     {
         Type        = OpenApiSecuritySchemeType.OAuth2,
         Flow        = OpenApiOAuth2Flow.Implicit,
         Description = "Oidc Authentication",
         Flows       = new OpenApiOAuthFlows
         {
             Implicit = new OpenApiOAuthFlow
             {
                 AuthorizationUrl = $"{configuration["Oidc:Authority"]}/protocol/openid-connect/auth",
                 TokenUrl         = $"{configuration["Oidc:Authority"]}/protocol/openid-connect/token",
                 Scopes           = new Dictionary <string, string>
                 {
                     //{"openid", "openid"},
                 }
             }
         },
     });
     settings.OperationProcessors.Add(new ApiVersionProcessor());
     settings.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));
     settings.OperationProcessors.Add(new AuthorizationOperationProcessor("bearer"));
     settings.PostProcess = document =>
     {
         document.Info.Version        = "v1";
         document.Info.Title          = this.GetType().Namespace;
         document.Info.Description    = "Weather API";
         document.Info.TermsOfService = "http://www.weather.com";
         document.Info.Contact        = new OpenApiContact
         {
             Name  = "John Doe",
             Email = "*****@*****.**",
             Url   = "http://www.weather.com"
         };
     };
 }