-
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes basic auth example doc; adds apikey example (#894)
* fixes basic auth example doc; adds apikey example * remove required_scopes references that dont pertain to apikey
- Loading branch information
Showing
4 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
======================= | ||
API Key Example | ||
======================= | ||
|
||
Running: | ||
|
||
.. code-block:: bash | ||
$ sudo pip3 install --upgrade connexion[swagger-ui] # install Connexion from PyPI | ||
$ ./app.py | ||
Now open your browser and go to http://localhost:8080/ui/ to see the Swagger UI. | ||
|
||
The hardcoded apikey is `asdf1234567890`. | ||
|
||
Test it out (in another terminal): | ||
|
||
.. code-block:: bash | ||
$ curl -H 'X-Auth: asdf1234567890' http://localhost:8080/secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
Basic example of a resource server | ||
''' | ||
|
||
import connexion | ||
from connexion.exceptions import OAuthProblem | ||
|
||
TOKEN_DB = { | ||
'asdf1234567890': { | ||
'uid': 100 | ||
} | ||
} | ||
|
||
|
||
def apikey_auth(token, required_scopes): | ||
info = TOKEN_DB.get(token, None) | ||
|
||
if not info: | ||
raise OAuthProblem('Invalid token') | ||
|
||
return info | ||
|
||
|
||
def get_secret(user) -> str: | ||
return "You are {user} and the secret is 'wbevuec'".format(user=user) | ||
|
||
|
||
if __name__ == '__main__': | ||
app = connexion.FlaskApp(__name__) | ||
app.add_api('openapi.yaml') | ||
app.run(port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: API Key Example | ||
version: '1.0' | ||
paths: | ||
/secret: | ||
get: | ||
summary: Return secret string | ||
operationId: app.get_secret | ||
responses: | ||
'200': | ||
description: secret response | ||
content: | ||
'*/*': | ||
schema: | ||
type: string | ||
security: | ||
- api_key: [] | ||
components: | ||
securitySchemes: | ||
api_key: | ||
type: apiKey | ||
name: X-Auth | ||
in: header | ||
x-apikeyInfoFunc: app.apikey_auth |