Decouple oauth methods from app with a namedtuple

This commit is contained in:
Sam Chow 2018-05-25 13:49:36 -04:00
parent d45b925155
commit e967fde3ae
7 changed files with 20 additions and 7 deletions

View file

@ -7,6 +7,7 @@ from abc import ABCMeta, abstractmethod
from six import add_metaclass
from util import get_app_url
from util.config import URLSchemeAndHostname
logger = logging.getLogger(__name__)
@ -111,9 +112,9 @@ class OAuthService(object):
return self.authorize_endpoint().with_params(params).to_url()
def get_redirect_uri(self, app_config, redirect_suffix=''):
return '%s://%s/oauth2/%s/callback%s' % (app_config['PREFERRED_URL_SCHEME'],
app_config['SERVER_HOSTNAME'],
def get_redirect_uri(self, url_scheme_and_hostname, redirect_suffix=''):
return '%s://%s/oauth2/%s/callback%s' % (url_scheme_and_hostname.url_scheme,
url_scheme_and_hostname.hostname,
self.service_id(),
redirect_suffix)
@ -153,10 +154,11 @@ class OAuthService(object):
def exchange_code(self, app_config, http_client, code, form_encode=False, redirect_suffix='',
client_auth=False):
""" Exchanges an OAuth access code for associated OAuth token and other data. """
url_scheme_and_hostname = URLSchemeAndHostname(app_config['PREFERRED_URL_SCHEME'], app_config['SERVER_HOSTNAME'])
payload = {
'code': code,
'grant_type': 'authorization_code',
'redirect_uri': self.get_redirect_uri(app_config, redirect_suffix)
'redirect_uri': self.get_redirect_uri(url_scheme_and_hostname, redirect_suffix)
}
headers = {