Self hosted FLOSS fitness/workout, nutrition and weight tracker
fix(auth): hide Google OAuth behind configuration flag
pankaj-basnetc572d3e35e7f
diff --git a/pyproject.toml b/pyproject.tomlindex 5a266bfa0..c06eb54d9 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -168,7 +168,7 @@ line-ending = "auto"src_paths = ["wger", "extras"]sections = ["FUTURE", "STDLIB", "DJANGO", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]-skip = ["extras", "build", "dist", "node_modules", "migrations", "docs", "settings", "apps.py"]+skip = ["extras", "build", "dist", "node_modules", "migrations", "docs", "settings", "apps.py", "venv", ".venv", ]# If set to true - ensures that if a star import is present, nothing else is# imported from that namespace.combine_star = falsediff --git a/settings/ci.py b/settings/ci.pyindex 04423b09c..5c83210ed 100644--- a/settings/ci.py+++ b/settings/ci.py@@ -1,16 +1,18 @@-# This file is part of wger Workout Manager.+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2026 wger Team#-# wger Workout Manager is free software: you can redistribute it and/or modify-# it under the terms of the GNU Affero General Public License as published by-# the Free Software Foundation, either version 3 of the License, or-# (at your option) any later version.+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.#-# wger Workout Manager is distributed in the hope that it will be useful,-# but WITHOUT ANY WARRANTY; without even the implied warranty of-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-# GNU General Public License for more details.+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.#-# You should have received a copy of the GNU Affero General Public License+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.# ruff: noqa: F405, F403@@ -47,6 +49,7 @@ WGER_SETTINGS['ALLOW_GUEST_USERS'] = TrueWGER_SETTINGS['ALLOW_UPLOAD_VIDEOS'] = FalseWGER_SETTINGS['MIN_ACCOUNT_AGE_TO_TRUST'] = 21 # in daysWGER_SETTINGS['EXERCISE_CACHE_TTL'] = 3600 # in seconds+WGER_SETTINGS['USE_SOCIAL_AUTH'] = FalseDATABASES = {'default': {diff --git a/settings/main.py b/settings/main.pyindex 9ee6a5b04..0007da87d 100644--- a/settings/main.py+++ b/settings/main.py@@ -259,6 +259,12 @@ AXES_IPWARE_META_PRECEDENCE_ORDER = env.list('AXES_IPWARE_META_PRECEDENCE_ORDER', default=['REMOTE_ADDR'])+#+# Django-allauth+# Dj-rest-auth+#+WGER_SETTINGS['USE_SOCIAL_AUTH'] = env.bool('USE_SOCIAL_AUTH', False)+## Django Rest Framework SimpleJWT#diff --git a/settings/settings_global.py b/settings/settings_global.pyindex b4c34fccd..62a91ed0e 100644--- a/settings/settings_global.py+++ b/settings/settings_global.py@@ -571,8 +571,25 @@ WGER_SETTINGS = {# Trophy system settings'TROPHIES_ENABLED': True,'TROPHIES_INACTIVE_USER_DAYS': 30, # Days of inactivity before skipping trophy evaluation+ # Social authentication/Oauth+ 'USE_SOCIAL_AUTH': False,}+#+# Django-allauth+# Dj-rest-auth+#+if WGER_SETTINGS.get('USE_SOCIAL_AUTH', False):+ INSTALLED_APPS += [++ 'allauth.socialaccount',+ 'allauth.socialaccount.providers.google',++ 'dj_rest_auth',+ 'dj_rest_auth.registration',++ ]+## Auth Proxy Authentication#diff --git a/wger/core/api/views.py b/wger/core/api/views.pyindex 0cc4190e7..f05d10c1e 100644--- a/wger/core/api/views.py+++ b/wger/core/api/views.py@@ -21,6 +21,7 @@ import logging# Djangofrom django.conf import settingsfrom django.contrib.auth.models import User+from django.contrib.sites.models import Sitefrom django.http import (HttpResponseForbidden,JsonResponse,@@ -55,9 +56,12 @@ from rest_framework.permissions import (IsAuthenticated,)from rest_framework.response import Response-from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter-from allauth.socialaccount.providers.oauth2.client import OAuth2Client-from dj_rest_auth.registration.views import SocialLoginView+++if settings.WGER_SETTINGS.get('USE_SOCIAL_AUTH'):+ from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter+ from allauth.socialaccount.providers.oauth2.client import OAuth2Client+ from dj_rest_auth.registration.views import SocialLoginView# wger# The per-app powersync modules are imported for their side effect: each one@@ -374,13 +378,23 @@ class UserAPIRegistrationViewSet(viewsets.ViewSet):status=status.HTTP_201_CREATED,)-class GoogleLogin(SocialLoginView):- """- REST endpoint to exchange a Google Access Token for a wger API JWT.- """- adapter_class = GoogleOAuth2Adapter- client_class = OAuth2Client- callback_url = "http://127.0.0.1:8000/accounts/google/login/callback/"++if settings.WGER_SETTINGS.get('USE_SOCIAL_AUTH'):++ class GoogleLogin(SocialLoginView):+ """+ REST endpoint to exchange a Google Access Token for a wger API JWT.+ """++ adapter_class = GoogleOAuth2Adapter+ client_class = OAuth2Client++ @property+ def callback_url():+ base_url = Site.objects.get_current()+ base_url_domain = base_url.domain+ return f'http://${base_url_domain}/accounts/google/login/callback/'+class LanguageViewSet(viewsets.ReadOnlyModelViewSet):"""diff --git a/wger/core/management/commands/setup_google_oauth.py b/wger/core/management/commands/setup_google_oauth.pyindex bcafaa9a0..d26777182 100644--- a/wger/core/management/commands/setup_google_oauth.py+++ b/wger/core/management/commands/setup_google_oauth.py@@ -1,8 +1,34 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2026 wger Team+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# Standard Libraryimport os-from django.core.management.base import BaseCommand++# Django+from django.conf import settingsfrom django.contrib.sites.models import Site+from django.core.management.base import (+ BaseCommand,+ CommandError,+)++# Third Partyfrom allauth.socialaccount.models import SocialApp+class Command(BaseCommand):help = 'Automates the creation of Google OAuth SocialApp credentials'@@ -11,33 +37,38 @@ class Command(BaseCommand):parser.add_argument('--client_secret', type=str, help='Google Client Secret')def handle(self, *args, **options):- # 1. Get or Create the Site (usually ID 1 for localhost)+ if not settings.WGER_SETTINGS.get('USE_SOCIAL_AUTH'):+ raise CommandError(+ 'Google OAuth is not enabled. '+ 'Set WGER_SETTINGS["USE_SOCIAL_AUTH"] = True '+ 'in your settings file before running this command.'+ )+site, created = Site.objects.get_or_create(- id=1,- defaults={'domain': 'localhost:8000', 'name': 'localhost'}+ id=1, defaults={'domain': 'localhost:8000', 'name': 'localhost'})- # 2. Get credentials from arguments or environment variables+ # Get credentials from arguments or environment variablesclient_id = options['client_id'] or os.environ.get('GOOGLE_CLIENT_ID')client_secret = options['client_secret'] or os.environ.get('GOOGLE_CLIENT_SECRET')if not client_id or not client_secret:- self.stderr.write("Error: Missing Client ID or Secret. Provide via args or Env Vars.")+ self.stderr.write('Error: Missing Client ID or Secret. Provide via args or Env Vars.')return- # 3. Create or Update the Social Appapp, created = SocialApp.objects.update_or_create(provider='google',defaults={'name': 'Google Auth','client_id': client_id,'secret': client_secret,- }+ },)# 4. Link the Site to the Social Appapp.sites.add(site)- status = "Created" if created else "Updated"- self.stdout.write(self.style.SUCCESS(f'Successfully {status} Google OAuth App for {site.domain}'))-\ No newline at end of file+ status = 'Created' if created else 'Updated'+ self.stdout.write(+ self.style.SUCCESS(f'Successfully {status} Google OAuth App for {site.domain}')+ )diff --git a/wger/core/templates/user/social_login.html b/wger/core/templates/user/social_login.htmlnew file mode 100644index 000000000..12b5d563a--- /dev/null+++ b/wger/core/templates/user/social_login.html@@ -0,0 +1,11 @@+{% load i18n socialaccount %}++<div class="row">+ <div class="col-md-12 text-center">+ <hr>+ <h4>{% translate "Login with Google" %}</h4>+ <a href="{% provider_login_url 'google' %}" class="btn btn-primary">+ <i class="fab fa-google"></i> {% translate "Login" %}+ </a>+ </div>+</div>\ No newline at end of filediff --git a/wger/urls.py b/wger/urls.pyindex e0b5f01a6..779817854 100644--- a/wger/urls.py+++ b/wger/urls.py@@ -1,19 +1,18 @@-# -*- coding: utf-8 -*---# This file is part of wger Workout Manager.+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2026 wger Team#-# wger Workout Manager is free software: you can redistribute it and/or modify-# it under the terms of the GNU Affero General Public License as published by-# the Free Software Foundation, either version 3 of the License, or-# (at your option) any later version.+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.#-# wger Workout Manager is distributed in the hope that it will be useful,-# but WITHOUT ANY WARRANTY; without even the implied warranty of-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-# GNU General Public License for more details.+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.#-# You should have received a copy of the GNU Affero General Public License-# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.# Djangofrom django.conf import settings@@ -373,12 +372,17 @@ urlpatterns += [SpectacularRedocView.as_view(url_name='schema'),name='api-redoc',),- path('accounts/', include('allauth.urls')),- path('auth/', include('dj_rest_auth.urls')),- path('auth/registration/', include('dj_rest_auth.registration.urls')),- path('auth/google/', core_api_views.GoogleLogin.as_view(), name='google_rest_login'),+ path('accounts/', include('allauth.account.urls')),]+if settings.WGER_SETTINGS.get('USE_SOCIAL_AUTH', False):+ urlpatterns += [+ path('accounts/', include('allauth.socialaccount.urls')),+ path('auth/registration/', include('dj_rest_auth.registration.urls')),+ path('auth/google/', core_api_views.GoogleLogin.as_view(), name='google_rest_login'),+ ]++## URL for user uploaded files, served like this during development only#