Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to integrate with jwt based authentication? #818

Closed
bebraw opened this issue Jan 9, 2015 · 32 comments
Closed

How to integrate with jwt based authentication? #818

bebraw opened this issue Jan 9, 2015 · 32 comments
Milestone

Comments

@bebraw
Copy link

bebraw commented Jan 9, 2015

I set up a little example that shows how to build a minimal Todo backend using Express, jwt and co. The problem is that swagger-ui relies on api key. I noticed it's possible to customize this a little bit (ie. push api key to header) but that doesn't solve the problem entirely.

I would need some way to log in to the system first. I will be able to get the token required by authentication through that.

An extra challenge is how to make this all work nicely with swagger-tools but that's a separate problem. If there was a nice extension point for something like this, it would probably be easier to handle that side. I would just need something that can be plugged into an Express middleware and served as a static site through it.

@fehguy
Copy link
Contributor

fehguy commented Jan 9, 2015

Hi, I'm not sure exactly what you mean. Can you please elaborate? Most people customize the index.html file for swagger-ui and bring in the javascript portions.

@bebraw
Copy link
Author

bebraw commented Jan 9, 2015

I guess the ideal would be that I could consume swagger-ui as a dependency of my project, inject the changes I need (ie. way to log in -> auth header) and serve it through a middleware.

If this isn't feasible, I suppose I could just maintain a copy of swagger-ui within the project itself. It looks like this is exactly what swagger-tools is doing.

@offero
Copy link

offero commented Jan 15, 2015

We have the same situation as @bebraw. We are also using JWT (http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html), which basically requires a custom Authorization header for the API calls. I would be OK if I could just paste the token I receive from my server into an Authorization header field for the remainder of the API calls.

@webron
Copy link
Contributor

webron commented Jan 15, 2015

It may be an issue with the spec itself. Based on http://oauth.net/documentation, it looks like JWT is an extension of OAuth2, though I'm not sure how it comes in to play. It looks like it can be integrated with the implicit (bearer) flow, which is already supported by Swagger, but I'm not sure whether that requires any additional extensions.

@fehguy fehguy added this to the v2.0.25 milestone Jan 16, 2015
@fehguy fehguy modified the milestones: v2.1.0-M2, v2.0.25 Jan 31, 2015
@Todilo
Copy link

Todilo commented Feb 9, 2015

Does anyone have more information regarding this, some sort of instructions or tips on how to implement the authentication process and adding the token to the header during all calls?

@bytesandwich
Copy link

ditto @Todilo

@fehguy
Copy link
Contributor

fehguy commented Feb 17, 2015

@Todilo @jackphel please see here:

https://github.com/swagger-api/swagger-js/blob/master/README.md#custom-request-signing

@bytesandwich
Copy link

@fehguy Thanks for your quick response! I got that working with swagger-ui. I think I should add jwt as a header token in https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#security-scheme-object- though I'm not sure whether it's exactly a great fit because the token encodes a json object with data specific to the user's context rather than an api key.

@Qwerios
Copy link

Qwerios commented Jun 18, 2015

I hacked this into index.html today to get it working. I changed the addApiKeyAuthorization implementation to the following:

      function addApiKeyAuthorization(){
        var key = encodeURIComponent( $('#input_apiKey')[0].value );
        if(key && key.trim() != "") {
            var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization( "Authorization", "Bearer " + key, "header" );
            window.swaggerUi.api.clientAuthorizations.add( "bearer", apiKeyAuth );
            log( "Set bearer token: " + key );
        }
      }

The security definition in my swagger.json looks like this:

  "securityDefinitions": {
    "bearer": {
      "type": "apiKey",
      "name": "Authorization",
      "in": "header"
    }
  }

Token is now being set in the headers for each request where I indicate the "bearer" security is required. I'll likely tack on a little localStorage magic to store the token in the browser to persist beyond reloads.

Hope this helps someone

@memordial
Copy link

@Qwerios thank you very much for your guide. that is really helps me. i didnt know if bearer between swaggerUi.api.clientAuthorizations.add() and securityDefinitions must be same

@webron webron modified the milestones: v2.1.2, v2.1.1 Jul 21, 2015
@webron webron modified the milestones: v2.1.2, v2.1.3 Jul 31, 2015
@knvpk
Copy link

knvpk commented Mar 22, 2016

Hi, by using @Qwerios 's solution, the header is sent for all routes, and it didn't checking that swagger spec has security defined or not.

@Qwerios
Copy link

Qwerios commented Mar 22, 2016

Applying the apiKey is done inside the swagger-ui.js. For testing with the swagger UI setting the token for each call is never a problem for my scenarios. If I need to call unauthenticated I just emtpy the bearer token input field.

I'm afraid I don't have intimate knowledge of how the swagger-ui code works internally nor do I have the time to dive into it.

@knvpk
Copy link

knvpk commented Mar 22, 2016

@Qwerios , But in my application , for some urls the token must not be used.

@K1ll3rF0x
Copy link
Contributor

Here is a related approach to add JWT support into Swagger UI project (#2234). This adds a new 'jwt'-type authorization scheme with login-support in the Swagger UI.

However, to get it this pull request integrated in the Swagger UI, support for JWT based authentication needs to be added in the OpenAPI specification first.
If somebody has some time to spare on this, it would be gladly appreciated.

@cmhayes
Copy link

cmhayes commented Jun 27, 2016

+1 for adding a feature similar to @Qwerios solution

@jvkumar
Copy link

jvkumar commented Jul 17, 2016

+1 for this feature

@robrez
Copy link

robrez commented Oct 10, 2016

The approach outlined by @Qwerios here #818 (comment) no longer works in swagger-ui-2.2.5

Authorization input has been broken out into a dialog window

The documentation here does not reflect that change: https://github.com/swagger-api/swagger-ui

Adding a header to every request per the docs also does not work for me:

If you have some header parameters which you need to send with every request, use the headers as below:

swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "XXXX", "header"));

@ilmoraunio
Copy link

ilmoraunio commented Jan 19, 2017

Adding just Bearer <token> as API key seemed to work for me.

Disclaimer: I use Clojure's metosin/ring-swagger-ui version 2.1.4-0 and override its base index.html with swagger-api/swagger-ui's index.html from version 2.2.8 in my resources folder.

@robrez
Copy link

robrez commented Jan 19, 2017

I've recently had great success using a legitimate AuthorizationCode flow initiated by swaggerUI. If your token is provided by an authorization server that correctly implements the flow, I'm sure this is the best option.

I've not directly tested implicit or password grants, but IIRC I've seen documentation stating they are supported

@curtisgibby
Copy link
Contributor

The OpenAPI spec now has a merged PR that adds support for bearer/JWT authentication. They plan to release this as an Implementer Draft on February 28, 2017.

After that, I assume that the next step would be for this project to implement @K1ll3rF0x 's PR (or similar) to add support on this side for the newly official JWT/bearer authentication.

@webron
Copy link
Contributor

webron commented Jan 27, 2017

The next step would be for this tool to support the entire spec, yes, which would include support for JWT/bearer authentication.

@ryan-barker-zefr
Copy link

Any update on this?

@webron
Copy link
Contributor

webron commented Apr 18, 2017

We're working on 3.0 support, but it's going to take a while, especially to support the entire spec.

@pqzxc
Copy link

pqzxc commented May 5, 2017

In the meantime, has anyone come up with a temporary way of adding JWT based authentication to Swagger UI 3.0.8?

@webron
Copy link
Contributor

webron commented Jun 9, 2017

Closing this ticket out. The comments have a suggested solution for the previous version.

@pqzxc we're working on bringing back a similar functionality to the current version.

3.0 support is in process. Right now we're working only on the rendering, but when the try it out functionality is introduced, the jwt support will be included in it.

@gabzim
Copy link

gabzim commented Jul 6, 2017

@webron is there any issue we can follow (since this is closed) so that we know when JWT is supported in the UI?

Maybe we could keep this one open so that we can track the status of this feature in any way. I'm waiting for this to be resolved to migrate my company's projects to Swagger.

@pqzxc
Copy link

pqzxc commented Jul 7, 2017

^ +1

@webron

@webron
Copy link
Contributor

webron commented Oct 18, 2017

For those of you who are migrating to OAS3 and use JWT... take a look at #3641 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests