Coder Social home page Coder Social logo

donalfenwick / swashbucklebasicauthcredentialspopup Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 27 KB

Adds a CSS dialog box to swagger-ui to accept username/password input when a user clicks the parameter input for an Authorization header within the api doc

HTML 30.23% JavaScript 39.87% CSS 29.90%

swashbucklebasicauthcredentialspopup's Introduction

Swashbuckle Basic HTTP Authentication credentials popup

Javascript plugin that can be loaded into swagger-ui when using Swashbuckle to generate api documentation for webapi. The plugin provides an interface for editing basic http credentials when submitting test requests to basic auth protected api endpoints from within swagger-ui.

build and instalation

Install packages npm install

Build gulp default

Copy /build/authPromptPlugin-min.js out to your webapi project and include it as an embedded resource. See injecting custom content on the swashbucke wiki.

To ensure the script is loaded into swagger-ui add the following code when configuring swashbuckle.

   .EnableSwagger( ... )
   .EnableSwaggerUi(c =>
       {
           c.InjectJavaScript(containingAssembly, "myWebApi.SwaggerExtensions.authPromptPlugin-min.js");
           // ...
       });

The popup is triggered when the user clicks on a parameter input field named "Authorization" <input type="text" name="Authorization" />.

The Authorization parameter field is not added to a request in swashbuckle by defualt. In order to wire this up, register the following operation filter during configuration of swashbuckle.

 public class ApplyBasicAuthToApiEndpoints : IOperationFilter
   {
       public string Name { get; private set; }

       public ApplyBasicAuthToApiEndpoints()
       {
           Name = "basic";
       }

       public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
       {
           if (operation.parameters == null)
           {
               operation.parameters = new List<Parameter>();
           }

           // modify the logic in this if statement to apply the header to any api actions that use basic authentication
           // this example uses a custom attribute on the controler actions
           if (apiDescription.ActionDescriptor.GetCustomAttributes<RequireBasicAuthAttribute>(true).Count > 0)
           {
               operation.parameters.Add(new Parameter
               {
                   name = "Authorization",
                   @in = "header",
                   description = "Basic http auth credeitials",
                   required = true,
                   type = "string"
               });
           }

       }
   }

swashbucklebasicauthcredentialspopup's People

Contributors

donalfenwick avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.