2018-11-22 12:12:24 +00:00
|
|
|
"""passbook oauth_client forms"""
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
from passbook.admin.forms.inlet import INLET_FORM_FIELDS
|
|
|
|
from passbook.channels.in_oauth.models import OAuthInlet
|
|
|
|
from passbook.channels.in_oauth.types.manager import MANAGER
|
2018-11-22 12:12:24 +00:00
|
|
|
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class OAuthInletForm(forms.ModelForm):
|
|
|
|
"""OAuthInlet Form"""
|
2018-11-22 12:12:24 +00:00
|
|
|
|
2018-12-18 09:40:46 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-12-31 11:51:16 +00:00
|
|
|
if hasattr(self.Meta, "overrides"):
|
|
|
|
for overide_field, overide_value in getattr(self.Meta, "overrides").items():
|
2018-12-18 09:40:46 +00:00
|
|
|
self.fields[overide_field].initial = overide_value
|
2019-12-31 11:51:16 +00:00
|
|
|
self.fields[overide_field].widget.attrs["readonly"] = "readonly"
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2018-11-22 12:12:24 +00:00
|
|
|
class Meta:
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
model = OAuthInlet
|
|
|
|
fields = INLET_FORM_FIELDS + [
|
2019-12-31 11:51:16 +00:00
|
|
|
"provider_type",
|
|
|
|
"request_token_url",
|
|
|
|
"authorization_url",
|
|
|
|
"access_token_url",
|
|
|
|
"profile_url",
|
|
|
|
"consumer_key",
|
|
|
|
"consumer_secret",
|
|
|
|
]
|
2018-12-18 09:40:46 +00:00
|
|
|
widgets = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"name": forms.TextInput(),
|
|
|
|
"consumer_key": forms.TextInput(),
|
|
|
|
"consumer_secret": forms.TextInput(),
|
|
|
|
"provider_type": forms.Select(choices=MANAGER.get_name_tuple()),
|
2018-12-18 09:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class GitHubOAuthInletForm(OAuthInletForm):
|
|
|
|
"""OAuth Inlet form with pre-determined URL for GitHub"""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class Meta(OAuthInletForm.Meta):
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
overrides = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"provider_type": "github",
|
|
|
|
"request_token_url": "",
|
|
|
|
"authorization_url": "https://github.com/login/oauth/authorize",
|
|
|
|
"access_token_url": "https://github.com/login/oauth/access_token",
|
2020-02-18 20:49:53 +00:00
|
|
|
"profile_url": "https://api.github.com/user",
|
2018-12-18 09:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class TwitterOAuthInletForm(OAuthInletForm):
|
|
|
|
"""OAuth Inlet form with pre-determined URL for Twitter"""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class Meta(OAuthInletForm.Meta):
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
overrides = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"provider_type": "twitter",
|
|
|
|
"request_token_url": "https://api.twitter.com/oauth/request_token",
|
|
|
|
"authorization_url": "https://api.twitter.com/oauth/authenticate",
|
|
|
|
"access_token_url": "https://api.twitter.com/oauth/access_token",
|
2020-02-18 20:49:53 +00:00
|
|
|
"profile_url": (
|
|
|
|
"https://api.twitter.com/1.1/account/"
|
|
|
|
"verify_credentials.json?include_email=true"
|
|
|
|
),
|
2018-12-18 09:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class FacebookOAuthInletForm(OAuthInletForm):
|
|
|
|
"""OAuth Inlet form with pre-determined URL for Facebook"""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class Meta(OAuthInletForm.Meta):
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
overrides = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"provider_type": "facebook",
|
|
|
|
"request_token_url": "",
|
|
|
|
"authorization_url": "https://www.facebook.com/v2.8/dialog/oauth",
|
|
|
|
"access_token_url": "https://graph.facebook.com/v2.8/oauth/access_token",
|
2020-02-18 20:49:53 +00:00
|
|
|
"profile_url": "https://graph.facebook.com/v2.8/me?fields=name,email,short_name",
|
2018-12-18 09:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class DiscordOAuthInletForm(OAuthInletForm):
|
|
|
|
"""OAuth Inlet form with pre-determined URL for Discord"""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class Meta(OAuthInletForm.Meta):
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
overrides = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"provider_type": "discord",
|
|
|
|
"request_token_url": "",
|
|
|
|
"authorization_url": "https://discordapp.com/api/oauth2/authorize",
|
|
|
|
"access_token_url": "https://discordapp.com/api/oauth2/token",
|
2020-02-18 20:49:53 +00:00
|
|
|
"profile_url": "https://discordapp.com/api/users/@me",
|
2018-12-18 09:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class GoogleOAuthInletForm(OAuthInletForm):
|
|
|
|
"""OAuth Inlet form with pre-determined URL for Google"""
|
2018-12-18 09:40:46 +00:00
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class Meta(OAuthInletForm.Meta):
|
2018-12-18 09:40:46 +00:00
|
|
|
|
|
|
|
overrides = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"provider_type": "google",
|
|
|
|
"request_token_url": "",
|
|
|
|
"authorization_url": "https://accounts.google.com/o/oauth2/auth",
|
|
|
|
"access_token_url": "https://accounts.google.com/o/oauth2/token",
|
2020-02-18 20:49:53 +00:00
|
|
|
"profile_url": "https://www.googleapis.com/oauth2/v1/userinfo",
|
2018-12-18 09:40:46 +00:00
|
|
|
}
|
2019-03-14 20:18:55 +00:00
|
|
|
|
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class AzureADOAuthInletForm(OAuthInletForm):
|
|
|
|
"""OAuth Inlet form with pre-determined URL for AzureAD"""
|
2019-03-14 20:18:55 +00:00
|
|
|
|
2020-05-15 20:15:01 +00:00
|
|
|
class Meta(OAuthInletForm.Meta):
|
2019-03-14 20:18:55 +00:00
|
|
|
|
|
|
|
overrides = {
|
2020-02-23 18:42:57 +00:00
|
|
|
"provider_type": "azure-ad",
|
2019-12-31 11:51:16 +00:00
|
|
|
"request_token_url": "",
|
|
|
|
"authorization_url": "https://login.microsoftonline.com/common/oauth2/authorize",
|
|
|
|
"access_token_url": "https://login.microsoftonline.com/common/oauth2/token",
|
2020-02-18 20:49:53 +00:00
|
|
|
"profile_url": "https://graph.windows.net/myorganization/me?api-version=1.6",
|
2019-03-14 20:18:55 +00:00
|
|
|
}
|