Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b609c0c
Python: conditionally set auth attributes
sebastien-rosset Nov 25, 2019
7d379db
Python: conditionally set auth attributes
sebastien-rosset Nov 25, 2019
29ee6af
[Python] conditionally set auth attributes. Update unit tests
sebastien-rosset Nov 26, 2019
d68bfde
[Python] conditionally set auth attributes. Format python code
sebastien-rosset Nov 26, 2019
10e9b61
[Python] regenerate Python samples
sebastien-rosset Nov 26, 2019
5a17a9d
[Python] update unit test
sebastien-rosset Nov 26, 2019
7a119d9
[Python] run ./bin/python-petstore-all.sh
sebastien-rosset Nov 26, 2019
670551a
[Python] remove security scheme attribute
sebastien-rosset Nov 26, 2019
688e0ae
[Python] Address spacether PR comments, remove unnecessary test
sebastien-rosset Nov 26, 2019
26258b6
[Python] Address spacether PR comments, add test
sebastien-rosset Nov 26, 2019
65f2152
[Python] Address spacether PR comments
sebastien-rosset Nov 26, 2019
f1f2a72
[Python] Address spacether PR comments
sebastien-rosset Nov 26, 2019
fb7bc46
[Python] Add sample to configure API keys
sebastien-rosset Nov 26, 2019
e600852
[Python] fix unit test
sebastien-rosset Nov 26, 2019
221a87b
[Python] fix unit test for python experimental
sebastien-rosset Nov 26, 2019
f2d247b
[Python] fix unit test for python experimental
sebastien-rosset Nov 26, 2019
fd4a0e4
[Python] fix max length of line in python
sebastien-rosset Nov 26, 2019
eedcfeb
CSCvs20843: specify api_key as a dictionary argument
sebastien-rosset Dec 2, 2019
74b2393
Remove pprint that was used for debug purpose
sebastien-rosset Dec 3, 2019
8bdf9f3
Fix unit test
sebastien-rosset Dec 3, 2019
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 @@ -516,13 +516,10 @@ class ApiClient(object):
"""
if not auth_settings:
return

for auth in auth_settings:
auth_setting = self.configuration.auth_settings().get(auth)
if auth_setting:
if not auth_setting['value']:
continue
elif auth_setting['in'] == 'cookie':
if auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,39 @@ class Configuration(object):
Do not edit the class manually.

:param host: Base url
:param api_key: Dict to store API key(s)
:param api_key_prefix: Dict to store API prefix (e.g. Bearer)
:param api_key: Dict to store API key(s).
Each entry in the dict specifies an API key. The dict key is the name
of the api key, as specified in the OAS specification.
The dict value is itself a dict that must have a 'value' entry.
The 'value' entry is required and specifies the api key secret.
The 'prefix' entry is optional and specifies an api key prefix when
generating the auth data.
:param username: Username for HTTP basic authentication
:param password: Password for HTTP basic authentication
:param access_token: The OAuth2 access token

:Example:

Given the following security scheme in the OpenAPI specification:
components:
securitySchemes:
cookieAuth: # name for the security scheme
type: apiKey
in: cookie
name: JSESSIONID # cookie name

You can programmatically set the cookie:
conf = {{{packageName}}}.Configuration(
api_key={'JSESSIONID': {prefix='JSESSIONID=', value='abc123'}}
)
The following cookie will be added to the HTTP request:
Cookie: JSESSIONID=abc123
"""

def __init__(self, host="{{{basePath}}}",
api_key=None, api_key_prefix=None,
username="", password=""):
api_key=None,
username=None, password=None,
access_token=None):
"""Constructor
"""
self.host = host
Expand All @@ -45,11 +69,6 @@ class Configuration(object):
self.api_key = api_key
"""dict to store API key(s)
"""
self.api_key_prefix = {}
if api_key_prefix:
self.api_key_prefix = api_key_prefix
"""dict to store API prefix (e.g. Bearer)
"""
self.refresh_api_key_hook = None
"""function hook to refresh API key if expired
"""
Expand All @@ -59,18 +78,9 @@ class Configuration(object):
self.password = password
"""Password for HTTP basic authentication
"""
{{#hasOAuthMethods}}
self.access_token = ""
"""access token for OAuth/Bearer
"""
{{/hasOAuthMethods}}
{{^hasOAuthMethods}}
{{#hasBearerMethods}}
self.access_token = ""
self.access_token = access_token
"""access token for OAuth/Bearer
"""
{{/hasBearerMethods}}
{{/hasOAuthMethods}}
self.logger = {}
"""Logging Settings
"""
Expand Down Expand Up @@ -234,11 +244,13 @@ class Configuration(object):
"""
if self.refresh_api_key_hook is not None:
self.refresh_api_key_hook(self)
key = self.api_key.get(identifier)
if key:
prefix = self.api_key_prefix.get(identifier)
if prefix:
return "%s %s" % (prefix, key)
key_entry = self.api_key.get(identifier)
if not isinstance(key_entry, dict):
raise Exception("The api_key attribute must be a dictionary")
if key_entry:
key = key_entry["value"]
if "prefix" in key_entry:
return "%s%s" % (key_entry["prefix"], key)
else:
return key

Expand All @@ -247,60 +259,66 @@ class Configuration(object):

:return: The token for basic HTTP authentication.
"""
username = ""
if self.username is not None:
username = self.username
password = ""
if self.password is not None:
password = self.password
return urllib3.util.make_headers(
basic_auth=self.username + ':' + self.password
basic_auth=username + ':' + password
).get('authorization')

def auth_settings(self):
"""Gets Auth Settings dict for api client.

:return: The Auth Settings information dict.
"""
return {
auth = {}
{{#authMethods}}
{{#isApiKey}}
'{{name}}':
{
'type': 'api_key',
'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
'key': '{{keyParamName}}',
'value': self.get_api_key_with_prefix('{{keyParamName}}')
},
if '{{keyParamName}}' 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}}')
}
{{/isApiKey}}
{{#isBasic}}
{{^isBasicBearer}}
'{{name}}':
{
'type': 'basic',
'in': 'header',
'key': 'Authorization',
'value': self.get_basic_auth_token()
},
if self.username is not None and self.password is not None:
auth['{{name}}'] = {
'type': 'basic',
'in': 'header',
'key': 'Authorization',
'value': self.get_basic_auth_token()
}
{{/isBasicBearer}}
{{#isBasicBearer}}
'{{name}}':
{
'type': 'bearer',
'in': 'header',
{{#bearerFormat}}
'format': '{{{.}}}',
{{/bearerFormat}}
'key': 'Authorization',
'value': 'Bearer ' + self.access_token
},
if self.access_token is not None:
auth['{{name}}'] = {
'type': 'bearer',
'in': 'header',
{{#bearerFormat}}
'format': '{{{.}}}',
{{/bearerFormat}}
'key': 'Authorization',
'value': 'Bearer ' + self.access_token
}
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}
'{{name}}':
{
'type': 'oauth2',
'in': 'header',
'key': 'Authorization',
'value': 'Bearer ' + self.access_token
},
if self.access_token is not None:
auth['{{name}}'] = {
'type': 'oauth2',
'in': 'header',
'key': 'Authorization',
'value': 'Bearer ' + self.access_token
}
{{/isOAuth}}
{{/authMethods}}
}
return auth

def to_debug_report(self):
"""Gets the essential information for debugging.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,10 @@ class ApiClient(object):
"""
if not auth_settings:
return

for auth in auth_settings:
auth_setting = self.configuration.auth_settings().get(auth)
if auth_setting:
if not auth_setting['value']:
continue
elif auth_setting['in'] == 'cookie':
if auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,10 @@ def update_params_for_auth(self, headers, querys, auth_settings):
"""
if not auth_settings:
return

for auth in auth_settings:
auth_setting = self.configuration.auth_settings().get(auth)
if auth_setting:
if not auth_setting['value']:
continue
elif auth_setting['in'] == 'cookie':
if auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value']
Expand Down
Loading