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

SwaggerUI OAuth2 clientCredentials authentication #4533

Closed
jochenjonc opened this issue May 9, 2018 · 12 comments
Closed

SwaggerUI OAuth2 clientCredentials authentication #4533

jochenjonc opened this issue May 9, 2018 · 12 comments

Comments

@jochenjonc
Copy link

Q&A (please complete the following information)

  • OS: Windows
  • Browser: Chrome
  • Version: [e.g. 22]
  • Method of installation: [e.g. npm, dist assets]
  • Swagger-UI version: 3.13.2
  • Swagger/OpenAPI version: Swashbuckle.AspNetCore 2.4.0

Content & configuration

Example Swagger/OpenAPI definition:

"securityDefinitions":{  
      "oauth2":{  
         "flow":"application",
         "tokenUrl":"https://*****/oauth/token",
         "scopes":{  
            "read":"Read access"
         },
         "type":"oauth2"
      },
...
"security":[  
      {  
         "oauth2":[  
            "read"
         ]
      },

Swagger-UI configuration options:
None

Describe the bug you're encountering

I configured Swagger to use the oauth2 clientCredentials flow and I get the following screen in Swagger UI for authentication.

image

But when I fill in the client_id and client_secret and I press Authorize I get an error.

When I look at the the actual POST Swagger UI does I see the following issues.

  • In the header I see Content-Type: application/x-www-form-urlencoded instead of application/json
  • In the header I also have Authorization: Basic ****, but I wasn't expecting that, the client_id & client_secret should have been part of the body
  • In the body I only have grant_type: client_credentials & scope: read, and I'm missing the client_id & client_secret

I think it is a bug, but maybe I'm doing something wrong?

@shockey
Copy link
Contributor

shockey commented May 10, 2018

Hmm, this does seem strange.

Do you have a live server I can test this against, @jochenjonc?

@jochenjonc
Copy link
Author

@shockey I don't have a live server available

@jochenjonc
Copy link
Author

What I'm actually trying to do is use Auth0 for authentication in Swagger.

curl --request POST \
  --url 'https://***.eu.auth0.com/oauth/token' \
  --header 'content-type: application/json' \
  --data '{"grant_type":"client_credentials","client_id": "YOUR_CLIENT_ID","client_secret": "YOUR_CLIENT_SECRET","audience": "https://psfl.eu.auth0.com/api/v2/"}'

As you can see they use a json body to send all parameters. Is there a way to do this with swagger-ui?

@dgwaldo
Copy link

dgwaldo commented Jul 25, 2018

@jochenjonc Did you ever find a way around this?

Looking at the code it looks like the there isn't a simple way to do this without a modification to this class...
https://github.com/swagger-api/swagger-ui/blob/master/src/core/oauth2-authorize.js

I'm utilizing Swashbuckle with .Net Core and I can get my data onto the authConfigs object, but it does no good because oauth2-authorize.js doesn't pass that object as the json on the request...

c.OAuthAdditionalQueryStringParams(new { audience = _appConfig.Auth0Settings.Audience });

@jochenjonc
Copy link
Author

@dgwaldo I never got around this and I just use a Bearer Token I generate via the Auth0 website.

@shockey
Copy link
Contributor

shockey commented Jul 31, 2018

Closing - sounds like this is resolved.

I don't know much about Auth0 in particular, but if anyone has observations on how we can support it better in Swagger UI, feel free to open a feature request 😄

@shockey shockey closed this as completed Jul 31, 2018
@dgwaldo
Copy link

dgwaldo commented Aug 2, 2018

I did a bit of a hacky work around. http://waldoscode.blogspot.com/2018/07/using-swashbuckle-or-swagger-ui-with.html

@shockey, I think all that would be needed is for the json object being posted to allow for an audience to be passed... If I get time I might try and work something up.

@MattHartz
Copy link

Support for this would be ready nice

@soliveira
Copy link

I believe the issue is here:
https://github.com/swagger-api/swagger-ui/blob/054d450a45474cd8099622b73653349bea4ec7e9/src/core/plugins/auth/actions.js

It is missing the ClientId and ClientSecret in the form.

export const authorizeApplication = ( auth ) => ( { authActions } ) => {
  let { schema, scopes, name, clientId, clientSecret } = auth
  let headers = {
    Authorization: "Basic " + btoa(clientId + ":" + clientSecret)
  }
  let form = {
    grant_type: "client_credentials",
    scope: scopes.join(scopeSeparator)
  }

  return authActions.authorizeRequest({body: buildFormData(form), name, url: schema.get("tokenUrl"), auth, headers })
}

@shockey
Copy link
Contributor

shockey commented Nov 12, 2018

@soliveira, per the OAuth spec, client_id and client_secret shouldn't be included in the access token request body:

Quoting myself, quoting the spec:

Clients in possession of a client password MAY use the HTTP Basic authentication scheme as defined in [RFC2617] to authenticate with the authorization server [...] The authorization server MUST support the HTTP Basic authentication scheme for authenticating clients that were issued a client password.

Including the client credentials in the request-body using the two parameters is NOT RECOMMENDED and SHOULD be limited to clients unable to directly utilize the HTTP Basic authentication scheme (or other password-based HTTP authentication schemes).

Since Swagger UI is able to use HTTP basic to transmit the client credentials, we do that instead of including it in the request body.

See #4905 (comment) for more context.

@friendly-tech
Copy link

I am still having this issue, its an issue with not being able to pass the audience parameter in the request body that causes the error from auth0
"Non-global clients are not allowed access to APIv1"

Passing in the querystring doesn't appear to work.

Would be nice to have similar functionality as OAuthAdditionalQueryStringParams, something like OAuthAdditionalRequestParams

@shockey
Copy link
Contributor

shockey commented Jan 15, 2019

@crazyman1979, this is a resolved support ticket - please open a new issue if you're having problems!

@swagger-api swagger-api locked as resolved and limited conversation to collaborators Jan 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants