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

[feature][python] Support aliasing of API keys #6469

Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2b80b8a
[python] Support aliasing of API keys
jirikuncar May 28, 2020
1b4250c
Merge remote-tracking branch 'upstream/master' into jirikuncar/python…
jirikuncar Jun 2, 2020
93a7223
Support for aliasing and prefix
jirikuncar Jun 2, 2020
7ae3dc6
Make more realistic usage
jirikuncar Jun 2, 2020
5b8f65f
Regenerate
jirikuncar Jun 2, 2020
0637df2
Merge remote-tracking branch 'upstream/master' into jirikuncar/python…
jirikuncar Jun 3, 2020
116fa25
Document alias in generated code
jirikuncar Jun 3, 2020
eb6557e
Support override of aliased keys
jirikuncar Jun 3, 2020
49a4c3d
Use diferent id and name for api keys
jirikuncar Jun 3, 2020
185650a
ensure up-to-date
jirikuncar Jun 3, 2020
c5710b7
Simple example without x-auth-id-alias
jirikuncar Jun 3, 2020
899e1f4
regenerate docs
jirikuncar Jun 3, 2020
12e4f9d
Merge remote-tracking branch 'upstream/master' into jirikuncar/python…
jirikuncar Jun 4, 2020
b2d82c1
Regenerate
jirikuncar Jun 4, 2020
ea2706d
Provide separate spec for x-auth-id-alias
jirikuncar Jun 4, 2020
e8cdc89
Apply suggestions from code review
jirikuncar Jun 4, 2020
3d5f7b7
Merge remote-tracking branch 'upstream/master' into jirikuncar/python…
jirikuncar Jun 5, 2020
e770aad
regenerated
jirikuncar Jun 5, 2020
5527702
Merge remote-tracking branch 'upstream/master' into jirikuncar/python…
jirikuncar Jun 5, 2020
2502a2c
Merge remote-tracking branch 'upstream/master' into jirikuncar/python…
jirikuncar Jun 8, 2020
a7a9183
Merge remote-tracking branch 'upstream/master' into jirikuncar/python…
jirikuncar Jun 11, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bin/openapi3/python-experimental-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ yaml="modules/openapi-generator/src/test/resources/3_0/python-experimental/petst
ags="generate -t modules/openapi-generator/src/main/resources/python -i $yaml -g python-experimental -o samples/openapi3/client/petstore/python-experimental/ --additional-properties packageName=petstore_api $@"

java $JAVA_OPTS -jar $executable $ags

yaml="modules/openapi-generator/src/test/resources/3_0/extensions/x-auth-id-alias.yaml"
ags="generate -t modules/openapi-generator/src/main/resources/python -i $yaml -g python-experimental -o samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/ --additional-properties packageName=x_auth_id_alias $@"
spacether marked this conversation as resolved.
Show resolved Hide resolved
spacether marked this conversation as resolved.
Show resolved Hide resolved

java $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,16 @@ conf = {{{packageName}}}.Configuration(
self.__logger_format = value
self.logger_formatter = logging.Formatter(self.__logger_format)

def get_api_key_with_prefix(self, identifier):
def get_api_key_with_prefix(self, identifier, alias=None):
"""Gets API key (with prefix if set).

:param identifier: The identifier of apiKey.
:param alias: The alternative identifier of apiKey.
:return: The token for api key authentication.
"""
if self.refresh_api_key_hook is not None:
self.refresh_api_key_hook(self)
key = self.api_key.get(identifier)
key = self.api_key.get(identifier, self.api_key.get(alias) if alias is not None else None)
if key:
prefix = self.api_key_prefix.get(identifier)
if prefix:
Expand Down Expand Up @@ -462,12 +463,15 @@ conf = {{{packageName}}}.Configuration(
auth = {}
{{#authMethods}}
{{#isApiKey}}
if '{{keyParamName}}' in self.api_key:
if '{{name}}' in self.api_key{{#vendorExtensions.x-auth-id-alias}} or '{{.}}' in self.api_key{{/vendorExtensions.x-auth-id-alias}}:
auth['{{name}}'] = {
'type': 'api_key',
'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
'key': '{{keyParamName}}',
'value': self.get_api_key_with_prefix('{{keyParamName}}')
'value': self.get_api_key_with_prefix(
'{{name}}',{{#vendorExtensions.x-auth-id-alias}}
alias='{{.}}',{{/vendorExtensions.x-auth-id-alias}}
spacether marked this conversation as resolved.
Show resolved Hide resolved
),
}
{{/isApiKey}}
{{#isBasic}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ configuration = {{{packageName}}}.Configuration(
configuration = {{{packageName}}}.Configuration(
host = "{{{basePath}}}",
api_key = {
'{{{keyParamName}}}': 'YOUR_API_KEY'
'{{name}}': 'YOUR_API_KEY'
}
)
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'
# configuration.api_key_prefix['{{name}}'] = 'Bearer'
{{/isApiKey}}
{{#isOAuth}}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
openapi: 3.0.0
info:
description: This specification shows how to use x-auth-id-alias extension for API keys.
version: 1.0.0
title: OpenAPI Extension x-auth-id-alias
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
tags:
- name: usage
description: Show usage of x-auth-id-alias
components:
securitySchemes:
api_key:
type: apiKey
name: X-Api-Key
in: header
api_key_query:
type: apiKey
name: api_key
# Test key aliasing
x-auth-id-alias: api_key
in: query
paths:
/both:
get:
tags:
- usage
summary: Use both API keys
description: Use both API keys
operationId: bothKeys
security:
- api_key_query: []
api_key: []
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
/any:
get:
tags:
- usage
summary: Use any API key
description: Use any API key
operationId: anyKey
security:
- api_key_query: []
- api_key: []
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
/query:
get:
tags:
- usage
summary: Use API key in query
description: Use API key in query
operationId: keyInQuery
security:
- api_key_query: []
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
/header:
get:
tags:
- usage
summary: Use API key in header
description: Use API key in header
operationId: keyInHeader
security:
- api_key: []
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: object
servers:
- url: 'http://{server}.swagger.io:{port}/v2'
description: petstore server
variables:
server:
enum:
- 'petstore'
- 'qa-petstore'
- 'dev-petstore'
default: 'petstore'
port:
enum:
- 80
- 8080
default: 80
- url: https://localhost:8080/{version}
description: The local server
variables:
version:
enum:
- 'v1'
- 'v2'
default: 'v2'
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,16 @@ def logger_format(self, value):
self.__logger_format = value
self.logger_formatter = logging.Formatter(self.__logger_format)

def get_api_key_with_prefix(self, identifier):
def get_api_key_with_prefix(self, identifier, alias=None):
"""Gets API key (with prefix if set).

:param identifier: The identifier of apiKey.
:param alias: The alternative identifier of apiKey.
:return: The token for api key authentication.
"""
if self.refresh_api_key_hook is not None:
self.refresh_api_key_hook(self)
key = self.api_key.get(identifier)
key = self.api_key.get(identifier, self.api_key.get(alias) if alias is not None else None)
if key:
prefix = self.api_key_prefix.get(identifier)
if prefix:
Expand Down Expand Up @@ -387,14 +388,18 @@ def auth_settings(self):
'type': 'api_key',
'in': 'header',
'key': 'api_key',
'value': self.get_api_key_with_prefix('api_key')
'value': self.get_api_key_with_prefix(
'api_key',
),
}
if 'api_key_query' in self.api_key:
auth['api_key_query'] = {
'type': 'api_key',
'in': 'query',
'key': 'api_key_query',
'value': self.get_api_key_with_prefix('api_key_query')
'value': self.get_api_key_with_prefix(
'api_key_query',
),
}
if self.username is not None and self.password is not None:
auth['http_basic_test'] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
venv/
.venv/
.python-version
.pytest_cache

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

#Ipython Notebook
.ipynb_checkpoints
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ref: https://docs.gitlab.com/ee/ci/README.html

stages:
- test

.nosetest:
stage: test
script:
- pip install -r requirements.txt
- pip install -r test-requirements.txt
- pytest --cov=x_auth_id_alias

nosetest-2.7:
extends: .nosetest
image: python:2.7-alpine
nosetest-3.3:
extends: .nosetest
image: python:3.3-alpine
nosetest-3.4:
extends: .nosetest
image: python:3.4-alpine
nosetest-3.5:
extends: .nosetest
image: python:3.5-alpine
nosetest-3.6:
extends: .nosetest
image: python:3.6-alpine
nosetest-3.7:
extends: .nosetest
image: python:3.7-alpine
nosetest-3.8:
extends: .nosetest
image: python:3.8-alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.gitignore
.gitlab-ci.yml
.openapi-generator-ignore
.travis.yml
README.md
docs/UsageApi.md
git_push.sh
requirements.txt
setup.cfg
setup.py
test-requirements.txt
test/__init__.py
test/test_usage_api.py
tox.ini
x_auth_id_alias/__init__.py
x_auth_id_alias/api/__init__.py
x_auth_id_alias/api/usage_api.py
x_auth_id_alias/api_client.py
x_auth_id_alias/apis/__init__.py
x_auth_id_alias/configuration.py
x_auth_id_alias/exceptions.py
x_auth_id_alias/model/__init__.py
x_auth_id_alias/model_utils.py
x_auth_id_alias/models/__init__.py
x_auth_id_alias/rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ref: https://docs.travis-ci.com/user/languages/python
language: python
python:
- "2.7"
- "3.2"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
# command to install dependencies
install:
- "pip install -r requirements.txt"
- "pip install -r test-requirements.txt"
# command to run tests
script: pytest --cov=x_auth_id_alias
Loading