2018-11-11 12:41:48 +00:00
|
|
|
"""OAuth Client models"""
|
|
|
|
|
|
|
|
from django.db import models
|
2019-03-13 15:49:30 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2018-12-18 09:40:46 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-10-09 12:37:40 +00:00
|
|
|
from passbook.core.models import Source, UserSettings, UserSourceConnection
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook.sources.oauth.clients import get_client
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OAuthSource(Source):
|
|
|
|
"""Configuration for OAuth provider."""
|
|
|
|
|
|
|
|
provider_type = models.CharField(max_length=255)
|
|
|
|
request_token_url = models.CharField(blank=True, max_length=255)
|
|
|
|
authorization_url = models.CharField(max_length=255)
|
|
|
|
access_token_url = models.CharField(max_length=255)
|
|
|
|
profile_url = models.CharField(max_length=255)
|
|
|
|
consumer_key = models.TextField()
|
|
|
|
consumer_secret = models.TextField()
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.sources.oauth.forms.OAuthSourceForm'
|
2018-11-22 12:12:24 +00:00
|
|
|
|
2018-12-18 12:24:58 +00:00
|
|
|
@property
|
2019-10-13 14:47:05 +00:00
|
|
|
def login_button(self):
|
2019-10-07 14:33:48 +00:00
|
|
|
url = reverse_lazy('passbook_sources_oauth:oauth-client-login',
|
2019-02-25 13:10:10 +00:00
|
|
|
kwargs={'source_slug': self.slug})
|
2019-03-14 20:18:13 +00:00
|
|
|
return url, self.provider_type, self.name
|
2019-02-25 13:10:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def additional_info(self):
|
2019-04-29 20:32:22 +00:00
|
|
|
return "Callback URL: <pre>%s</pre>" % \
|
2019-10-07 14:33:48 +00:00
|
|
|
reverse_lazy('passbook_sources_oauth:oauth-client-callback',
|
2019-04-29 20:32:22 +00:00
|
|
|
kwargs={'source_slug': self.slug})
|
2018-12-18 12:24:58 +00:00
|
|
|
|
2019-10-09 10:47:14 +00:00
|
|
|
def user_settings(self) -> UserSettings:
|
2019-03-14 20:18:13 +00:00
|
|
|
icon_type = self.provider_type
|
|
|
|
if icon_type == 'azure ad':
|
|
|
|
icon_type = 'windows'
|
|
|
|
icon_class = 'fa fa-%s' % icon_type
|
2019-10-07 14:33:48 +00:00
|
|
|
view_name = 'passbook_sources_oauth:oauth-client-user'
|
2019-10-09 10:47:14 +00:00
|
|
|
return UserSettings(self.name, icon_class, reverse((view_name), kwargs={
|
2019-03-13 15:49:30 +00:00
|
|
|
'source_slug': self.slug
|
2019-10-09 10:47:14 +00:00
|
|
|
}))
|
2019-03-13 15:49:30 +00:00
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
class Meta:
|
|
|
|
|
2018-12-18 09:40:46 +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):
|
|
|
|
"""Abstract subclass of OAuthSource to specify GitHub Form"""
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.sources.oauth.forms.GitHubOAuthSourceForm'
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
|
|
|
verbose_name = _('GitHub OAuth Source')
|
|
|
|
verbose_name_plural = _('GitHub OAuth Sources')
|
|
|
|
|
|
|
|
|
|
|
|
class TwitterOAuthSource(OAuthSource):
|
|
|
|
"""Abstract subclass of OAuthSource to specify Twitter Form"""
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.sources.oauth.forms.TwitterOAuthSourceForm'
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
|
|
|
verbose_name = _('Twitter OAuth Source')
|
|
|
|
verbose_name_plural = _('Twitter OAuth Sources')
|
|
|
|
|
|
|
|
|
|
|
|
class FacebookOAuthSource(OAuthSource):
|
|
|
|
"""Abstract subclass of OAuthSource to specify Facebook Form"""
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.sources.oauth.forms.FacebookOAuthSourceForm'
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
|
|
|
verbose_name = _('Facebook OAuth Source')
|
|
|
|
verbose_name_plural = _('Facebook OAuth Sources')
|
|
|
|
|
|
|
|
|
|
|
|
class DiscordOAuthSource(OAuthSource):
|
|
|
|
"""Abstract subclass of OAuthSource to specify Discord Form"""
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.sources.oauth.forms.DiscordOAuthSourceForm'
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
|
|
|
verbose_name = _('Discord OAuth Source')
|
|
|
|
verbose_name_plural = _('Discord OAuth Sources')
|
|
|
|
|
|
|
|
|
|
|
|
class GoogleOAuthSource(OAuthSource):
|
|
|
|
"""Abstract subclass of OAuthSource to specify Google Form"""
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.sources.oauth.forms.GoogleOAuthSourceForm'
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
|
|
|
verbose_name = _('Google OAuth Source')
|
|
|
|
verbose_name_plural = _('Google OAuth Sources')
|
|
|
|
|
2019-03-14 20:18:13 +00:00
|
|
|
|
|
|
|
class AzureADOAuthSource(OAuthSource):
|
|
|
|
"""Abstract subclass of OAuthSource to specify AzureAD Form"""
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.sources.oauth.forms.AzureADOAuthSourceForm'
|
2019-03-14 20:18:13 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
abstract = True
|
|
|
|
verbose_name = _('Azure AD OAuth Source')
|
|
|
|
verbose_name_plural = _('Azure AD OAuth Sources')
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def api_client(self):
|
|
|
|
"""Get API Client"""
|
|
|
|
return get_client(self.source, self.access_token or '')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
2018-12-18 09:40:46 +00:00
|
|
|
verbose_name = _('User OAuth Source Connection')
|
|
|
|
verbose_name_plural = _('User OAuth Source Connections')
|