diff --git a/README.md b/README.md index 9edddbc..3a83eaf 100644 --- a/README.md +++ b/README.md @@ -92,16 +92,7 @@ production néccessite **une installation de Debian Bullseye ou plus récent**. $ sudo chmod g+rwx -R static media ``` -3. **Création d'un environment de travail Python décorrélé du système.** - - ```bash - $ python3 -m venv venv --system-site-packages - $ source venv/bin/activate # entrer dans l'environnement - (env)$ pip3 install -r requirements.txt - (env)$ deactivate # sortir de l'environnement - ``` - -4. **Configuration de UWSGI et NGINX.** +3. **Configuration de UWSGI et NGINX.** ```bash $ sudo cp docs/uwsgi_photos.ini /etc/uwsgi/apps-available/uwsgi_photos.ini @@ -110,7 +101,7 @@ production néccessite **une installation de Debian Bullseye ou plus récent**. $ sudo ln -s /etc/nginx/sites-available/photos.crans.org /etc/nginx/sites-enabled/ ``` -5. **Base de données.** +4. **Base de données.** En production on utilise PostgreSQL. ```bash @@ -120,17 +111,17 @@ production néccessite **une installation de Debian Bullseye ou plus récent**. postgres=# CREATE DATABASE photo21 OWNER photo21; ``` -6. **Migrations et collecte des fichiers statiques**, +5. **Migrations et collecte des fichiers statiques**, ``` - $ sudo -u www-data ./venv/bin/python ./manage.py collectstatic - $ sudo -u www-data ./venv/bin/python ./manage.py check - $ sudo -u www-data ./venv/bin/python ./manage.py migrate - $ sudo -u www-data ./venv/bin/python ./manage.py loaddata initial - $ sudo ./venv/bin/python ./manage.py compilemessages + $ sudo -u www-data ./manage.py collectstatic + $ sudo -u www-data ./manage.py check + $ sudo -u www-data ./manage.py migrate + $ sudo -u www-data ./manage.py loaddata initial + $ sudo ./manage.py compilemessages ``` -7. *Enjoy \o/* +6. *Enjoy \o/* ## Documentation diff --git a/allauth_note_kfet/__init__.py b/allauth_note_kfet/__init__.py new file mode 100644 index 0000000..6c9e378 --- /dev/null +++ b/allauth_note_kfet/__init__.py @@ -0,0 +1,3 @@ +# 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 diff --git a/allauth_note_kfet/provider.py b/allauth_note_kfet/provider.py new file mode 100644 index 0000000..d2ce6a5 --- /dev/null +++ b/allauth_note_kfet/provider.py @@ -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 NoteKfetAccount(ProviderAccount): + def to_str(self): + return self.account.extra_data.get("username") + + +class NoteKfetProvider(OAuth2Provider): + id = "notekfet" + name = "Note Kfet" + account_class = NoteKfetAccount + + 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 = [NoteKfetProvider] diff --git a/allauth_note_kfet/urls.py b/allauth_note_kfet/urls.py new file mode 100644 index 0000000..e1bf561 --- /dev/null +++ b/allauth_note_kfet/urls.py @@ -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 NoteKfetProvider + +urlpatterns = default_urlpatterns(NoteKfetProvider) diff --git a/allauth_note_kfet/views.py b/allauth_note_kfet/views.py new file mode 100644 index 0000000..86a16ee --- /dev/null +++ b/allauth_note_kfet/views.py @@ -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 NoteKfetProvider + + +class NoteKfetOAuth2Adapter(OAuth2Adapter): + provider_id = NoteKfetProvider.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", "note.crans.org") + + @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(NoteKfetOAuth2Adapter) +oauth2_callback = OAuth2CallbackView.adapter_view(NoteKfetOAuth2Adapter) diff --git a/docs/uwsgi_photos.ini b/docs/uwsgi_photos.ini index 60cfa48..ce622a7 100644 --- a/docs/uwsgi_photos.ini +++ b/docs/uwsgi_photos.ini @@ -9,8 +9,6 @@ gid = www-data # Django-related settings # the base directory (full path) chdir = /var/www/photos/photo21 -# the virtualenv (full path) -home = /var/www/photos/photo21/venv wsgi-file = /var/www/photos/photo21/photo21/wsgi.py plugin = python3 # process-related settings diff --git a/requirements.txt b/requirements.txt index cf9dcaf..79b6959 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,4 @@ django-allauth>=0.44 django-crispy-forms~=1.7 Django>=2.2.20 ExifRead>=2.1.2 -git+https://gitlab.crans.org/bde/allauth-note-kfet.git Pillow>=6.0.0 \ No newline at end of file diff --git a/tox.ini b/tox.ini index 885d2da..41246da 100644 --- a/tox.ini +++ b/tox.ini @@ -26,7 +26,7 @@ deps = pep8-naming pyflakes commands = - flake8 photo21 photologue + flake8 allauth_note_kfet photo21 photologue [flake8] ignore = W503, I100, I101