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 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,15 @@ 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.
: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 +462,15 @@ conf = {{{packageName}}}.Configuration(
auth = {}
{{#authMethods}}
{{#isApiKey}}
if '{{keyParamName}}' in self.api_key:
if '{{#vendorExtensions.x-auth-id-alias}}{{.}}{{/vendorExtensions.x-auth-id-alias}}{{^vendorExtensions.x-auth-id-alias}}{{name}}{{/vendorExtensions.x-auth-id-alias}}' in self.api_key:
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(
'{{keyParamName}}',
alias='{{#vendorExtensions.x-auth-id-alias}}{{.}}{{/vendorExtensions.x-auth-id-alias}}{{^vendorExtensions.x-auth-id-alias}}{{name}}{{/vendorExtensions.x-auth-id-alias}}',
),
}
{{/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'
'{{#vendorExtensions.x-auth-id-alias}}{{{.}}}{{/vendorExtensions.x-auth-id-alias}}{{^vendorExtensions.x-auth-id-alias}}{{{name}}}{{/vendorExtensions.x-auth-id-alias}}': 'YOUR_API_KEY'
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
}
)
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['{{{keyParamName}}}'] = 'Bearer'
# configuration.api_key_prefix['{{#vendorExtensions.x-auth-id-alias}}{{{.}}}{{/vendorExtensions.x-auth-id-alias}}{{^vendorExtensions.x-auth-id-alias}}{{{name}}}{{/vendorExtensions.x-auth-id-alias}}'] = 'Bearer'
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
{{/isApiKey}}
{{#isOAuth}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ paths:
summary: To test "client" model
description: To test "client" model
operationId: testClientModel
security:
- api_key_query: []
api_key: []
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
responses:
'200':
description: successful operation
Expand Down Expand Up @@ -1164,6 +1167,8 @@ components:
api_key_query:
type: apiKey
name: api_key_query
# Test key aliasing
x-auth-id-alias: api_key
in: query
http_basic_test:
type: http
Expand Down Expand Up @@ -1443,7 +1448,7 @@ components:
maximum: 543.2
minimum: 32.1
type: number
multipleOf: 32.5
multipleOf: 32.5
float:
type: number
format: float
Expand Down Expand Up @@ -1969,7 +1974,7 @@ components:
# Here the additional properties are specified using a referenced schema.
# This is just to validate the generated code works when using $ref
# under 'additionalProperties'.
$ref: '#/components/schemas/fruit'
$ref: '#/components/schemas/fruit'
Shape:
oneOf:
- $ref: '#/components/schemas/Triangle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ To test \"client\" model

### Example

* Api Key Authentication (api_key):
* Api Key Authentication (api_key_query):
```python
from __future__ import print_function
import time
Expand All @@ -477,9 +479,33 @@ configuration = petstore_api.Configuration(
host = "http://petstore.swagger.io:80/v2"
)

# The client must configure the authentication and authorization parameters
# in accordance with the API server security policy.
# Examples for each auth method are provided below, use the example that
# satisfies your auth use case.

# Configure API key authorization: api_key
configuration = petstore_api.Configuration(
host = "http://petstore.swagger.io:80/v2",
api_key = {
'api_key': 'YOUR_API_KEY'
}
)
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key'] = 'Bearer'

# Configure API key authorization: api_key_query
configuration = petstore_api.Configuration(
host = "http://petstore.swagger.io:80/v2",
api_key = {
'api_key': 'YOUR_API_KEY'
}
)
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key'] = 'Bearer'

# Enter a context with an instance of the API client
with petstore_api.ApiClient() as api_client:
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = fake_api.FakeApi(api_client)
client_client = client.Client() # client.Client | client model
Expand All @@ -505,7 +531,7 @@ Name | Type | Description | Notes

### Authorization

No authorization required
[api_key](../README.md#api_key), [api_key_query](../README.md#api_key_query)

### HTTP request headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ configuration = petstore_api.Configuration(
configuration = petstore_api.Configuration(
host = "http://petstore.swagger.io:80/v2",
api_key = {
'api_key_query': 'YOUR_API_KEY'
'api_key': 'YOUR_API_KEY'
}
)
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key_query'] = 'Bearer'
# configuration.api_key_prefix['api_key'] = 'Bearer'

# Enter a context with an instance of the API client
with petstore_api.ApiClient(configuration) as api_client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,10 @@ def __test_client_model(
self.test_client_model = Endpoint(
settings={
'response_type': (client.Client,),
'auth': [],
'auth': [
'api_key',
'api_key_query'
],
'endpoint_path': '/fake',
'operation_id': 'test_client_model',
'http_method': 'PATCH',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,15 @@ 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):
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
"""Gets API key (with prefix if set).

:param identifier: The 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 @@ -438,14 +438,20 @@ 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',
alias='api_key',
),
}
if 'api_key_query' in self.api_key:
if 'api_key' 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',
alias='api_key',
),
}
if self.access_token is not None:
auth['bearer_test'] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from collections import namedtuple

import petstore_api
from petstore_api.api import FakeApi
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved
from petstore_api.model import format_test
import petstore_api.configuration

Expand All @@ -43,6 +44,31 @@ def test_configuration(self):
config.disabled_client_side_validations = ""


def test_x_auth_id_alias(self):
configuration = petstore_api.Configuration(
host='http://localhost/',
api_key={
'api_key': 'SECRET_VALUE'
},
api_key_prefix={
'api_key': 'PREFIX'
}
)

def request(*args, **kwargs):
assert ('api_key_query', 'SECRET_VALUE') in kwargs['query_params']
assert 'PREFIX SECRET_VALUE' == kwargs['headers']['api_key']
raise RuntimeError("passed")

with petstore_api.ApiClient(configuration) as client:
api = FakeApi(client)
client.request = request
try:
api.test_client_model(client_client={})
except RuntimeError as e:
assert "passed" == str(e)
jirikuncar marked this conversation as resolved.
Show resolved Hide resolved


def checkRaiseRegex(self, expected_exception, expected_regex):
if sys.version_info < (3, 0):
return self.assertRaisesRegexp(expected_exception, expected_regex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,15 @@ 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.
: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 @@ -438,14 +438,20 @@ 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',
alias='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',
alias='api_key_query',
),
}
if self.access_token is not None:
auth['bearer_test'] = {
Expand Down