diff --git a/modules/openapi-generator/src/main/resources/python/api_client.mustache b/modules/openapi-generator/src/main/resources/python/api_client.mustache index 3bf085e1904b..57bb78133c53 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -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'] diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 6e02b238da1a..07d75647e80a 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -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 @@ -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 """ @@ -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 """ @@ -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 @@ -247,8 +259,14 @@ 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): @@ -256,51 +274,51 @@ class Configuration(object): :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. diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache index 25ca4b5043ff..a553f05ec62d 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/api_client.mustache @@ -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'] diff --git a/samples/client/petstore/python-asyncio/petstore_api/api_client.py b/samples/client/petstore/python-asyncio/petstore_api/api_client.py index 24b99a2946f4..b07166d76450 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/api_client.py +++ b/samples/client/petstore/python-asyncio/petstore_api/api_client.py @@ -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'] diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index fa90c926deef..e3acb62fd230 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -27,15 +27,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 = petstore_api.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="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, - username="", password=""): + api_key=None, + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -50,11 +74,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", 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 """ @@ -64,7 +83,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -219,11 +238,13 @@ def get_api_key_with_prefix(self, identifier): """ 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 @@ -232,8 +253,14 @@ 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): @@ -241,36 +268,36 @@ def auth_settings(self): :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. diff --git a/samples/client/petstore/python-experimental/petstore_api/api_client.py b/samples/client/petstore/python-experimental/petstore_api/api_client.py index 6673889b51f7..37025b1314b3 100644 --- a/samples/client/petstore/python-experimental/petstore_api/api_client.py +++ b/samples/client/petstore/python-experimental/petstore_api/api_client.py @@ -519,13 +519,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'] diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index 9a0c95c90458..b8194838e3b3 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -28,15 +28,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 = petstore_api.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="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, - username="", password=""): + api_key=None, + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -51,11 +75,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", 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 """ @@ -65,7 +84,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -223,11 +242,13 @@ def get_api_key_with_prefix(self, identifier): """ 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 @@ -236,8 +257,14 @@ 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): @@ -245,36 +272,36 @@ def auth_settings(self): :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. diff --git a/samples/client/petstore/python-experimental/tests/test_api_client.py b/samples/client/petstore/python-experimental/tests/test_api_client.py index ad50f1862014..cd993eb760a1 100644 --- a/samples/client/petstore/python-experimental/tests/test_api_client.py +++ b/samples/client/petstore/python-experimental/tests/test_api_client.py @@ -29,8 +29,10 @@ def test_configuration(self): config = petstore_api.Configuration() config.host = 'http://localhost/' - config.api_key['api_key'] = '123456' - config.api_key_prefix['api_key'] = 'PREFIX' + config.api_key['api_key'] = {'value': '123456', 'prefix': 'PREFIX='} + # api key prefix used to be set with the api_key_prefix attribute. + # Now the key prefix is set in the 'api_key' dictionary. + #config.api_key_prefix['api_key'] = 'PREFIX=' config.username = 'test_username' config.password = 'test_password' @@ -41,20 +43,32 @@ def test_configuration(self): client = petstore_api.ApiClient(config) # test prefix - self.assertEqual('PREFIX', client.configuration.api_key_prefix['api_key']) + self.assertEqual('PREFIX=', client.configuration.api_key['api_key']['prefix']) # update parameters based on auth setting client.update_params_for_auth(header_params, query_params, auth_settings) # test api key auth self.assertEqual(header_params['test1'], 'value1') - self.assertEqual(header_params['api_key'], 'PREFIX 123456') + self.assertEqual(header_params['api_key'], 'PREFIX=123456') self.assertEqual(query_params['test2'], 'value2') # test basic auth self.assertEqual('test_username', client.configuration.username) self.assertEqual('test_password', client.configuration.password) + # test api key without prefix + config.api_key['api_key'] = {'value': '123456'} + # update parameters based on auth setting + client.update_params_for_auth(header_params, query_params, auth_settings) + self.assertEqual(header_params['api_key'], '123456') + + # test api key with wrong type + config.api_key['api_key'] = '123456' + with self.assertRaises(Exception): + # update parameters based on auth setting + client.update_params_for_auth(header_params, query_params, auth_settings) + def test_select_header_accept(self): accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] accept = self.api_client.select_header_accept(accepts) diff --git a/samples/client/petstore/python-experimental/tests/test_pet_api.py b/samples/client/petstore/python-experimental/tests/test_pet_api.py index 5e1dd8528c9c..a86d07e0e627 100644 --- a/samples/client/petstore/python-experimental/tests/test_pet_api.py +++ b/samples/client/petstore/python-experimental/tests/test_pet_api.py @@ -73,6 +73,7 @@ class PetApiTests(unittest.TestCase): def setUp(self): config = Configuration() config.host = HOST + config.access_token = 'ACCESS_TOKEN' self.api_client = petstore_api.ApiClient(config) self.pet_api = petstore_api.PetApi(self.api_client) self.setUpModels() @@ -115,6 +116,26 @@ def test_preload_content_flag(self): resp.close() resp.release_conn() + def test_config(self): + config = Configuration(host=HOST, access_token='ACCESS_TOKEN') + self.assertIsNotNone(config.get_host_settings()) + self.assertEquals(config.get_basic_auth_token(), + urllib3.util.make_headers(basic_auth=":").get('authorization')) + self.assertEquals(len(config.auth_settings()), 1) + self.assertIn("petstore_auth", config.auth_settings().keys()) + config.username = "user" + config.password = "password" + self.assertEquals( + config.get_basic_auth_token(), + urllib3.util.make_headers(basic_auth="user:password").get('authorization')) + self.assertEquals(len(config.auth_settings()), 2) + self.assertIn("petstore_auth", config.auth_settings().keys()) + self.assertIn("http_basic_test", config.auth_settings().keys()) + config.username = None + config.password = None + self.assertEquals(len(config.auth_settings()), 1) + self.assertIn("petstore_auth", config.auth_settings().keys()) + def test_timeout(self): mock_pool = MockPoolManager(self) self.api_client.rest_client.pool_manager = mock_pool @@ -122,13 +143,13 @@ def test_timeout(self): mock_pool.expect_request('POST', 'http://localhost/v2/pet', body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), headers={'Content-Type': 'application/json', - 'Authorization': 'Bearer ', + 'Authorization': 'Bearer ACCESS_TOKEN', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}, preload_content=True, timeout=TimeoutWithEqual(total=5)) mock_pool.expect_request('POST', 'http://localhost/v2/pet', body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), headers={'Content-Type': 'application/json', - 'Authorization': 'Bearer ', + 'Authorization': 'Bearer ACCESS_TOKEN', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}, preload_content=True, timeout=TimeoutWithEqual(connect=1, read=2)) @@ -325,7 +346,7 @@ def get_header(name, default=None): 'Accept': 'application/json', 'Content-Type': 'multipart/form-data', 'User-Agent': 'OpenAPI-Generator/1.0.0/python', - 'Authorization': 'Bearer ' + 'Authorization': 'Bearer ACCESS_TOKEN' }, post_params=[ ('files', ('1px_pic1.png', b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x00\x00\x00\x00:~\x9bU\x00\x00\x00\nIDATx\x9cc\xfa\x0f\x00\x01\x05\x01\x02\xcf\xa0.\xcd\x00\x00\x00\x00IEND\xaeB`\x82', 'image/png')), diff --git a/samples/client/petstore/python-tornado/petstore_api/api_client.py b/samples/client/petstore/python-tornado/petstore_api/api_client.py index 0d1075ffe970..d86d8817779b 100644 --- a/samples/client/petstore/python-tornado/petstore_api/api_client.py +++ b/samples/client/petstore/python-tornado/petstore_api/api_client.py @@ -511,13 +511,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'] diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 9a0c95c90458..b8194838e3b3 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -28,15 +28,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 = petstore_api.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="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, - username="", password=""): + api_key=None, + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -51,11 +75,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", 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 """ @@ -65,7 +84,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -223,11 +242,13 @@ def get_api_key_with_prefix(self, identifier): """ 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 @@ -236,8 +257,14 @@ 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): @@ -245,36 +272,36 @@ def auth_settings(self): :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. diff --git a/samples/client/petstore/python/petstore_api/api_client.py b/samples/client/petstore/python/petstore_api/api_client.py index 46e568df26ab..52910fd8d0d9 100644 --- a/samples/client/petstore/python/petstore_api/api_client.py +++ b/samples/client/petstore/python/petstore_api/api_client.py @@ -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'] diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 9a0c95c90458..b8194838e3b3 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -28,15 +28,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 = petstore_api.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="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, - username="", password=""): + api_key=None, + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -51,11 +75,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", 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 """ @@ -65,7 +84,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -223,11 +242,13 @@ def get_api_key_with_prefix(self, identifier): """ 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 @@ -236,8 +257,14 @@ 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): @@ -245,36 +272,36 @@ def auth_settings(self): :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. diff --git a/samples/client/petstore/python/tests/test_api_client.py b/samples/client/petstore/python/tests/test_api_client.py index c9b9edd86c44..5d2cea35a128 100644 --- a/samples/client/petstore/python/tests/test_api_client.py +++ b/samples/client/petstore/python/tests/test_api_client.py @@ -28,8 +28,7 @@ def setUp(self): def test_configuration(self): config = petstore_api.Configuration() - config.api_key['api_key'] = '123456' - config.api_key_prefix['api_key'] = 'PREFIX' + config.api_key['api_key'] = { 'value': '123456', 'prefix': 'PREFIX' } config.username = 'test_username' config.password = 'test_password' @@ -39,15 +38,12 @@ def test_configuration(self): client = petstore_api.ApiClient(config) - # test prefix - self.assertEqual('PREFIX', client.configuration.api_key_prefix['api_key']) - # update parameters based on auth setting client.update_params_for_auth(header_params, query_params, auth_settings) # test api key auth self.assertEqual(header_params['test1'], 'value1') - self.assertEqual(header_params['api_key'], 'PREFIX 123456') + self.assertEqual(header_params['api_key'], 'PREFIX123456') self.assertEqual(query_params['test2'], 'value2') # test basic auth diff --git a/samples/client/petstore/python/tests/test_configuration.py b/samples/client/petstore/python/tests/test_configuration.py index f1074b17e70a..7ec03789a887 100644 --- a/samples/client/petstore/python/tests/test_configuration.py +++ b/samples/client/petstore/python/tests/test_configuration.py @@ -29,7 +29,6 @@ def testConfiguration(self): c1 = petstore_api.Configuration() c2 = petstore_api.Configuration() assert id(c1.api_key) != id(c2.api_key) - assert id(c1.api_key_prefix) != id(c2.api_key_prefix) if __name__ == '__main__': diff --git a/samples/client/petstore/python/tests/test_pet_api.py b/samples/client/petstore/python/tests/test_pet_api.py index 80d34f6b5f21..6ec416885dce 100644 --- a/samples/client/petstore/python/tests/test_pet_api.py +++ b/samples/client/petstore/python/tests/test_pet_api.py @@ -57,6 +57,7 @@ class PetApiTests(unittest.TestCase): def setUp(self): config = Configuration() config.host = HOST + config.access_token = 'ACCESS_TOKEN' self.api_client = petstore_api.ApiClient(config) self.pet_api = petstore_api.PetApi(self.api_client) self.setUpModels() @@ -107,13 +108,13 @@ def test_timeout(self): mock_pool.expect_request('POST', HOST + '/pet', body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), headers={'Content-Type': 'application/json', - 'Authorization': 'Bearer ', + 'Authorization': 'Bearer ACCESS_TOKEN', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}, preload_content=True, timeout=TimeoutWithEqual(total=5)) mock_pool.expect_request('POST', HOST + '/pet', body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)), headers={'Content-Type': 'application/json', - 'Authorization': 'Bearer ', + 'Authorization': 'Bearer ACCESS_TOKEN', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}, preload_content=True, timeout=TimeoutWithEqual(connect=1, read=2)) @@ -222,7 +223,6 @@ def test_find_pets_by_status(self): def test_find_pets_by_tags(self): self.pet_api.add_pet(self.pet) - self.assertIn( self.pet.id, list(map(lambda x: getattr(x, 'id'), self.pet_api.find_pets_by_tags(tags=[self.tag.name]))) diff --git a/samples/openapi3/client/petstore/python/petstore_api/api_client.py b/samples/openapi3/client/petstore/python/petstore_api/api_client.py index 46e568df26ab..52910fd8d0d9 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -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'] diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index 91109c7d78cb..ac7b3c6ce9ca 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -28,15 +28,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 = petstore_api.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="http://petstore.swagger.io:80/v2", - api_key=None, api_key_prefix=None, - username="", password=""): + api_key=None, + username=None, password=None, + access_token=None): """Constructor """ self.host = host @@ -51,11 +75,6 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", 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 """ @@ -65,7 +84,7 @@ def __init__(self, host="http://petstore.swagger.io:80/v2", self.password = password """Password for HTTP basic authentication """ - self.access_token = "" + self.access_token = access_token """access token for OAuth/Bearer """ self.logger = {} @@ -223,11 +242,13 @@ def get_api_key_with_prefix(self, identifier): """ 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 @@ -236,8 +257,14 @@ 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): @@ -245,44 +272,44 @@ def auth_settings(self): :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') - }, - 'bearer_test': - { - 'type': 'bearer', - 'in': 'header', - 'format': 'JWT', - 'key': 'Authorization', - 'value': 'Bearer ' + self.access_token - }, - '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.access_token is not None: + auth['bearer_test'] = { + 'type': 'bearer', + 'in': 'header', + 'format': 'JWT', + 'key': 'Authorization', + 'value': 'Bearer ' + self.access_token + } + 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.