-
Notifications
You must be signed in to change notification settings - Fork 0
feat(login): add login with google from @europython.eu domain #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
artcz
wants to merge
4
commits into
main
Choose a base branch
from
login-with-google
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Google OAuth Login | ||
|
|
||
| Users log in with their `@europython.eu` Google Workspace accounts via [django-allauth](https://docs.allauth.org/). | ||
|
|
||
| Only `@europython.eu` emails are allowed — other domains see a friendly error page. On first login a Django user is created with `is_staff=False`, so they can log in but can't access protected pages until an admin promotes them in Django admin. | ||
|
|
||
| Protected views use a `@staff_required` decorator (`core/auth.py`) that checks both authentication and staff status. | ||
|
|
||
| ## Setup | ||
|
|
||
| ### Production | ||
|
|
||
| 1. In [Google Cloud Console](https://console.cloud.google.com/), create an OAuth client ID (Web application) with redirect URI: | ||
| ``` | ||
| https://internal.europython.eu/accounts/google/login/callback/ | ||
| ``` | ||
| 2. Set env vars on the server: | ||
| ``` | ||
| GOOGLE_OAUTH_CLIENT_ID=<your-client-id> | ||
| GOOGLE_OAUTH_CLIENT_SECRET=<your-client-secret> | ||
| ``` | ||
| 3. Deploy and run migrations (`make deploy/app` handles this) | ||
|
|
||
| ### Local development | ||
|
|
||
| Create a separate OAuth client with redirect URI `http://localhost:4672/accounts/google/login/callback/` and add credentials to `intbot/.env`. Then run `make migrate`. | ||
|
|
||
| You can also skip this entirely — for daily usage create users via Django admin (`/admin/`), and in tests use `force_login()`: | ||
|
|
||
| ```python | ||
| def test_products_page(self, client): | ||
| user = User.objects.create_user(username="test", is_staff=True) | ||
| client.force_login(user) | ||
|
|
||
| response = client.get("/products/") | ||
|
|
||
| assert response.status_code == 200 | ||
| ``` | ||
|
|
||
| Use `is_staff=True` to access `@staff_required` views, or omit it to test the non-staff redirect to `/no-access/`. | ||
|
|
||
| ## Granting access to users | ||
|
|
||
| 1. Go to Django admin (`/admin/` > **Users**) | ||
| 2. Find the user and set **Staff status** to checked | ||
| 3. Save — the user can now access protected pages | ||
|
|
||
| ## Files | ||
|
|
||
| - `core/auth.py` — `EuroPythonSocialAccountAdapter` (domain restriction) and `staff_required` decorator | ||
| - `templates/account/login.html` — login page with Google button | ||
| - `templates/no_access.html` — shown to logged-in non-staff users | ||
| - `templates/socialaccount/authentication_error.html` — shown for non-europython.eu emails | ||
|
|
||
| ## Settings | ||
|
|
||
| Allauth config in `intbot/settings.py`: | ||
|
|
||
| - `SOCIALACCOUNT_ONLY = True` — only social login, no username/password | ||
| - `ACCOUNT_EMAIL_VERIFICATION = "none"` — Google already verifies emails | ||
| - `SOCIALACCOUNT_ADAPTER` — points to our adapter that restricts to `@europython.eu` | ||
| - Google credentials configured via env vars, no `SocialApp` database entries needed | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from functools import wraps | ||
|
|
||
| from allauth.socialaccount.adapter import DefaultSocialAccountAdapter # type: ignore[import-untyped] | ||
| from django.contrib.auth.decorators import login_required | ||
| from django.http import HttpRequest | ||
| from django.shortcuts import redirect | ||
|
|
||
|
|
||
| class EuroPythonSocialAccountAdapter(DefaultSocialAccountAdapter): | ||
| def is_open_for_signup(self, request: HttpRequest, sociallogin: object) -> bool: | ||
| email = sociallogin.user.email # type: ignore[attr-defined] | ||
| return email.endswith("@europython.eu") | ||
|
|
||
|
|
||
| def staff_required(view_func): # type: ignore[no-untyped-def] | ||
| @wraps(view_func) | ||
| def wrapper(request: HttpRequest, *args, **kwargs): # type: ignore[no-untyped-def] | ||
| if not request.user.is_staff: | ||
| return redirect("/no-access/") | ||
| return view_func(request, *args, **kwargs) | ||
|
|
||
| return login_required(wrapper, login_url="/accounts/login/") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| {% load socialaccount %} | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Sign In - EuroPython Internal</title> | ||
| <style> | ||
| body { | ||
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| min-height: 100vh; | ||
| margin: 0; | ||
| background: #f5f5f5; | ||
| } | ||
| .login-box { | ||
| background: white; | ||
| padding: 2rem; | ||
| border-radius: 8px; | ||
| box-shadow: 0 2px 8px rgba(0,0,0,0.1); | ||
| text-align: center; | ||
| max-width: 400px; | ||
| } | ||
| h1 { margin-bottom: 0.5rem; } | ||
| p { color: #666; margin-bottom: 1.5rem; } | ||
| .btn-google { | ||
| display: inline-block; | ||
| padding: 0.75rem 1.5rem; | ||
| background: #4285f4; | ||
| color: white; | ||
| text-decoration: none; | ||
| border-radius: 4px; | ||
| font-size: 1rem; | ||
| } | ||
| .btn-google:hover { background: #357ae8; } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="login-box"> | ||
| <h1>EuroPython Internal</h1> | ||
| <p>Sign in with your @europython.eu account</p> | ||
| <a class="btn-google" href="{% provider_login_url 'google' %}">Sign in with Google</a> | ||
| </div> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>No Access - EuroPython Internal</title> | ||
| <style> | ||
| body { | ||
| font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| min-height: 100vh; | ||
| margin: 0; | ||
| background: #f5f5f5; | ||
| } | ||
| .box { | ||
| background: white; | ||
| padding: 2rem; | ||
| border-radius: 8px; | ||
| box-shadow: 0 2px 8px rgba(0,0,0,0.1); | ||
| text-align: center; | ||
| max-width: 500px; | ||
| } | ||
| h1 { color: #d32f2f; } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="box"> | ||
| <h1>Access Denied</h1> | ||
| <p>You are logged in but do not have access to this resource yet.</p> | ||
| <p>Please contact an admin to get your account promoted.</p> | ||
| </div> | ||
| </body> | ||
| </html> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.