Skip to content

Commit

Permalink
support OAuth2 config
Browse files Browse the repository at this point in the history
  • Loading branch information
PWZER committed Dec 8, 2021
1 parent d0f9f78 commit e1be155
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,27 @@ python3 -c "from swagger_ui import supported_list; print(supported_list)"
api_doc(app, config_path='./config/test.yaml', parameters=parameters)
```

For details about configuration parameters, see the official documentation [Configuration](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/).
For details about parameters configuration, see the official documentation [Parameters Configuration](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/).

## OAuth2 Configuration

The format is similar to `parameters`.

```python
oauth2_config = {
"clientId": "\"your-client-id\"",
"clientSecret": "\"your-client-secret-if-required\"",
"realm": "\"your-realms\"",
"appName": "\"your-app-name\"",
"scopeSeparator": "\" \"",
"scopes": "\"openid profile\"",
"additionalQueryStringParams": "{test: \"hello\"}",
"usePkceWithAuthorizationCodeGrant": True,
}
api_doc(app, config_path='./config/test.yaml', oauth2_config=oauth2_config)
```

For details about OAuth2 configuration, see the official documentation [OAuth2 Configuration](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/).

## Swagger UI
Swagger UI version is `v4.1.2`. see [https://github.com/swagger-api/swagger-ui](https://github.com/swagger-api/swagger-ui).
Expand Down
5 changes: 5 additions & 0 deletions swagger_ui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self,
title='API doc',
editor=False,
parameters={},
oauth2_config={},
**extra_config):
self.app = app
self.app_type = app_type
Expand All @@ -55,6 +56,9 @@ def __init__(self,
self.parameters.update(parameters)
self.parameters["url"] = "\"{}\"".format(self.swagger_json_uri_absolute)

# oauth2_config
self.oauth2_config = oauth2_config

self.env = Environment(
loader=FileSystemLoader(
str(SWAGGER_UI_PY_ROOT.joinpath('templates'))),
Expand All @@ -76,6 +80,7 @@ def doc_html(self):
title=self.title,
config_url=self.swagger_json_uri_absolute,
parameters=self.parameters,
oauth2_config=self.oauth2_config,
)

@property
Expand Down
Binary file modified swagger_ui/static/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified swagger_ui/static/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 15 additions & 9 deletions swagger_ui/templates/doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@
<script src="{{ url_prefix }}/static/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
{%- for key, value in parameters.items() %}
{{ key|safe }}: {{ value|safe }},
{%- endfor %}
});
// End Swagger UI call region
const ui = SwaggerUIBundle({
{%- for key, value in parameters.items() %}
{{ key|safe }}: {{ value|safe }},
{%- endfor %}
});

window.ui = ui;
};
{% if oauth2_config %}
ui.initOAuth({
{%- for key, value in oauth2_config.items() %}
{{ key|safe }}: {{ value|safe }},
{%- endfor %}
});
{% endif %}

window.ui = ui;
};
</script>
</body>
</html>
26 changes: 20 additions & 6 deletions tools/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@
SWAGGER_EDITOR_REPO = 'swagger-api/swagger-editor'


DOC_HTML_JAVASCRIPT = '''window.onload = function() {
const ui = SwaggerUIBundle({
{%- for key, value in parameters.items() %}
{{ key|safe }}: {{ value|safe }},
{%- endfor %}
});
{% if oauth2_config %}
ui.initOAuth({
{%- for key, value in oauth2_config.items() %}
{{ key|safe }}: {{ value|safe }},
{%- endfor %}
});
{% endif %}
window.ui = ui;
};'''


def detect_latest_release(repo):
print('detect latest release')
resp = requests.get('https://api.github.com/repos/{}/releases/latest'.format(repo), timeout=120)
Expand Down Expand Up @@ -101,12 +120,7 @@ def replace_html_content():
if str(html_path).endswith('doc.html'):
html_content = re.sub(r'https://petstore.swagger.io/v[1-9]/swagger.json',
'{{ config_url }}', html_content)
content = '''const ui = SwaggerUIBundle({
{%- for key, value in parameters.items() %}
{{ key|safe }}: {{ value|safe }},
{%- endfor %}
});'''
html_content = re.sub(r'const ui = SwaggerUIBundle\({.*}\);$', content,
html_content = re.sub(r'window.onload = function\(\) {.*};$', DOC_HTML_JAVASCRIPT,
html_content, flags=re.MULTILINE | re.DOTALL)

with html_path.open('w') as html_file:
Expand Down

0 comments on commit e1be155

Please sign in to comment.