Skip to content

Commit

Permalink
Added OpenAPI 3 getting API Key from cookie support. (#794)
Browse files Browse the repository at this point in the history
* Added OpenAPI 3 getting API Key from cookie support.
  • Loading branch information
ErmakovDmitriy authored and jmcs committed Dec 18, 2018
1 parent 089840b commit a0ac0fd
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions connexion/decorators/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import textwrap

import requests
from six.moves import http_cookies

from connexion.utils import get_function_from_name

Expand Down Expand Up @@ -115,8 +116,10 @@ def security_deny(function):
:type function: types.FunctionType
:rtype: types.FunctionType
"""

def deny(*args, **kwargs):
raise ConnexionException("Error in security definitions")

return deny


Expand Down Expand Up @@ -181,6 +184,7 @@ def verify_authorization_token(request, token_info_func):


def verify_oauth(token_info_func, scope_validate_func):

def wrapper(request, required_scopes):
token_info = verify_authorization_token(request, token_info_func)
if token_info is None:
Expand All @@ -196,10 +200,12 @@ def wrapper(request, required_scopes):
)

return token_info

return wrapper


def verify_basic(basic_info_func):

def wrapper(request, required_scopes):
authorization = request.headers.get('Authorization')
if not authorization:
Expand All @@ -225,15 +231,34 @@ def wrapper(request, required_scopes):
token_response=None
)
return token_info

return wrapper


def get_cookie_value(cookies, name):
'''
Returns cookie value by its name. None if no such value.
:param cookies: str: cookies raw data
:param name: str: cookies key
'''
cookie_parser = http_cookies.SimpleCookie()
cookie_parser.load(str(cookies))
try:
return cookie_parser[name].value
except KeyError:
return None


def verify_apikey(apikey_info_func, loc, name):

def wrapper(request, required_scopes):
if loc == 'query':
apikey = request.query.get(name)
elif loc == 'header':
apikey = request.headers.get(name)
elif loc == 'cookie':
cookieslist = request.headers.get('Cookie')
apikey = get_cookie_value(cookieslist, name)
else:
return None

Expand All @@ -247,6 +272,7 @@ def wrapper(request, required_scopes):
token_response=None
)
return token_info

return wrapper


Expand All @@ -255,12 +281,15 @@ def verify_bearer(bearer_info_func):
:param bearer_info_func: types.FunctionType
:rtype: types.FunctionType
"""

def wrapper(request, required_scopes):
return verify_authorization_token(request, bearer_info_func)

return wrapper


def verify_security(auth_funcs, required_scopes, function):

@functools.wraps(function)
def wrapper(request):
token_info = get_authorization_info(auth_funcs, request, required_scopes)
Expand All @@ -269,6 +298,7 @@ def wrapper(request):
request.context['user'] = token_info.get('sub', token_info.get('uid'))
request.context['token_info'] = token_info
return function(request)

return wrapper


Expand Down

0 comments on commit a0ac0fd

Please sign in to comment.