Skip to content

Commit

Permalink
[Python] Conditionally set auth attributes (#4988)
Browse files Browse the repository at this point in the history
* Enhance python API keys

* Run python scripts under ./bin/openapi3

* fix unit test issue

* Fix unit tests

* Fix unit tests

* Invoke bin scripts
  • Loading branch information
sebastien-rosset authored and wing328 committed Jan 16, 2020
1 parent b94fe7a commit fa0ef2b
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,7 @@ class ApiClient(object):
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,38 @@ 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: Dict to store API key(s).
Each entry in the dict specifies an API key.
The dict key is the name of the security scheme in the OAS specification.
The dict value is the API key secret.
:param api_key_prefix: Dict to store API prefix (e.g. Bearer)
The dict key is the name of the security scheme in the OAS specification.
The dict value is an API key prefix when generating the auth data.
:param username: Username for HTTP basic authentication
:param password: Password for HTTP basic authentication

: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={'cookieAuth': 'abc123'}
api_key_prefix={'cookieAuth': 'JSESSIONID'}
)
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=""):
username=None, password=None):
"""Constructor
"""
self.host = host
Expand Down Expand Up @@ -247,60 +270,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 @@ -530,9 +530,7 @@ class ApiClient(object):
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 @@ -513,9 +513,7 @@ def update_params_for_auth(self, headers, querys, auth_settings):
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 @@ -27,15 +27,38 @@ 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: Dict to store API key(s).
Each entry in the dict specifies an API key.
The dict key is the name of the security scheme in the OAS specification.
The dict value is the API key secret.
:param api_key_prefix: Dict to store API prefix (e.g. Bearer)
The dict key is the name of the security scheme in the OAS specification.
The dict value is an API key prefix when generating the auth data.
:param username: Username for HTTP basic authentication
:param password: Password for HTTP basic authentication
: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 = petstore_api.Configuration(
api_key={'cookieAuth': 'abc123'}
api_key_prefix={'cookieAuth': 'JSESSIONID'}
)
The following cookie will be added to the HTTP request:
Cookie: JSESSIONID abc123
"""

def __init__(self, host="http://petstore.swagger.io:80/v2",
api_key=None, api_key_prefix=None,
username="", password=""):
username=None, password=None):
"""Constructor
"""
self.host = host
Expand Down Expand Up @@ -232,45 +255,51 @@ def get_basic_auth_token(self):
: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 {
'api_key':
{
'type': 'api_key',
'in': 'header',
'key': 'api_key',
'value': self.get_api_key_with_prefix('api_key')
},
'api_key_query':
{
'type': 'api_key',
'in': 'query',
'key': 'api_key_query',
'value': self.get_api_key_with_prefix('api_key_query')
},
'http_basic_test':
{
'type': 'basic',
'in': 'header',
'key': 'Authorization',
'value': self.get_basic_auth_token()
},
'petstore_auth':
{
'type': 'oauth2',
'in': 'header',
'key': 'Authorization',
'value': 'Bearer ' + self.access_token
},
}
auth = {}
if 'api_key' in self.api_key:
auth['api_key'] = {
'type': 'api_key',
'in': 'header',
'key': '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')
}
if self.username is not None and self.password is not None:
auth['http_basic_test'] = {
'type': 'basic',
'in': 'header',
'key': 'Authorization',
'value': self.get_basic_auth_token()
}
if self.access_token is not None:
auth['petstore_auth'] = {
'type': 'oauth2',
'in': 'header',
'key': 'Authorization',
'value': 'Bearer ' + self.access_token
}
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 @@ -523,9 +523,7 @@ def update_params_for_auth(self, headers, querys, auth_settings):
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

0 comments on commit fa0ef2b

Please sign in to comment.