-
Notifications
You must be signed in to change notification settings - Fork 6
Add OIDC offline_token user sync #73
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
schliz
wants to merge
13
commits into
HaDiNet:master
Choose a base branch
from
schliz:feat/ical-subscription
base: master
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
13 commits
Select commit
Hold shift + click to select a range
6b844ef
refactor: extract OIDC user population into shared utility
schliz 271e3c3
feat: add CalendarToken and OIDCOfflineToken models
schliz 43016b1
feat: add token-authenticated iCal feed endpoints
schliz 12ca93e
feat: add calendar subscription UI to user profile
schliz 808dd05
feat: move calendar card to main column and add webcal buttons
schliz 20ac9e6
style: place calendar button and URL field in a single row
schliz 2aadf10
style: match calendar card format to user info card
schliz 86d74b6
feat: add German translations and rename My Participations to My Shifts
schliz 6a98b99
feat: register CalendarToken and OIDCOfflineToken in admin
schliz 8c120d1
refactor: remove CalendarToken and calendar feed integration
schliz 01cf213
refactor: move OIDCOfflineToken to standalone module, drop CalendarToken
schliz f59576a
feat: add sync_oidc_users management command for periodic group sync
schliz 11c13e0
chore: remove stale translation strings and clean up .po files
schliz 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| from django.contrib import admin | ||
|
|
||
| from shiftings.accounts.models import User | ||
| from shiftings.accounts.models import OIDCOfflineToken, User | ||
|
|
||
| admin.site.register(User) | ||
|
|
||
|
|
||
| @admin.register(OIDCOfflineToken) | ||
| class OIDCOfflineTokenAdmin(admin.ModelAdmin): | ||
| list_display = ('user', 'updated') | ||
| search_fields = ('user__username',) | ||
| readonly_fields = ('updated',) |
44 changes: 44 additions & 0 deletions
44
src/shiftings/accounts/management/commands/sync_oidc_users.py
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,44 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from shiftings.accounts.models import OIDCOfflineToken | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = 'Sync all users with stored OIDC offline tokens (groups, admin status).' | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| '--purge-expired', | ||
| action='store_true', | ||
| help='Delete offline tokens that fail to refresh (expired/revoked).', | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| tokens = OIDCOfflineToken.objects.select_related('user').all() | ||
| total = tokens.count() | ||
| success = 0 | ||
| failed = 0 | ||
|
|
||
| self.stdout.write(f'Syncing {total} OIDC user(s)...') | ||
|
|
||
| for offline_token in tokens: | ||
| username = offline_token.user.username | ||
| if offline_token.refresh_user_info(): | ||
| success += 1 | ||
| logger.info('Synced user %s', username) | ||
| else: | ||
| failed += 1 | ||
| logger.warning('Failed to sync user %s', username) | ||
| if options['purge_expired']: | ||
| offline_token.delete() | ||
| logger.info('Purged expired token for user %s', username) | ||
|
|
||
| self.stdout.write(self.style.SUCCESS( | ||
| f'Done. {success} synced, {failed} failed (of {total} total).' | ||
| )) | ||
27 changes: 27 additions & 0 deletions
27
src/shiftings/accounts/migrations/0002_oidcofflinetoken.py
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,27 @@ | ||
| # Generated by Django 6.0.2 on 2026-03-15 16:45 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('accounts', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='OIDCOfflineToken', | ||
| fields=[ | ||
| ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('refresh_token', models.TextField(verbose_name='Refresh Token')), | ||
| ('updated', models.DateTimeField(auto_now=True, verbose_name='Updated')), | ||
| ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='oidc_offline_token', to=settings.AUTH_USER_MODEL, verbose_name='User')), | ||
| ], | ||
| options={ | ||
| 'default_permissions': (), | ||
| }, | ||
| ), | ||
| ] |
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| from .oidc_token import OIDCOfflineToken | ||
| from .user import BaseUser, User |
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,103 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from functools import cache | ||
| from typing import Any | ||
|
|
||
| import requests | ||
| from django.conf import settings | ||
| from django.db import models | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @cache | ||
| def _get_oidc_endpoints() -> dict[str, str]: | ||
| """Fetch and cache the OpenID Connect discovery document.""" | ||
| resp = requests.get(settings.OPENID_CONF_URL, timeout=10) | ||
| resp.raise_for_status() | ||
| return resp.json() | ||
|
|
||
|
|
||
| class OIDCOfflineToken(models.Model): | ||
| user = models.OneToOneField( | ||
| settings.AUTH_USER_MODEL, | ||
| on_delete=models.CASCADE, | ||
| related_name='oidc_offline_token', | ||
| verbose_name=_('User'), | ||
| ) | ||
| refresh_token = models.TextField(verbose_name=_('Refresh Token')) | ||
| updated = models.DateTimeField(auto_now=True, verbose_name=_('Updated')) | ||
|
|
||
| class Meta: | ||
| default_permissions = () | ||
|
|
||
| def __str__(self) -> str: | ||
| return f'OIDCOfflineToken for {self.user}' | ||
|
|
||
| def refresh_user_info(self) -> bool: | ||
| """Use the stored offline token to refresh the user's OIDC data. | ||
|
|
||
| Returns True on success, False if the token is expired or invalid. | ||
| """ | ||
| try: | ||
| endpoints = _get_oidc_endpoints() | ||
| token_endpoint = endpoints['token_endpoint'] | ||
| userinfo_endpoint = endpoints['userinfo_endpoint'] | ||
| except Exception: | ||
| logger.exception('Failed to fetch OIDC discovery document') | ||
| return False | ||
|
|
||
| client_config = settings.AUTHLIB_OAUTH_CLIENTS['shiftings'] | ||
|
|
||
| try: | ||
| token_resp = requests.post( | ||
| token_endpoint, | ||
| data={ | ||
| 'grant_type': 'refresh_token', | ||
| 'refresh_token': self.refresh_token, | ||
| 'client_id': client_config['client_id'], | ||
| 'client_secret': client_config['client_secret'], | ||
| }, | ||
| timeout=10, | ||
| ) | ||
| except Exception: | ||
| logger.exception('Failed to call OIDC token endpoint') | ||
| return False | ||
|
|
||
| if token_resp.status_code != 200: | ||
| logger.warning( | ||
| 'OIDC token refresh failed (status %d): %s', | ||
| token_resp.status_code, | ||
| token_resp.text, | ||
| ) | ||
| return False | ||
|
|
||
| token_data = token_resp.json() | ||
|
|
||
| self.refresh_token = token_data['refresh_token'] | ||
| self.save(update_fields=['refresh_token', 'updated']) | ||
|
|
||
| access_token = token_data['access_token'] | ||
| try: | ||
| userinfo_resp = requests.get( | ||
| userinfo_endpoint, | ||
| headers={'Authorization': f'Bearer {access_token}'}, | ||
| timeout=10, | ||
| ) | ||
| except Exception: | ||
| logger.exception('Failed to call OIDC userinfo endpoint') | ||
| return False | ||
|
|
||
| if userinfo_resp.status_code != 200: | ||
| logger.warning( | ||
| 'OIDC userinfo request failed (status %d): %s', | ||
| userinfo_resp.status_code, | ||
| userinfo_resp.text, | ||
| ) | ||
| return False | ||
|
|
||
| from shiftings.accounts.oidc import populate_user_from_oidc | ||
| populate_user_from_oidc(userinfo_resp.json()) | ||
| return True |
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,38 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from typing import Any | ||
|
|
||
| from django.conf import settings | ||
| from django.contrib.auth.models import AbstractUser, Group | ||
| from django.contrib.auth.views import UserModel | ||
|
|
||
| from shiftings.accounts.models import User | ||
|
|
||
|
|
||
| def populate_user_from_oidc(user_data: dict[str, Any]) -> AbstractUser: | ||
| """Create or update a Django user from OIDC userinfo claims. | ||
|
|
||
| Syncs username, email, groups, and admin status from the OIDC provider. | ||
| """ | ||
| username = user_data.get(settings.OAUTH_USERNAME_CLAIM, '') | ||
| groups: list[str] = user_data.get(settings.OAUTH_GROUP_CLAIM, []) | ||
| for group in groups: | ||
| if re.match(settings.OAUTH_GROUP_IGNORE_REGEX, group) is None: | ||
| Group.objects.get_or_create(name=group) | ||
| try: | ||
| user: AbstractUser = User.objects.get_by_natural_key(username) | ||
| except UserModel.DoesNotExist: | ||
| user = User.objects.create(username=username) | ||
| user.set_unusable_password() | ||
| # ignore secondary names | ||
| user.first_name = user_data.get(settings.OAUTH_FIRST_NAME_CLAIM, '').split(' ')[0] | ||
| user.last_name = user_data.get(settings.OAUTH_LAST_NAME_CLAIM, '') | ||
| user.email = user_data.get(settings.OAUTH_EMAIL_CLAIM, '') | ||
| user.groups.set(Group.objects.filter(name__in=groups)) | ||
|
|
||
| user.is_superuser = settings.OAUTH_ADMIN_GROUP in groups | ||
| user.is_staff = settings.OAUTH_ADMIN_GROUP in groups | ||
| user.backend = 'django.contrib.auth.backends.ModelBackend' | ||
| user.save() | ||
| return user |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also print out the time how long a refresh run took, so an admin can get a feeling of the performance impact? I also saw some async stuff was added to Django recently. I don't know if it is easy to add but it maybe an option to improve overall reactiveness, if this task is IO heavy.
When does such a token expire? Do I understand it correctly that we have to refresh the token regularly and expired tokens can only be "refreshed" if the user does relogin?
If so, this only solves #41 for future deployments.
On a running system no tokens are there (yet?) so this implies for me some kind of purge when this change is rolled out:
Maybe some scripting/documentation to support this would be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The offline token expiry is handled by the OIDC IdP. This implementation currently does nothing on expiry. Ways to solve this is adding a grace period until the 30. June and begin clearing group associations and requiring re-authentication from then. The HaDiKo deployment of Shiftings should use quite short token periods which would force everybody using the App to have logged in at least once since then.