2020-07-09 21:08:28 +00:00
|
|
|
"""OAuth Base views"""
|
2020-09-25 21:58:58 +00:00
|
|
|
from typing import Optional, Type
|
2020-07-09 21:08:28 +00:00
|
|
|
|
2020-09-25 22:34:57 +00:00
|
|
|
from django.http.request import HttpRequest
|
|
|
|
|
2020-09-25 21:58:58 +00:00
|
|
|
from passbook.sources.oauth.clients.base import BaseOAuthClient
|
|
|
|
from passbook.sources.oauth.clients.oauth1 import OAuthClient
|
|
|
|
from passbook.sources.oauth.clients.oauth2 import OAuth2Client
|
2020-07-09 21:08:28 +00:00
|
|
|
from passbook.sources.oauth.models import OAuthSource
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class OAuthClientMixin:
|
|
|
|
"Mixin for getting OAuth client for a source."
|
|
|
|
|
2020-09-25 22:34:57 +00:00
|
|
|
request: HttpRequest # Set by View class
|
|
|
|
|
2020-09-25 21:58:58 +00:00
|
|
|
client_class: Optional[Type[BaseOAuthClient]] = None
|
2020-07-09 21:08:28 +00:00
|
|
|
|
2020-09-25 22:34:57 +00:00
|
|
|
def get_client(self, source: OAuthSource, **kwargs) -> BaseOAuthClient:
|
2020-07-09 21:08:28 +00:00
|
|
|
"Get instance of the OAuth client for this source."
|
|
|
|
if self.client_class is not None:
|
|
|
|
# pylint: disable=not-callable
|
2020-09-25 22:34:57 +00:00
|
|
|
return self.client_class(source, self.request, **kwargs)
|
2020-09-25 21:58:58 +00:00
|
|
|
if source.request_token_url:
|
2020-09-25 22:34:57 +00:00
|
|
|
return OAuthClient(source, self.request, **kwargs)
|
|
|
|
return OAuth2Client(source, self.request, **kwargs)
|