2018-11-11 12:41:48 +00:00
|
|
|
"""OAuth Client models"""
|
2020-07-20 13:11:27 +00:00
|
|
|
from typing import Optional, Type
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
from django.db import models
|
2020-07-20 13:11:27 +00:00
|
|
|
from django.forms import ModelForm
|
2019-03-13 15:49:30 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2020-02-16 11:34:33 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2020-02-20 12:51:41 +00:00
|
|
|
from passbook.core.models import Source, UserSourceConnection
|
2020-11-22 19:30:26 +00:00
|
|
|
from passbook.core.types import UILoginButton
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OAuthSource(Source):
|
2020-07-01 16:40:52 +00:00
|
|
|
"""Login using a Generic OAuth provider."""
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
provider_type = models.CharField(max_length=255)
|
2020-02-16 11:34:33 +00:00
|
|
|
request_token_url = models.CharField(
|
2020-05-20 11:59:56 +00:00
|
|
|
blank=True,
|
|
|
|
max_length=255,
|
|
|
|
verbose_name=_("Request Token URL"),
|
|
|
|
help_text=_(
|
|
|
|
"URL used to request the initial token. This URL is only required for OAuth 1."
|
|
|
|
),
|
2020-02-16 11:34:33 +00:00
|
|
|
)
|
|
|
|
authorization_url = models.CharField(
|
2020-05-20 11:59:56 +00:00
|
|
|
max_length=255,
|
|
|
|
verbose_name=_("Authorization URL"),
|
|
|
|
help_text=_("URL the user is redirect to to conest the flow."),
|
2020-02-16 11:34:33 +00:00
|
|
|
)
|
|
|
|
access_token_url = models.CharField(
|
2020-05-20 11:59:56 +00:00
|
|
|
max_length=255,
|
|
|
|
verbose_name=_("Access Token URL"),
|
|
|
|
help_text=_("URL used by passbook to retrive tokens."),
|
2020-02-16 11:34:33 +00:00
|
|
|
)
|
2020-05-20 11:47:58 +00:00
|
|
|
profile_url = models.CharField(
|
2020-05-20 11:59:56 +00:00
|
|
|
max_length=255,
|
|
|
|
verbose_name=_("Profile URL"),
|
|
|
|
help_text=_("URL used by passbook to get user information."),
|
|
|
|
)
|
2018-11-11 12:41:48 +00:00
|
|
|
consumer_key = models.TextField()
|
|
|
|
consumer_secret = models.TextField()
|
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 13:11:27 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.sources.oauth.forms import OAuthSourceForm
|
|
|
|
|
|
|
|
return OAuthSourceForm
|
2018-11-22 12:12:24 +00:00
|
|
|
|
2018-12-18 12:24:58 +00:00
|
|
|
@property
|
2020-02-20 12:51:41 +00:00
|
|
|
def ui_login_button(self) -> UILoginButton:
|
|
|
|
return UILoginButton(
|
|
|
|
url=reverse_lazy(
|
|
|
|
"passbook_sources_oauth:oauth-client-login",
|
|
|
|
kwargs={"source_slug": self.slug},
|
|
|
|
),
|
2020-02-23 12:53:16 +00:00
|
|
|
icon_path=f"passbook/sources/{self.provider_type}.svg",
|
2020-02-20 12:51:41 +00:00
|
|
|
name=self.name,
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
2019-02-25 13:10:10 +00:00
|
|
|
|
|
|
|
@property
|
2020-02-20 12:51:41 +00:00
|
|
|
def ui_additional_info(self) -> str:
|
|
|
|
url = reverse_lazy(
|
2019-12-31 11:51:16 +00:00
|
|
|
"passbook_sources_oauth:oauth-client-callback",
|
|
|
|
kwargs={"source_slug": self.slug},
|
|
|
|
)
|
2020-02-20 12:51:41 +00:00
|
|
|
return f"Callback URL: <pre>{url}</pre>"
|
2018-12-18 12:24:58 +00:00
|
|
|
|
2020-02-20 12:51:41 +00:00
|
|
|
@property
|
2020-11-22 19:30:26 +00:00
|
|
|
def ui_user_settings(self) -> Optional[str]:
|
2019-12-31 11:51:16 +00:00
|
|
|
view_name = "passbook_sources_oauth:oauth-client-user"
|
2020-11-22 19:30:26 +00:00
|
|
|
return reverse(view_name, kwargs={"source_slug": self.slug})
|
2019-03-13 15:49:30 +00:00
|
|
|
|
2020-05-20 07:17:06 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"OAuth Source {self.name}"
|
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
class Meta:
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("Generic OAuth Source")
|
|
|
|
verbose_name_plural = _("Generic OAuth Sources")
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
|
2018-12-18 09:40:46 +00:00
|
|
|
class GitHubOAuthSource(OAuthSource):
|
2020-07-01 16:40:52 +00:00
|
|
|
"""Social Login using GitHub.com or a GitHub-Enterprise Instance."""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 13:11:27 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.sources.oauth.forms import GitHubOAuthSourceForm
|
|
|
|
|
|
|
|
return GitHubOAuthSourceForm
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("GitHub OAuth Source")
|
|
|
|
verbose_name_plural = _("GitHub OAuth Sources")
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
|
2020-09-25 21:58:58 +00:00
|
|
|
class TwitterOAuthSource(OAuthSource):
|
|
|
|
"""Social Login using Twitter.com"""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-09-25 21:58:58 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.sources.oauth.forms import TwitterOAuthSourceForm
|
|
|
|
|
|
|
|
return TwitterOAuthSourceForm
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-09-25 21:58:58 +00:00
|
|
|
class Meta:
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-09-25 21:58:58 +00:00
|
|
|
abstract = True
|
|
|
|
verbose_name = _("Twitter OAuth Source")
|
|
|
|
verbose_name_plural = _("Twitter OAuth Sources")
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FacebookOAuthSource(OAuthSource):
|
2020-07-01 16:40:52 +00:00
|
|
|
"""Social Login using Facebook.com."""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 13:11:27 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.sources.oauth.forms import FacebookOAuthSourceForm
|
|
|
|
|
|
|
|
return FacebookOAuthSourceForm
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("Facebook OAuth Source")
|
|
|
|
verbose_name_plural = _("Facebook OAuth Sources")
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DiscordOAuthSource(OAuthSource):
|
2020-07-01 16:40:52 +00:00
|
|
|
"""Social Login using Discord."""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 13:11:27 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.sources.oauth.forms import DiscordOAuthSourceForm
|
|
|
|
|
|
|
|
return DiscordOAuthSourceForm
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("Discord OAuth Source")
|
|
|
|
verbose_name_plural = _("Discord OAuth Sources")
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GoogleOAuthSource(OAuthSource):
|
2020-07-01 16:40:52 +00:00
|
|
|
"""Social Login using Google or Gsuite."""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 13:11:27 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.sources.oauth.forms import GoogleOAuthSourceForm
|
|
|
|
|
|
|
|
return GoogleOAuthSourceForm
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("Google OAuth Source")
|
|
|
|
verbose_name_plural = _("Google OAuth Sources")
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2019-03-14 20:18:13 +00:00
|
|
|
|
|
|
|
class AzureADOAuthSource(OAuthSource):
|
2020-07-01 16:40:52 +00:00
|
|
|
"""Social Login using Azure AD."""
|
2019-03-14 20:18:13 +00:00
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 13:11:27 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.sources.oauth.forms import AzureADOAuthSourceForm
|
|
|
|
|
|
|
|
return AzureADOAuthSourceForm
|
2019-03-14 20:18:13 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("Azure AD OAuth Source")
|
|
|
|
verbose_name_plural = _("Azure AD OAuth Sources")
|
2019-03-14 20:18:13 +00:00
|
|
|
|
2020-05-20 11:59:56 +00:00
|
|
|
|
2020-05-20 11:47:58 +00:00
|
|
|
class OpenIDOAuthSource(OAuthSource):
|
2020-07-01 16:40:52 +00:00
|
|
|
"""Login using a Generic OpenID-Connect compliant provider."""
|
2020-05-20 11:47:58 +00:00
|
|
|
|
2020-09-29 08:32:41 +00:00
|
|
|
@property
|
2020-07-20 13:11:27 +00:00
|
|
|
def form(self) -> Type[ModelForm]:
|
|
|
|
from passbook.sources.oauth.forms import OAuthSourceForm
|
|
|
|
|
|
|
|
return OAuthSourceForm
|
2020-05-20 11:47:58 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
|
|
|
verbose_name = _("OpenID OAuth Source")
|
|
|
|
verbose_name_plural = _("OpenID OAuth Sources")
|
|
|
|
|
2019-03-14 20:18:13 +00:00
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
class UserOAuthSourceConnection(UserSourceConnection):
|
|
|
|
"""Authorized remote OAuth provider."""
|
|
|
|
|
|
|
|
identifier = models.CharField(max_length=255)
|
|
|
|
access_token = models.TextField(blank=True, null=True, default=None)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.access_token = self.access_token or None
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
verbose_name = _("User OAuth Source Connection")
|
|
|
|
verbose_name_plural = _("User OAuth Source Connections")
|