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

Schema is not reloaded on OAuth2 Authorization #1190

Open
BramEsposito opened this issue Feb 29, 2024 · 4 comments
Open

Schema is not reloaded on OAuth2 Authorization #1190

BramEsposito opened this issue Feb 29, 2024 · 4 comments

Comments

@BramEsposito
Copy link
Contributor

Describe the bug
I am running:

  • Django 4.2.10
  • django-oauth-toolkit 2.3.0
  • djangorestframework 3.14.0
  • drf-spectacular 0.27.1

I have configured drf-spectacular to authorize with OAuth2, which appears as an option in the Swagger-ui authorize popup.
However, after authorizing, the schema is not reloaded.

To Reproduce
my spectacular settings are as follows:

SPECTACULAR_SETTINGS = {
   "TITLE": "Your Project API",
    "DESCRIPTION": "Your project description",
    "VERSION": "1.0.0",
    "SERVE_INCLUDE_SCHEMA": False,
    "SERVE_PUBLIC": False,
    # OTHER SETTINGS
    "OAUTH2_FLOWS": ["clientCredentials"],
    "OAUTH2_AUTHORIZATION_URL": "/o/authorize/",
    "OAUTH2_TOKEN_URL": "/o/token/",
}

Expected behavior
I expect the schema to reload after authenticating successfully with OAuth2 as per the reloadSchemaOnAuthChange plugin

@tfranzel
Copy link
Owner

yes it should reload. not exactly sure why. I have only tested this for non-oauth cases myself.

  • is there something in the browser console? warnings?
  • has this ever worked before for you?
  • are you sure it is not reloading? maybe it is reloading but the credentials are not injected?

@BramEsposito
Copy link
Contributor Author

BramEsposito commented Feb 29, 2024

Wow, you respond faster than I anticipated. 😄

I am able to authorize and reload with other authorization methods, but not with OAuth

I have solved this for myself by customising the swagger_ui plugin and adding an additional actionwrapper:

const reloadSchemaOnAuthChange = () => {
  return {
    statePlugins: {
      auth: {
        wrapActions: {
// ADD THIS BLOCK
          authorizeOauth2:(ori) => (...args) => {
            schemaAuthFailed = false;
            setTimeout(() => ui.specActions.download());
            return ori(...args);
          },
// END OF ADDED BLOCK
          authorize: (ori) => (...args) => {
            schemaAuthFailed = false;
            setTimeout(() => ui.specActions.download());
            return ori(...args);
          },
          logout: (ori) => (...args) => {
            schemaAuthFailed = false;
            setTimeout(() => ui.specActions.download());
            return ori(...args);
          },
        },
      },
    },
  };
};

@tfranzel
Copy link
Owner

Excellent! Of course they used another method. doh.

Do you want to make a PR or should I add it when I come around to it?

@BramEsposito
Copy link
Contributor Author

I'll make a PR right away

tfranzel added a commit that referenced this issue Mar 4, 2024
…zation

Add a specific Action Wrapper for OAuth Authorization requests #1190
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants