Make database, oauth, smtp server, mail verificaiton configurable in .env
This commit is contained in:
parent
9b5fe1627d
commit
712d9a843e
8 changed files with 109 additions and 31 deletions
5
allauth_oauth/__init__.py
Normal file
5
allauth_oauth/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# This file is part of photo21
|
||||
# Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
default_app_config = "allauth_oauth.apps.AllauthOAuthConfig"
|
||||
42
allauth_oauth/provider.py
Normal file
42
allauth_oauth/provider.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# From https://gitlab.crans.org/bde/allauth-note-kfet
|
||||
# Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from allauth.account.models import EmailAddress
|
||||
from allauth.socialaccount.providers.base import ProviderAccount
|
||||
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
|
||||
|
||||
|
||||
class OAuthAccount(ProviderAccount):
|
||||
def to_str(self):
|
||||
return self.account.extra_data.get("username")
|
||||
|
||||
|
||||
class OAuthProvider(OAuth2Provider):
|
||||
id = "oauth"
|
||||
name = "OAuth"
|
||||
account_class = OAuthAccount
|
||||
|
||||
def extract_uid(self, data):
|
||||
return str(data["username"])
|
||||
|
||||
def extract_common_fields(self, data):
|
||||
return dict(
|
||||
email=data.get("email"),
|
||||
username=data.get("username"),
|
||||
last_name=data.get("last_name"),
|
||||
first_name=data.get("first_name"),
|
||||
)
|
||||
|
||||
def get_default_scope(self):
|
||||
return ["read"]
|
||||
|
||||
def extract_email_addresses(self, data):
|
||||
ret = []
|
||||
email = data.get("email")
|
||||
if email:
|
||||
ret.append(EmailAddress(email=email, verified=True, primary=True))
|
||||
return ret
|
||||
|
||||
|
||||
provider_classes = [OAuthProvider]
|
||||
9
allauth_oauth/urls.py
Normal file
9
allauth_oauth/urls.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# This file is part of photo21
|
||||
# Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
|
||||
|
||||
from .provider import OAuthProvider
|
||||
|
||||
urlpatterns = default_urlpatterns(OAuthProvider)
|
||||
50
allauth_oauth/views.py
Normal file
50
allauth_oauth/views.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
# From https://gitlab.crans.org/bde/allauth-note-kfet
|
||||
# Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import requests
|
||||
from allauth.socialaccount import app_settings
|
||||
from allauth.socialaccount.providers.oauth2.views import (
|
||||
OAuth2Adapter,
|
||||
OAuth2CallbackView,
|
||||
OAuth2LoginView,
|
||||
)
|
||||
|
||||
from .provider import OAuthProvider
|
||||
|
||||
|
||||
class OAuthAdapter(OAuth2Adapter):
|
||||
provider_id = OAuthProvider.id
|
||||
|
||||
def complete_login(self, request, app, token, **kwargs):
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token.token}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
extra_data = requests.get(self.profile_url, headers=headers)
|
||||
|
||||
return self.get_provider().sociallogin_from_response(request, extra_data.json())
|
||||
|
||||
@property
|
||||
def settings(self):
|
||||
return app_settings.PROVIDERS.get(self.provider_id, {})
|
||||
|
||||
@property
|
||||
def domain(self):
|
||||
return self.settings.get("DOMAIN", "")
|
||||
|
||||
@property
|
||||
def access_token_url(self):
|
||||
return f"https://{self.domain}/o/token/"
|
||||
|
||||
@property
|
||||
def authorize_url(self):
|
||||
return f"https://{self.domain}/o/authorize/"
|
||||
|
||||
@property
|
||||
def profile_url(self):
|
||||
return f"https://{self.domain}/api/me/"
|
||||
|
||||
|
||||
oauth2_login = OAuth2LoginView.adapter_view(OAuthAdapter)
|
||||
oauth2_callback = OAuth2CallbackView.adapter_view(OAuthAdapter)
|
||||
Loading…
Add table
Add a link
Reference in a new issue