Switch base classes in OAuth to use ABC

This commit is contained in:
Joseph Schorr 2017-01-24 15:20:03 -05:00
parent a9791ea419
commit adb2ff0b81
2 changed files with 33 additions and 12 deletions

View file

@ -1,6 +1,9 @@
import logging
import urllib
from abc import ABCMeta, abstractmethod
from six import add_metaclass
from util import get_app_url
logger = logging.getLogger(__name__)
@ -13,36 +16,43 @@ class OAuthGetUserInfoException(Exception):
""" Exception raised if a call to get user information fails. """
pass
@add_metaclass(ABCMeta)
class OAuthService(object):
""" A base class for defining an external service, exposed via OAuth. """
def __init__(self, config, key_name):
self.key_name = key_name
self.config = config.get(key_name) or {}
@abstractmethod
def service_id(self):
""" The internal ID for this service. Must match the URL portion for the service, e.g. `github`
"""
raise NotImplementedError
pass
@abstractmethod
def service_name(self):
""" The user-readable name for the service, e.g. `GitHub`"""
raise NotImplementedError
pass
@abstractmethod
def token_endpoint(self):
""" The endpoint at which the OAuth code can be exchanged for a token. """
raise NotImplementedError
pass
@abstractmethod
def user_endpoint(self):
""" The endpoint at which user information can be looked up. """
raise NotImplementedError
pass
@abstractmethod
def validate_client_id_and_secret(self, http_client, app_config):
""" Performs validation of the client ID and secret, raising an exception on failure. """
raise NotImplementedError
pass
@abstractmethod
def authorize_endpoint(self):
""" Endpoint for authorization. """
raise NotImplementedError
pass
def requires_form_encoding(self):
""" Returns True if form encoding is necessary for the exchange_code_for_token call. """