oauth2.py 4.01 KB
Newer Older
1 2 3 4 5 6 7 8 9
""" Taken from https://github.com/maraujop/requests-oauth2 """

from urllib.parse import parse_qs, quote, urlencode

import requests

from .errors import ConfigurationError


Daniele Venzano's avatar
Daniele Venzano committed
10
class OAuth2:
11 12 13 14 15 16 17 18 19 20 21 22
    """Main OAuth2 class."""
    client_id = None
    client_secret = None
    site = None
    redirect_uri = None
    authorization_url = '/oauth/authorize'
    token_url = '/oauth/token'
    revoke_url = '/oauth2/revoke'
    scope_sep = None

    def __init__(self, client_id=None, client_secret=None, site=None,
                 redirect_uri=None, authorization_url=None,
Daniele Venzano's avatar
Daniele Venzano committed
23
                 token_url=None, revoke_url=None, scope_sep=None):  # pylint: disable=too-many-arguments
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        """
        Initializes the hook with OAuth2 parameters
        """
        if client_id is not None:
            self.client_id = client_id
        if client_secret is not None:
            self.client_secret = client_secret
        if site is not None:
            self.site = site
        if redirect_uri is not None:
            self.redirect_uri = redirect_uri
        if authorization_url is not None:
            self.authorization_url = authorization_url
        if token_url is not None:
            self.token_url = token_url
        if revoke_url is not None:
            self.revoke_url = revoke_url
        if scope_sep is not None:
            self.scope_sep = scope_sep

    def _check_configuration(self, *attrs):
        """Check that each named attr has been configured
        """
        for attr in attrs:
            if getattr(self, attr, None) is None:
                raise ConfigurationError("{} not configured".format(attr))

    def _make_request(self, url, **kwargs):
        """
        Make a request to an OAuth2 endpoint
        """
        response = requests.post(url, **kwargs)
        try:
            return response.json()
        except ValueError:
            pass
        return parse_qs(response.content)

    def authorize_url(self, scope='', **kwargs):
        """
        Returns the url to redirect the user to for user consent
        """
        self._check_configuration("site", "authorization_url", "redirect_uri",
                                  "client_id")
        if isinstance(scope, (list, tuple, set, frozenset)):
            self._check_configuration("scope_sep")
            scope = self.scope_sep.join(scope)
        oauth_params = {
            'redirect_uri': self.redirect_uri,
            'client_id': self.client_id,
            'scope': scope,
        }
        oauth_params.update(kwargs)
        return "%s%s?%s" % (self.site, quote(self.authorization_url),
                            urlencode(oauth_params))

    def get_token(self, code, headers=None, **kwargs):
        """
        Requests an access token
        """
        self._check_configuration("site", "token_url", "redirect_uri",
                                  "client_id", "client_secret")
        url = "%s%s" % (self.site, quote(self.token_url))
        data = {
            'redirect_uri': self.redirect_uri,
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'code': code,
        }
        data.update(kwargs)

        return self._make_request(url, data=data, headers=headers)

    def refresh_token(self, headers=None, **kwargs):
        """
        Request a refreshed token
        """
        self._check_configuration("site", "token_url", "client_id",
                                  "client_secret")
        url = "%s%s" % (self.site, quote(self.token_url))
        data = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
        }
        data.update(kwargs)

        return self._make_request(url, data=data, headers=headers)

    def revoke_token(self, token, headers=None, **kwargs):
        """
        Revoke an access token
        """
        self._check_configuration("site", "revoke_uri")
        url = "%s%s" % (self.site, quote(self.revoke_url))
        data = {'token': token}
        data.update(kwargs)

        return self._make_request(url, data=data, headers=headers)