swagger-api / swagger-api/swagger-ui
Preauthorization methods are confusing
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 29k
- Forks
- 9.3k
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 25
Description
| Q | A |
|---|---|
| Bug or feature request? | Usability Improvement |
| Which Swagger/OpenAPI version? | Any |
| Which Swagger-UI version? | 3.13.1 |
| How did you install Swagger-UI? | n/a |
| Which browser & version? | n/a |
| Which operating system? | n/a |
Description
In cases where authorization data is fetched from a remote source (e.g. loading a definition by URL), preauthorization methods do not work until the definition is in memory.
This is due to the auth state being designed so that a security definition must be stored in state alongside authorization information for that definition. That definition is not available at synchronous call time, so we're unable to open up the preauthorization methods to a simpler usage pattern.
Expected Behavior
I should be able to call a preauthorization method immediately after creating my Swagger-UI instance that points to a URL:
const ui = SwaggerUI({
dom_id: '#myDomId',
url: "http://petstore.swagger.io/v2/swagger.json"
})
ui.preauthorizeApiKey("MyAuth", "MyApiKey1234")
Current Behavior
I must place my preauthorization inside of onComplete when I'm fetching a definition by URL:
const ui = SwaggerUI({
dom_id: '#myDomId',
url: "http://petstore.swagger.io/v2/swagger.json"
onComplete: () => {
ui.preauthorizeApiKey("MyAuth", "MyApiKey1234")
}
})
Not doing this results in the data being lost, thanks to this code:
https://github.com/swagger-api/swagger-ui/blob/master/src/core/plugins/auth/index.js#L62
Possible Solution
Refactor the auth state system to hold an auth data lookup table that is keyed by name+type, and does not depend on the definition itself being stored in state.
May also want to find a way to flush auth data after another definition is loaded later.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/core/plugins/auth/index.js at the referenced state-handling code, then trace how preauthorizeApiKey is called before and after a definition loaded by URL. Compare the immediate-call and onComplete examples to understand the lost authorization data. Done means preauthorization remains available when called immediately after creating SwaggerUI with a URL, while later-loaded definitions are handled safely.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- authentication, frontend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100