0

I am trying to use the NelmioApiDocBundle for a Symfony 3.4 projects API documentation, while also trying to wrap my head around OAuth 2 authorization for the project API access to begin with.

So far I've followed this tutorial on how to get FOSOAuthServerBundle working. So far I can 1.) create a client using the command line command:

php bin/console fos:oauth-server:create-client --redirect-uri="___" --grant-type="authorization_code" --grant-type="password" --grant-type="refresh_token" --grant-type="token" --grant-type="client_credentials"

2.) I can also get an access token manually by visiting this url on my server

http://127.0.0.1:8000/oauth/v2/token?client_id=______&client_secret=________&grant_type=client_credentials

3.) I can use the token to access areas of my Symfony project requiring OAuth Access by including the token in a GET parameter

However, in the NelmioApiDocBundle Authorizations I cannot get this to work to completion. Here is a screenshot:

NelmioApiDocBundle Authorization page

If enter my client_id and secret key it takes me to the Login Page, as expected. I can enter my login information and in takes me to the Approve or Deny Page, as expected. At this point if I click either Approve or Deny it tries to use a "redirect_uri" of http://localhost:3200/oauth2-redirect.html. No matter what I do I cannot change the redirect URI.

How to I get the a proper redirect URI?

MEmerson
  • 772
  • 1
  • 6
  • 17

1 Answers1

5

Ok, this was actually easily fixed. You need to add a single line:

oauth2RedirectUrl: 'URLhere',

to the file init-swagger-ui.js which is located (Symfony 3.4) in web/bundles/nelmioapidoc/

The final file ended up looking like this:

window.onload = () => {
  const data = JSON.parse(document.getElementById('swagger-data').innerText);
  const ui = SwaggerUIBundle({
      oauth2RedirectUrl: 'URLhere',
    spec: data.spec,
    dom_id: '#swagger-ui',
    validatorUrl: null,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: 'StandaloneLayout'
  });

  window.ui = ui;
};

Also you likely are going to want to download the file oauth2-redirect.html from the Swagger project to include for the actual redirect.

MEmerson
  • 772
  • 1
  • 6
  • 17