api-platform / api-platform/core

Swagger UI pre-authorize feature

Open
#8,079 1 comment 4 reactions 0 assignees View on GitHub
OpenAPI
Dominant language
PHP
Stars
2.6k
Forks
980
Avg merge
2d 5h
Merged PRs (30d)
48

Description

**Description**

Allow API Platform configuration to specify an API Key to be pre-filled in Swagger UI.
Swagger UI documentation (https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/) states that a method `preauthorizeApiKey` can be used to set default API KEY / Bearer token.

At the moment, the only way I found to implement this behavior was to override the Swagger UI template, and to override the `javascript` block with the following code :

```
{% block javascript %}
{{ parent() }}


const data = JSON.parse(document.getElementById('swagger-data').innerText);
const apiKey = data.extraConfiguration.apiKey;

let swaggerUiTimeout = window.setTimeout(preAuthorizeSwaggerUi, 100);
function preAuthorizeSwaggerUi() {
if (window.swaggerUI !== undefined) {
window.swaggerUI.preauthorizeApiKey("apiKey", apiKey);
window.clearTimeout(swaggerUiTimeout);
} else {
swaggerUiTimeout = window.setTimeout(preAuthorizeSwaggerUi, 100);
}
}

{% endblock %}
```

It would be interesting to implement a custom event dispatched on the SwaggerUI onComplete method, or to allow this to be configured into the API Platform YML directly.

What do you think ? I can try to prepare a PR if people are interested into this feature.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.