Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make abstract async security handler
  • Loading branch information
cognifloyd committed Jun 4, 2020
commit c6e4f387dff6ed4eea4173ed80ee8ec7d946a7bd
37 changes: 37 additions & 0 deletions connexion/security/aiohttp_security_handler_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import asyncio
import logging

import aiohttp

from .async_security_handler_factory import AsyncSecurityHandlerFactory

logger = logging.getLogger('connexion.api.security')


class AioHttpSecurityHandlerFactory(AsyncSecurityHandlerFactory):
def __init__(self, pass_context_arg_name):
AioHttpSecurityHandlerFactory.__init__(self, pass_context_arg_name)
self.client_session = None

def get_token_info_remote(self, token_info_url):
"""
Return a function which will call `token_info_url` to retrieve token info.

Returned function must accept oauth token in parameter.
It must return a token_info dict in case of success, None otherwise.

:param token_info_url: Url to get information about the token
:type token_info_url: str
:rtype: types.FunctionType
"""
@asyncio.coroutine
def wrapper(token):
if not self.client_session:
# Must be created in a coroutine
self.client_session = aiohttp.ClientSession()
headers = {'Authorization': 'Bearer {}'.format(token)}
token_request = yield from self.client_session.get(token_info_url, headers=headers, timeout=5)
if token_request.status != 200:
return None
return token_request.json()
return wrapper
19 changes: 3 additions & 16 deletions connexion/security/async_security_handler_factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import abc
import asyncio
import functools
import logging
Expand All @@ -10,11 +11,7 @@
logger = logging.getLogger('connexion.api.security')


class AioHttpSecurityHandlerFactory(SecurityHandlerFactory):
def __init__(self, pass_context_arg_name):
SecurityHandlerFactory.__init__(self, pass_context_arg_name)
self.client_session = None

class AsyncSecurityHandlerFactory(SecurityHandlerFactory):
def _generic_check(self, func, exception_msg):
need_to_add_context, need_to_add_required_scopes = self._need_to_add_context_or_scopes(func)

Expand Down Expand Up @@ -87,6 +84,7 @@ def wrapper(request):

return wrapper

@abc.abstractmethod
def get_token_info_remote(self, token_info_url):
"""
Return a function which will call `token_info_url` to retrieve token info.
Expand All @@ -98,14 +96,3 @@ def get_token_info_remote(self, token_info_url):
:type token_info_url: str
:rtype: types.FunctionType
"""
@asyncio.coroutine
def wrapper(token):
if not self.client_session:
# Must be created in a coroutine
self.client_session = aiohttp.ClientSession()
headers = {'Authorization': 'Bearer {}'.format(token)}
token_request = yield from self.client_session.get(token_info_url, headers=headers, timeout=5)
if token_request.status != 200:
return None
return token_request.json()
return wrapper