swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Oauth2 password flow

Open reginaldlouis opened this issue 11 years ago • 43 comments

Hi. I'm trying to use the oauth2 feature with password flow from swagger 2.0 spec. I dont' know if I'm doing thing wrong or if there some missing stuff, but let me explain. This is my spec:

swagger: '2.0'

info:
  title: BakPak API
  description: BakPak server API
  version: '1.0.0'

host: localhost:5000
basePath: /api/v1

schemes:
  - http
  - https

consumes:
  - application/json

produces:
  - application/json

securityDefinitions:
  basicPassword:
    type: oauth2
    flow: password
    tokenUrl: http://localhost:5000/api/v1/token
    scopes:
      s1: Scope 1
      s2: Scope 2

paths:
  /token:
    get:
      tags:
        - auth
      summary: Get an authentication token
      description: Get an authentication token
      operationId: GetToken
      responses:
        '200':
          description: ""
        default:
          description: Operation failed.
  /test:
    get:
      summary: Test endpoint
      description: Test endpoint
      responses:
        '200':
          description: ""
        default:
          description: Operation failed.
      security:
        - basicPassword:
          - s1
          - s2  
  /user:
    post:
      tags:
        - user
      summary: Add a user
      description: Add a user
      operationId: AddUser
      parameters:
        - name: body
          in: body
          description: Type can be `bp`, `facebook` or `google`.
          schema:
            $ref: '#/definitions/Credentials'
      responses:
        '200':
          description: ""
        default:
          description: Operation failed.

definitions:
  Credentials:
    properties:
      type:
        type: string
      username:
        type: string
      password:
        type: string
      token:
        type: string

I have a /test endpoint just to test oauth2. I remove the comment in index.html to enable oauth2 flow:

            initOAuth({
              clientId: "your-client-id",
              realm: "your-realms",
              appName: "your-app-name"
            });

When I click on the "on/off" button to authorize, a new windows browser is opened with the following url http://localhost:5000/swagger-ui/null&redirect_uri=http://localhost:5000/swagger-ui/o2c.html&realm=your-realms&client_id=your-client-id&scope=s1,s2.

Note the null in the url. It is normal? I'm not super familiar with every aspect of oauth, but I was expecting a popup for username/password and somehow receiving at some endpoint a POST with a application/x-www-form-urlencoded in the request body like this: grant_type=password&username=johndoe&password=A3ddj3w

reference: http://tools.ietf.org/html/rfc6749#section-4.3.2

reginaldlouis avatar Jan 06 '15 02:01 reginaldlouis

Right now, the UI only supports the implicit flow for OAuth2, and even that was a (great) community contribution. If you think you can help us to add support for the password flow, that would be great.

webron avatar Jan 07 '15 20:01 webron

@webron I'd like to submit a pull-request to fix some of the issues with oauth2 security settings, to get it to work with the 2.0 spec. I've already identified and fixed a few minor things in the swagger-oauth.js file. The read.me file says to only make changes to the coffeescript files, but i don't see one for the swagger-oauth.js file. From what I can tell the coffeescript files only generate the swagger-ui.js file. Am I missing something? How should I proceed? Thanks.

bshamblen avatar Jan 09 '15 00:01 bshamblen

Thanks @bshamblen. The readme is indeed out of date then--if you're going to help contribute support to the 2.0 spec support, please do so in the develop_2.0 branch and try to include tests as possible. It's tough with oauth to do so, but let's do our best. I'll get the readme updated.

fehguy avatar Jan 09 '15 18:01 fehguy

@bshamblen - we'd love to add this feature, and obviously it's a matter of priorities. I remember you've mentioned a few months ago that you're going to be occupied, and was wondering whether your time has cleared out a bit. We'd love a PR for this feature.

webron avatar May 04 '15 12:05 webron

@webron - Unfortunately I'm still in full on startup mode right now, and probably won't have any free time for at least another few months. I had to make significant changes to the architecture of the Swagger UI files to get it to work within a Meteor app (not my choice). Since Meteor wraps each js file in its own container I had to parameterize any global variables that were called between files, and basically rewrite the oauth class to make it an object. Meteor also uses a different version of Handlebars, which caused errors when it tried to render the main UI page inside a Meteor template. Since it was such a huge change to the way the code was structured I didn't think you'd want me submitting a PR. But now that I have it working for my particular use case I don't know if I want to mess with it right now.

If case someone else wants to take on adding another flow type, like password, there are a lot of conditional if statements if the oauth file that check to see if the flow is implicit or accessCode. This logic could get out of hand with more flow types, so it should probably be refactored a little to isolate each flow's unique logic.

bshamblen avatar May 04 '15 15:05 bshamblen

@bshamblen - no worries, you've done a lot already. Thanks for the elaborate input.

webron avatar May 04 '15 15:05 webron

has there been any progress here or does swagger-ui still only support implicit?

bandtank avatar Jun 11 '15 16:06 bandtank

@bandtank - thanks to @bshamblen we also support the access code flow. At the moment, there's no progress with the password flow though.

webron avatar Jun 11 '15 16:06 webron

+1

andrassy avatar Jul 17 '15 09:07 andrassy

+1

aidapsibr avatar Oct 07 '15 15:10 aidapsibr

+1

milanov avatar Oct 23 '15 13:10 milanov

I would like to contribute adding the password flow on Swagger UI, and I would like try to change the authentication screen adding a combobox with the grant types specified by the user.

ismaelgcosta avatar Nov 05 '15 18:11 ismaelgcosta

Would be greatly appreciated @mundodojava

fehguy avatar Nov 06 '15 00:11 fehguy

Which branch I can fork to work?

ismaelgcosta avatar Nov 06 '15 11:11 ismaelgcosta

+1

BernieN avatar Nov 23 '15 17:11 BernieN

@bshamblen Would you mind publishing the souse code for modified Swagger UI that could work within Meteor App. We are facing similar issues that you've described. Related issue - #1757.

If you could publish modified source code or even create a separate package for meteor that would be really helpful.

frenchbread avatar Nov 24 '15 07:11 frenchbread

+1

markwalsh-liverpool avatar Feb 17 '16 19:02 markwalsh-liverpool

+1

jsmusgrave avatar Feb 17 '16 20:02 jsmusgrave

+1

jeancloudvanlang avatar Mar 01 '16 16:03 jeancloudvanlang

+1

Joe-Palmer avatar Mar 31 '16 11:03 Joe-Palmer