Python apikey cookie (#2367)

* The `ApiClient` will now have support to store and use HTTP Cookies (as APIKey auth).

* Use Cookie authentication for user management.

* - Updated Python related samples.
This commit is contained in:
Tom Ghyselinck 2019-03-14 09:33:35 +01:00 committed by William Cheng
parent 33786e11f3
commit 83bc863b2b
14 changed files with 128 additions and 1 deletions

View File

@ -523,6 +523,8 @@ class ApiClient(object):
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
elif auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header': elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value'] headers[auth_setting['key']] = auth_setting['value']
elif auth_setting['in'] == 'query': elif auth_setting['in'] == 'query':

View File

@ -218,7 +218,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
'{{name}}': '{{name}}':
{ {
'type': 'api_key', 'type': 'api_key',
'in': {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, 'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
'key': '{{keyParamName}}', 'key': '{{keyParamName}}',
'value': self.get_api_key_with_prefix('{{keyParamName}}') 'value': self.get_api_key_with_prefix('{{keyParamName}}')
}, },

View File

@ -374,6 +374,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
requestBody: requestBody:
content: content:
application/json: application/json:
@ -391,6 +393,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
requestBody: requestBody:
$ref: '#/components/requestBodies/UserArray' $ref: '#/components/requestBodies/UserArray'
/user/createWithList: /user/createWithList:
@ -403,6 +407,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
requestBody: requestBody:
$ref: '#/components/requestBodies/UserArray' $ref: '#/components/requestBodies/UserArray'
/user/login: /user/login:
@ -430,6 +436,13 @@ paths:
'200': '200':
description: successful operation description: successful operation
headers: headers:
Set-Cookie:
description: >-
Cookie authentication key for use with the `auth_cookie`
apiKey authentication.
schema:
type: string
example: AUTH_KEY=abcde12345; Path=/; HttpOnly
X-Rate-Limit: X-Rate-Limit:
description: calls per hour allowed by the user description: calls per hour allowed by the user
schema: schema:
@ -459,6 +472,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
'/user/{username}': '/user/{username}':
get: get:
tags: tags:
@ -505,6 +520,8 @@ paths:
description: Invalid user supplied description: Invalid user supplied
'404': '404':
description: User not found description: User not found
security:
- auth_cookie: []
requestBody: requestBody:
content: content:
application/json: application/json:
@ -530,6 +547,8 @@ paths:
description: Invalid username supplied description: Invalid username supplied
'404': '404':
description: User not found description: User not found
security:
- auth_cookie: []
externalDocs: externalDocs:
description: Find out more about Swagger description: Find out more about Swagger
url: 'http://swagger.io' url: 'http://swagger.io'
@ -567,6 +586,10 @@ components:
type: apiKey type: apiKey
name: api_key name: api_key
in: header in: header
auth_cookie:
type: apiKey
name: AUTH_KEY
in: cookie
schemas: schemas:
Order: Order:
title: Pet Order title: Pet Order

View File

@ -517,6 +517,8 @@ class ApiClient(object):
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
elif auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header': elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value'] headers[auth_setting['key']] = auth_setting['value']
elif auth_setting['in'] == 'query': elif auth_setting['in'] == 'query':

View File

@ -516,6 +516,8 @@ class ApiClient(object):
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
elif auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header': elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value'] headers[auth_setting['key']] = auth_setting['value']
elif auth_setting['in'] == 'query': elif auth_setting['in'] == 'query':

View File

@ -518,6 +518,8 @@ class ApiClient(object):
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
elif auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header': elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value'] headers[auth_setting['key']] = auth_setting['value']
elif auth_setting['in'] == 'query': elif auth_setting['in'] == 'query':

View File

@ -516,6 +516,8 @@ class ApiClient(object):
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
elif auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header': elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value'] headers[auth_setting['key']] = auth_setting['value']
elif auth_setting['in'] == 'query': elif auth_setting['in'] == 'query':

View File

@ -516,6 +516,8 @@ class ApiClient(object):
if auth_setting: if auth_setting:
if not auth_setting['value']: if not auth_setting['value']:
continue continue
elif auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header': elif auth_setting['in'] == 'header':
headers[auth_setting['key']] = auth_setting['value'] headers[auth_setting['key']] = auth_setting['value']
elif auth_setting['in'] == 'query': elif auth_setting['in'] == 'query':

View File

@ -17,6 +17,22 @@ def info_from_api_key(api_key, required_scopes):
return {'uid': 'user_id'} return {'uid': 'user_id'}
def info_from_auth_cookie(api_key, required_scopes):
"""
Check and retrieve authentication information from api_key.
Returned value will be passed in 'token_info' parameter of your operation function, if there is one.
'sub' or 'uid' will be set in 'user' parameter of your operation function, if there is one.
:param api_key API key provided by Authorization header
:type api_key: str
:param required_scopes Always None. Used for other authentication method
:type required_scopes: None
:return: Information attached to provided api_key or None if api_key is invalid or does not allow access to called API
:rtype: dict | None
"""
return {'uid': 'user_id'}
def info_from_petstore_auth(token): def info_from_petstore_auth(token):
""" """
Validate and decode token. Validate and decode token.

View File

@ -395,6 +395,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
summary: Create user summary: Create user
tags: tags:
- user - user
@ -407,6 +409,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
summary: Creates list of users with given input array summary: Creates list of users with given input array
tags: tags:
- user - user
@ -419,6 +423,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
summary: Creates list of users with given input array summary: Creates list of users with given input array
tags: tags:
- user - user
@ -455,6 +461,13 @@ paths:
type: string type: string
description: successful operation description: successful operation
headers: headers:
Set-Cookie:
description: Cookie authentication key for use with the `auth_cookie` apiKey authentication.
explode: false
schema:
example: AUTH_KEY=abcde12345; Path=/; HttpOnly
type: string
style: simple
X-Rate-Limit: X-Rate-Limit:
description: calls per hour allowed by the user description: calls per hour allowed by the user
explode: false explode: false
@ -481,6 +494,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
summary: Logs out current logged in user session summary: Logs out current logged in user session
tags: tags:
- user - user
@ -503,6 +518,8 @@ paths:
description: Invalid username supplied description: Invalid username supplied
404: 404:
description: User not found description: User not found
security:
- auth_cookie: []
summary: Delete user summary: Delete user
tags: tags:
- user - user
@ -560,6 +577,8 @@ paths:
description: Invalid user supplied description: Invalid user supplied
404: 404:
description: User not found description: User not found
security:
- auth_cookie: []
summary: Updated user summary: Updated user
tags: tags:
- user - user
@ -803,3 +822,8 @@ components:
name: api_key name: api_key
type: apiKey type: apiKey
x-apikeyInfoFunc: openapi_server.controllers.security_controller_.info_from_api_key x-apikeyInfoFunc: openapi_server.controllers.security_controller_.info_from_api_key
auth_cookie:
in: cookie
name: AUTH_KEY
type: apiKey
x-apikeyInfoFunc: openapi_server.controllers.security_controller_.info_from_auth_cookie

View File

@ -30,6 +30,7 @@ class TestUserController(BaseTestCase):
} }
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user', '/v2/user',
@ -48,6 +49,7 @@ class TestUserController(BaseTestCase):
user = [] user = []
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/createWithArray', '/v2/user/createWithArray',
@ -66,6 +68,7 @@ class TestUserController(BaseTestCase):
user = [] user = []
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/createWithList', '/v2/user/createWithList',
@ -82,6 +85,7 @@ class TestUserController(BaseTestCase):
Delete user Delete user
""" """
headers = { headers = {
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/{username}'.format(username='username_example'), '/v2/user/{username}'.format(username='username_example'),
@ -129,6 +133,7 @@ class TestUserController(BaseTestCase):
Logs out current logged in user session Logs out current logged in user session
""" """
headers = { headers = {
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/logout', '/v2/user/logout',
@ -154,6 +159,7 @@ class TestUserController(BaseTestCase):
} }
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/{username}'.format(username='username_example'), '/v2/user/{username}'.format(username='username_example'),

View File

@ -17,6 +17,22 @@ def info_from_api_key(api_key, required_scopes):
return {'uid': 'user_id'} return {'uid': 'user_id'}
def info_from_auth_cookie(api_key, required_scopes):
"""
Check and retrieve authentication information from api_key.
Returned value will be passed in 'token_info' parameter of your operation function, if there is one.
'sub' or 'uid' will be set in 'user' parameter of your operation function, if there is one.
:param api_key API key provided by Authorization header
:type api_key: str
:param required_scopes Always None. Used for other authentication method
:type required_scopes: None
:return: Information attached to provided api_key or None if api_key is invalid or does not allow access to called API
:rtype: dict | None
"""
return {'uid': 'user_id'}
def info_from_petstore_auth(token): def info_from_petstore_auth(token):
""" """
Validate and decode token. Validate and decode token.

View File

@ -395,6 +395,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
summary: Create user summary: Create user
tags: tags:
- user - user
@ -407,6 +409,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
summary: Creates list of users with given input array summary: Creates list of users with given input array
tags: tags:
- user - user
@ -419,6 +423,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
summary: Creates list of users with given input array summary: Creates list of users with given input array
tags: tags:
- user - user
@ -455,6 +461,13 @@ paths:
type: string type: string
description: successful operation description: successful operation
headers: headers:
Set-Cookie:
description: Cookie authentication key for use with the `auth_cookie` apiKey authentication.
explode: false
schema:
example: AUTH_KEY=abcde12345; Path=/; HttpOnly
type: string
style: simple
X-Rate-Limit: X-Rate-Limit:
description: calls per hour allowed by the user description: calls per hour allowed by the user
explode: false explode: false
@ -481,6 +494,8 @@ paths:
responses: responses:
default: default:
description: successful operation description: successful operation
security:
- auth_cookie: []
summary: Logs out current logged in user session summary: Logs out current logged in user session
tags: tags:
- user - user
@ -503,6 +518,8 @@ paths:
description: Invalid username supplied description: Invalid username supplied
404: 404:
description: User not found description: User not found
security:
- auth_cookie: []
summary: Delete user summary: Delete user
tags: tags:
- user - user
@ -560,6 +577,8 @@ paths:
description: Invalid user supplied description: Invalid user supplied
404: 404:
description: User not found description: User not found
security:
- auth_cookie: []
summary: Updated user summary: Updated user
tags: tags:
- user - user
@ -803,3 +822,8 @@ components:
name: api_key name: api_key
type: apiKey type: apiKey
x-apikeyInfoFunc: openapi_server.controllers.security_controller_.info_from_api_key x-apikeyInfoFunc: openapi_server.controllers.security_controller_.info_from_api_key
auth_cookie:
in: cookie
name: AUTH_KEY
type: apiKey
x-apikeyInfoFunc: openapi_server.controllers.security_controller_.info_from_auth_cookie

View File

@ -30,6 +30,7 @@ class TestUserController(BaseTestCase):
} }
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user', '/v2/user',
@ -48,6 +49,7 @@ class TestUserController(BaseTestCase):
user = [] user = []
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/createWithArray', '/v2/user/createWithArray',
@ -66,6 +68,7 @@ class TestUserController(BaseTestCase):
user = [] user = []
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/createWithList', '/v2/user/createWithList',
@ -82,6 +85,7 @@ class TestUserController(BaseTestCase):
Delete user Delete user
""" """
headers = { headers = {
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/{username}'.format(username='username_example'), '/v2/user/{username}'.format(username='username_example'),
@ -129,6 +133,7 @@ class TestUserController(BaseTestCase):
Logs out current logged in user session Logs out current logged in user session
""" """
headers = { headers = {
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/logout', '/v2/user/logout',
@ -154,6 +159,7 @@ class TestUserController(BaseTestCase):
} }
headers = { headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'auth_cookie': 'special-key',
} }
response = self.client.open( response = self.client.open(
'/v2/user/{username}'.format(username='username_example'), '/v2/user/{username}'.format(username='username_example'),