mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 06:00:52 +00:00
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:
parent
33786e11f3
commit
83bc863b2b
@ -523,6 +523,8 @@ class ApiClient(object):
|
||||
if auth_setting:
|
||||
if not auth_setting['value']:
|
||||
continue
|
||||
elif auth_setting['in'] == 'cookie':
|
||||
headers['Cookie'] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'query':
|
||||
|
@ -218,7 +218,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
|
||||
'{{name}}':
|
||||
{
|
||||
'type': 'api_key',
|
||||
'in': {{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
|
||||
'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}},
|
||||
'key': '{{keyParamName}}',
|
||||
'value': self.get_api_key_with_prefix('{{keyParamName}}')
|
||||
},
|
||||
|
@ -374,6 +374,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
@ -391,6 +393,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
requestBody:
|
||||
$ref: '#/components/requestBodies/UserArray'
|
||||
/user/createWithList:
|
||||
@ -403,6 +407,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
requestBody:
|
||||
$ref: '#/components/requestBodies/UserArray'
|
||||
/user/login:
|
||||
@ -430,6 +436,13 @@ paths:
|
||||
'200':
|
||||
description: successful operation
|
||||
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:
|
||||
description: calls per hour allowed by the user
|
||||
schema:
|
||||
@ -459,6 +472,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
'/user/{username}':
|
||||
get:
|
||||
tags:
|
||||
@ -505,6 +520,8 @@ paths:
|
||||
description: Invalid user supplied
|
||||
'404':
|
||||
description: User not found
|
||||
security:
|
||||
- auth_cookie: []
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
@ -530,6 +547,8 @@ paths:
|
||||
description: Invalid username supplied
|
||||
'404':
|
||||
description: User not found
|
||||
security:
|
||||
- auth_cookie: []
|
||||
externalDocs:
|
||||
description: Find out more about Swagger
|
||||
url: 'http://swagger.io'
|
||||
@ -567,6 +586,10 @@ components:
|
||||
type: apiKey
|
||||
name: api_key
|
||||
in: header
|
||||
auth_cookie:
|
||||
type: apiKey
|
||||
name: AUTH_KEY
|
||||
in: cookie
|
||||
schemas:
|
||||
Order:
|
||||
title: Pet Order
|
||||
|
@ -517,6 +517,8 @@ class ApiClient(object):
|
||||
if auth_setting:
|
||||
if not auth_setting['value']:
|
||||
continue
|
||||
elif auth_setting['in'] == 'cookie':
|
||||
headers['Cookie'] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'query':
|
||||
|
@ -516,6 +516,8 @@ class ApiClient(object):
|
||||
if auth_setting:
|
||||
if not auth_setting['value']:
|
||||
continue
|
||||
elif auth_setting['in'] == 'cookie':
|
||||
headers['Cookie'] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'query':
|
||||
|
@ -518,6 +518,8 @@ class ApiClient(object):
|
||||
if auth_setting:
|
||||
if not auth_setting['value']:
|
||||
continue
|
||||
elif auth_setting['in'] == 'cookie':
|
||||
headers['Cookie'] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'query':
|
||||
|
@ -516,6 +516,8 @@ class ApiClient(object):
|
||||
if auth_setting:
|
||||
if not auth_setting['value']:
|
||||
continue
|
||||
elif auth_setting['in'] == 'cookie':
|
||||
headers['Cookie'] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'query':
|
||||
|
@ -516,6 +516,8 @@ class ApiClient(object):
|
||||
if auth_setting:
|
||||
if not auth_setting['value']:
|
||||
continue
|
||||
elif auth_setting['in'] == 'cookie':
|
||||
headers['Cookie'] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'header':
|
||||
headers[auth_setting['key']] = auth_setting['value']
|
||||
elif auth_setting['in'] == 'query':
|
||||
|
@ -17,6 +17,22 @@ def info_from_api_key(api_key, required_scopes):
|
||||
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):
|
||||
"""
|
||||
Validate and decode token.
|
||||
|
@ -395,6 +395,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Create user
|
||||
tags:
|
||||
- user
|
||||
@ -407,6 +409,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Creates list of users with given input array
|
||||
tags:
|
||||
- user
|
||||
@ -419,6 +423,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Creates list of users with given input array
|
||||
tags:
|
||||
- user
|
||||
@ -455,6 +461,13 @@ paths:
|
||||
type: string
|
||||
description: successful operation
|
||||
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:
|
||||
description: calls per hour allowed by the user
|
||||
explode: false
|
||||
@ -481,6 +494,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Logs out current logged in user session
|
||||
tags:
|
||||
- user
|
||||
@ -503,6 +518,8 @@ paths:
|
||||
description: Invalid username supplied
|
||||
404:
|
||||
description: User not found
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Delete user
|
||||
tags:
|
||||
- user
|
||||
@ -560,6 +577,8 @@ paths:
|
||||
description: Invalid user supplied
|
||||
404:
|
||||
description: User not found
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Updated user
|
||||
tags:
|
||||
- user
|
||||
@ -803,3 +822,8 @@ components:
|
||||
name: api_key
|
||||
type: apiKey
|
||||
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
|
||||
|
@ -30,6 +30,7 @@ class TestUserController(BaseTestCase):
|
||||
}
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user',
|
||||
@ -48,6 +49,7 @@ class TestUserController(BaseTestCase):
|
||||
user = []
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/createWithArray',
|
||||
@ -66,6 +68,7 @@ class TestUserController(BaseTestCase):
|
||||
user = []
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/createWithList',
|
||||
@ -82,6 +85,7 @@ class TestUserController(BaseTestCase):
|
||||
Delete user
|
||||
"""
|
||||
headers = {
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/{username}'.format(username='username_example'),
|
||||
@ -129,6 +133,7 @@ class TestUserController(BaseTestCase):
|
||||
Logs out current logged in user session
|
||||
"""
|
||||
headers = {
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/logout',
|
||||
@ -154,6 +159,7 @@ class TestUserController(BaseTestCase):
|
||||
}
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/{username}'.format(username='username_example'),
|
||||
|
@ -17,6 +17,22 @@ def info_from_api_key(api_key, required_scopes):
|
||||
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):
|
||||
"""
|
||||
Validate and decode token.
|
||||
|
@ -395,6 +395,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Create user
|
||||
tags:
|
||||
- user
|
||||
@ -407,6 +409,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Creates list of users with given input array
|
||||
tags:
|
||||
- user
|
||||
@ -419,6 +423,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Creates list of users with given input array
|
||||
tags:
|
||||
- user
|
||||
@ -455,6 +461,13 @@ paths:
|
||||
type: string
|
||||
description: successful operation
|
||||
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:
|
||||
description: calls per hour allowed by the user
|
||||
explode: false
|
||||
@ -481,6 +494,8 @@ paths:
|
||||
responses:
|
||||
default:
|
||||
description: successful operation
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Logs out current logged in user session
|
||||
tags:
|
||||
- user
|
||||
@ -503,6 +518,8 @@ paths:
|
||||
description: Invalid username supplied
|
||||
404:
|
||||
description: User not found
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Delete user
|
||||
tags:
|
||||
- user
|
||||
@ -560,6 +577,8 @@ paths:
|
||||
description: Invalid user supplied
|
||||
404:
|
||||
description: User not found
|
||||
security:
|
||||
- auth_cookie: []
|
||||
summary: Updated user
|
||||
tags:
|
||||
- user
|
||||
@ -803,3 +822,8 @@ components:
|
||||
name: api_key
|
||||
type: apiKey
|
||||
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
|
||||
|
@ -30,6 +30,7 @@ class TestUserController(BaseTestCase):
|
||||
}
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user',
|
||||
@ -48,6 +49,7 @@ class TestUserController(BaseTestCase):
|
||||
user = []
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/createWithArray',
|
||||
@ -66,6 +68,7 @@ class TestUserController(BaseTestCase):
|
||||
user = []
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/createWithList',
|
||||
@ -82,6 +85,7 @@ class TestUserController(BaseTestCase):
|
||||
Delete user
|
||||
"""
|
||||
headers = {
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/{username}'.format(username='username_example'),
|
||||
@ -129,6 +133,7 @@ class TestUserController(BaseTestCase):
|
||||
Logs out current logged in user session
|
||||
"""
|
||||
headers = {
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/logout',
|
||||
@ -154,6 +159,7 @@ class TestUserController(BaseTestCase):
|
||||
}
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'auth_cookie': 'special-key',
|
||||
}
|
||||
response = self.client.open(
|
||||
'/v2/user/{username}'.format(username='username_example'),
|
||||
|
Loading…
x
Reference in New Issue
Block a user