authentik.providers.oauth2.migrations.0038_oauth2sessionlogin
1# Generated by Django 5.2.17 on 2026-09-21 06:13 2 3from json import JSONDecodeError, loads 4 5import django.db.models.deletion 6from django.db import migrations, models 7 8import authentik.lib.models 9 10 11def backfill_session_logins(apps, schema_editor): 12 """Create logins for sessions which already exist, based on the tokens issued for them""" 13 OAuth2SessionLogin = apps.get_model("authentik_providers_oauth2", "OAuth2SessionLogin") 14 db_alias = schema_editor.connection.alias 15 16 logins = {} 17 for model in ("AccessToken", "RefreshToken"): 18 Model = apps.get_model("authentik_providers_oauth2", model) 19 # Historical models use the default manager, so this includes expired tokens 20 tokens = ( 21 Model.objects.using(db_alias) 22 .exclude(session=None) 23 .values_list("session_id", "provider_id", "_id_token") 24 ) 25 for session_id, provider_id, raw_id_token in tokens.iterator(): 26 if (session_id, provider_id) in logins: 27 continue 28 try: 29 id_token = loads(raw_id_token) 30 except JSONDecodeError: 31 continue 32 if not id_token.get("iss") or not id_token.get("sub"): 33 continue 34 logins[(session_id, provider_id)] = OAuth2SessionLogin( 35 session_id=session_id, 36 provider_id=provider_id, 37 iss=id_token["iss"], 38 sub=id_token["sub"], 39 ) 40 OAuth2SessionLogin.objects.using(db_alias).bulk_create( 41 logins.values(), batch_size=1000, ignore_conflicts=True 42 ) 43 44 45class Migration(migrations.Migration): 46 47 dependencies = [ 48 ("authentik_core", "0065_merge_20260916_1253"), 49 ("authentik_providers_oauth2", "0037_accesstoken_actor_authorizationcode_actor_and_more"), 50 ] 51 52 operations = [ 53 migrations.CreateModel( 54 name="OAuth2SessionLogin", 55 fields=[ 56 ( 57 "id", 58 models.AutoField( 59 auto_created=True, primary_key=True, serialize=False, verbose_name="ID" 60 ), 61 ), 62 ("iss", models.TextField()), 63 ("sub", models.TextField()), 64 ( 65 "provider", 66 models.ForeignKey( 67 on_delete=django.db.models.deletion.CASCADE, 68 to="authentik_providers_oauth2.oauth2provider", 69 ), 70 ), 71 ( 72 "session", 73 models.ForeignKey( 74 on_delete=django.db.models.deletion.CASCADE, 75 to="authentik_core.authenticatedsession", 76 ), 77 ), 78 ], 79 options={ 80 "verbose_name": "OAuth2 Session Login", 81 "verbose_name_plural": "OAuth2 Session Logins", 82 "constraints": [ 83 models.UniqueConstraint( 84 fields=("session", "provider"), name="oauth2_session_login_unique" 85 ) 86 ], 87 }, 88 bases=(authentik.lib.models.InternallyManagedMixin, models.Model), 89 ), 90 migrations.RunPython(backfill_session_logins, migrations.RunPython.noop), 91 ]
def
backfill_session_logins(apps, schema_editor):
12def backfill_session_logins(apps, schema_editor): 13 """Create logins for sessions which already exist, based on the tokens issued for them""" 14 OAuth2SessionLogin = apps.get_model("authentik_providers_oauth2", "OAuth2SessionLogin") 15 db_alias = schema_editor.connection.alias 16 17 logins = {} 18 for model in ("AccessToken", "RefreshToken"): 19 Model = apps.get_model("authentik_providers_oauth2", model) 20 # Historical models use the default manager, so this includes expired tokens 21 tokens = ( 22 Model.objects.using(db_alias) 23 .exclude(session=None) 24 .values_list("session_id", "provider_id", "_id_token") 25 ) 26 for session_id, provider_id, raw_id_token in tokens.iterator(): 27 if (session_id, provider_id) in logins: 28 continue 29 try: 30 id_token = loads(raw_id_token) 31 except JSONDecodeError: 32 continue 33 if not id_token.get("iss") or not id_token.get("sub"): 34 continue 35 logins[(session_id, provider_id)] = OAuth2SessionLogin( 36 session_id=session_id, 37 provider_id=provider_id, 38 iss=id_token["iss"], 39 sub=id_token["sub"], 40 ) 41 OAuth2SessionLogin.objects.using(db_alias).bulk_create( 42 logins.values(), batch_size=1000, ignore_conflicts=True 43 )
Create logins for sessions which already exist, based on the tokens issued for them
class
Migration(django.db.migrations.migration.Migration):
46class Migration(migrations.Migration): 47 48 dependencies = [ 49 ("authentik_core", "0065_merge_20260916_1253"), 50 ("authentik_providers_oauth2", "0037_accesstoken_actor_authorizationcode_actor_and_more"), 51 ] 52 53 operations = [ 54 migrations.CreateModel( 55 name="OAuth2SessionLogin", 56 fields=[ 57 ( 58 "id", 59 models.AutoField( 60 auto_created=True, primary_key=True, serialize=False, verbose_name="ID" 61 ), 62 ), 63 ("iss", models.TextField()), 64 ("sub", models.TextField()), 65 ( 66 "provider", 67 models.ForeignKey( 68 on_delete=django.db.models.deletion.CASCADE, 69 to="authentik_providers_oauth2.oauth2provider", 70 ), 71 ), 72 ( 73 "session", 74 models.ForeignKey( 75 on_delete=django.db.models.deletion.CASCADE, 76 to="authentik_core.authenticatedsession", 77 ), 78 ), 79 ], 80 options={ 81 "verbose_name": "OAuth2 Session Login", 82 "verbose_name_plural": "OAuth2 Session Logins", 83 "constraints": [ 84 models.UniqueConstraint( 85 fields=("session", "provider"), name="oauth2_session_login_unique" 86 ) 87 ], 88 }, 89 bases=(authentik.lib.models.InternallyManagedMixin, models.Model), 90 ), 91 migrations.RunPython(backfill_session_logins, migrations.RunPython.noop), 92 ]
The base class for all migrations.
Migration files will import this from django.db.migrations.Migration and subclass it as a class called Migration. It will have one or more of the following attributes:
- operations: A list of Operation instances, probably from django.db.migrations.operations
- dependencies: A list of tuples of (app_path, migration_name)
- run_before: A list of tuples of (app_path, migration_name)
- replaces: A list of migration_names
Note that all migrations come out of migrations and into the Loader or Graph as instances, having been initialized with their app label and name.
dependencies =
[('authentik_core', '0065_merge_20260916_1253'), ('authentik_providers_oauth2', '0037_accesstoken_actor_authorizationcode_actor_and_more')]
operations =
[<CreateModel name='OAuth2SessionLogin', fields=[('id', <django.db.models.fields.AutoField>), ('iss', <django.db.models.fields.TextField>), ('sub', <django.db.models.fields.TextField>), ('provider', <django.db.models.fields.related.ForeignKey>), ('session', <django.db.models.fields.related.ForeignKey>)], options={'verbose_name': 'OAuth2 Session Login', 'verbose_name_plural': 'OAuth2 Session Logins', 'constraints': [<UniqueConstraint: fields=('session', 'provider') name='oauth2_session_login_unique'>]}, bases=(<class 'authentik.lib.models.InternallyManagedMixin'>, <class 'django.db.models.base.Model'>)>, <RunPython <function backfill_session_logins>, <function RunPython.noop>>]