Ejemplo n.º 1
0
        public static string GetSwaggerEndpoint(ElectSwaggerOptions options, bool isIncludeAccessKey = true)
        {
            string swaggerEndpoint = $"/{options.SwaggerRoutePrefix}/{options.Version}/{options.SwaggerName}";

            if (isIncludeAccessKey && !string.IsNullOrWhiteSpace(options.AccessKey))
            {
                swaggerEndpoint += $"?{ElectSwaggerConstants.AccessKeyName}={options.AccessKey}";
            }

            return(swaggerEndpoint);
        }
        public static ElectSwaggerOptions GetOptions(IConfiguration configuration,
                                                     string sectionName = "ElectSwagger")
        {
            var electSwaggerOptions = new ElectSwaggerOptions();

            electSwaggerOptions.IsEnable =
                configuration.GetValueByEnv <bool>($"{sectionName}:{nameof(electSwaggerOptions.IsEnable)}");

            electSwaggerOptions.SwaggerRoutePrefix =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.SwaggerRoutePrefix)}");

            electSwaggerOptions.SwaggerName =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.SwaggerName)}");

            electSwaggerOptions.Url =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.Url)}");

            electSwaggerOptions.JsonViewerUrl =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.JsonViewerUrl)}");

            electSwaggerOptions.Title =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.Title)}");

            electSwaggerOptions.Version =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.Version)}");

            electSwaggerOptions.AccessKey =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.AccessKey)}");

            electSwaggerOptions.UnAuthorizeMessage =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.UnAuthorizeMessage)}");

            electSwaggerOptions.AuthTokenType =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.AuthTokenType)}");

            electSwaggerOptions.IsFullSchemaForType =
                configuration.GetValueByEnv <bool>($"{sectionName}:{nameof(electSwaggerOptions.IsFullSchemaForType)}");

            electSwaggerOptions.AuthorName =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.AuthorName)}");

            electSwaggerOptions.AuthorEmail =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.AuthorEmail)}");

            electSwaggerOptions.AuthorWebsite =
                configuration.GetValueByEnv <string>($"{sectionName}:{nameof(electSwaggerOptions.AuthorWebsite)}");

            electSwaggerOptions.GlobalParameters =
                configuration.GetListValueByEnv <OpenApiParameter>(
                    $"{sectionName}:{nameof(electSwaggerOptions.GlobalParameters)}");

            return(electSwaggerOptions);
        }
Ejemplo n.º 3
0
        public static bool IsAccessUI(HttpContext httpContext, ElectSwaggerOptions options)
        {
            var pathQuery = httpContext.Request.Path.Value?.Trim('/').ToLower() ?? string.Empty;

            pathQuery = pathQuery.ToLowerInvariant();

            var documentApiBaseUrl = options.SwaggerRoutePrefix ?? string.Empty;

            documentApiBaseUrl = documentApiBaseUrl.ToLowerInvariant();

            var isSwaggerUi = pathQuery == documentApiBaseUrl || pathQuery == $"{documentApiBaseUrl}/index.html";

            return(isSwaggerUi || httpContext.Request.IsRequestFor(options.Url));
        }
Ejemplo n.º 4
0
 public static SwaggerGenOptions AddElectSwaggerGenOptions([NotNull] SwaggerGenOptions swaggerGenOptions,
                                                           [NotNull] ElectSwaggerOptions configuration)
 {
     return(AddElectSwaggerGenOptions(swaggerGenOptions, _ =>
     {
         _.SwaggerRoutePrefix = configuration.SwaggerRoutePrefix;
         _.SwaggerName = configuration.SwaggerName;
         _.Url = configuration.Url;
         _.JsonViewerUrl = configuration.JsonViewerUrl;
         _.Title = configuration.Title;
         _.Version = configuration.Version;
         _.AccessKey = configuration.AccessKey;
         _.UnAuthorizeMessage = configuration.UnAuthorizeMessage;
         _.AuthTokenType = configuration.AuthTokenType;
         _.IsFullSchemaForType = configuration.IsFullSchemaForType;
         _.AuthorName = configuration.AuthorName;
         _.AuthorEmail = configuration.AuthorEmail;
         _.AuthorWebsite = configuration.AuthorWebsite;
     }));
 }
 public static IServiceCollection AddElectSwagger(this IServiceCollection services,
                                                  [NotNull] ElectSwaggerOptions configuration)
 {
     return(services.AddElectSwagger(_ =>
     {
         _.IsEnable = configuration.IsEnable;
         _.SwaggerRoutePrefix = configuration.SwaggerRoutePrefix;
         _.SwaggerName = configuration.SwaggerName;
         _.Url = configuration.Url;
         _.JsonViewerUrl = configuration.JsonViewerUrl;
         _.Title = configuration.Title;
         _.Version = configuration.Version;
         _.AccessKey = configuration.AccessKey;
         _.UnAuthorizeMessage = configuration.UnAuthorizeMessage;
         _.AuthTokenType = configuration.AuthTokenType;
         _.IsFullSchemaForType = configuration.IsFullSchemaForType;
         _.AuthorName = configuration.AuthorName;
         _.AuthorEmail = configuration.AuthorEmail;
         _.AuthorWebsite = configuration.AuthorWebsite;
         _.GlobalParameters = configuration.GlobalParameters;
         _.ExtendOptions = configuration.ExtendOptions;
     }));
 }
Ejemplo n.º 6
0
 public static bool IsAccessSwaggerEndpoint(HttpContext httpContext, ElectSwaggerOptions options)
 {
     return(httpContext.Request.IsRequestFor(GetSwaggerEndpoint(options, false)));
 }
Ejemplo n.º 7
0
 public static bool IsAccessJsonViewer(HttpContext httpContext, ElectSwaggerOptions options)
 {
     return(httpContext.Request.IsRequestFor(options.JsonViewerUrl));
 }
Ejemplo n.º 8
0
 /// <summary>
 ///     Check request access to UI, Json Viewer or Swagger Pure Doc
 /// </summary>
 /// <param name="httpContext"></param>
 /// <param name="options">    </param>
 /// <returns></returns>
 public static bool IsAccessSwagger(HttpContext httpContext, ElectSwaggerOptions options)
 {
     return(IsAccessUI(httpContext, options) || IsAccessJsonViewer(httpContext, options) ||
            IsAccessSwaggerEndpoint(httpContext, options));
 }
 public GlobalParameterOperationFilter(IOptions <ElectSwaggerOptions> configuration)
 {
     Options = configuration.Value;
 }
Ejemplo n.º 10
0
 public HomeController(IOptions <ElectSwaggerOptions> electSwaggerConfig)
 {
     _electSwaggerOptions = electSwaggerConfig.Value;
 }
Ejemplo n.º 11
0
            public ElectSwaggerMiddleware(RequestDelegate next, IOptions <ElectSwaggerOptions> configuration)
            {
                _next = next;

                _options = configuration.Value;
            }
 public static IServiceCollection AddElectSwagger(this IServiceCollection services, [NotNull] ElectSwaggerOptions configuration)
 {
     return(services.AddElectSwagger(_ =>
     {
         _.SwaggerRoutePrefix = configuration.SwaggerRoutePrefix;
         _.SwaggerName = configuration.SwaggerName;
         _.Url = configuration.Url;
         _.JsonViewerUrl = configuration.JsonViewerUrl;
         _.Title = configuration.Title;
         _.Version = configuration.Version;
         _.AccessKey = configuration.AccessKey;
         _.UnAuthorizeMessage = configuration.UnAuthorizeMessage;
         _.AuthTokenType = configuration.AuthTokenType;
         _.IsFullSchemaForType = configuration.IsFullSchemaForType;
         _.IsDescribeAllEnumsAsString = configuration.IsDescribeAllEnumsAsString;
         _.IsDescribeAllParametersInCamelCase = configuration.IsDescribeAllParametersInCamelCase;
         _.AuthorName = configuration.AuthorName;
         _.AuthorEmail = configuration.AuthorEmail;
         _.AuthorWebsite = configuration.AuthorWebsite;
     }));
 }
Ejemplo n.º 13
0
 public static SwaggerGenOptions AddElectSwaggerGenOptions([NotNull] this SwaggerGenOptions swaggerGenOptions, [NotNull] ElectSwaggerOptions configuration)
 {
     return(SwaggerGenOptionsHelper.AddElectSwaggerGenOptions(swaggerGenOptions, configuration));
 }