authentik.providers.oauth2.migrations.0022_remove_accesstoken_session_id_and_more

  1# Generated by Django 5.0.9 on 2024-10-23 13:38
  2
  3from hashlib import sha256
  4import django.db.models.deletion
  5from django.db import migrations, models
  6from django.apps.registry import Apps
  7from django.db.backends.base.schema import BaseDatabaseSchemaEditor
  8from authentik.lib.migrations import progress_bar
  9
 10
 11def migrate_session(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
 12    AuthenticatedSession = apps.get_model("authentik_core", "authenticatedsession")
 13    AuthorizationCode = apps.get_model("authentik_providers_oauth2", "authorizationcode")
 14    AccessToken = apps.get_model("authentik_providers_oauth2", "accesstoken")
 15    RefreshToken = apps.get_model("authentik_providers_oauth2", "refreshtoken")
 16    db_alias = schema_editor.connection.alias
 17
 18    print(f"\nFetching session keys, this might take a couple of minutes...")
 19    session_ids = {}
 20    for session in progress_bar(AuthenticatedSession.objects.using(db_alias).all()):
 21        session_ids[sha256(session.session_key.encode("ascii")).hexdigest()] = session.session_key
 22    for model in [AuthorizationCode, AccessToken, RefreshToken]:
 23        print(
 24            f"\nAdding session to {model._meta.verbose_name}, this might take a couple of minutes..."
 25        )
 26        for code in progress_bar(model.objects.using(db_alias).all()):
 27            if code.session_id_old not in session_ids:
 28                continue
 29            code.session = (
 30                AuthenticatedSession.objects.using(db_alias)
 31                .filter(session_key=session_ids[code.session_id_old])
 32                .first()
 33            )
 34            code.save()
 35
 36
 37class Migration(migrations.Migration):
 38
 39    dependencies = [
 40        ("authentik_core", "0039_source_group_matching_mode_alter_group_name_and_more"),
 41        ("authentik_providers_oauth2", "0021_oauth2provider_encryption_key_and_more"),
 42    ]
 43
 44    operations = [
 45        migrations.RenameField(
 46            model_name="accesstoken",
 47            old_name="session_id",
 48            new_name="session_id_old",
 49        ),
 50        migrations.RenameField(
 51            model_name="authorizationcode",
 52            old_name="session_id",
 53            new_name="session_id_old",
 54        ),
 55        migrations.RenameField(
 56            model_name="refreshtoken",
 57            old_name="session_id",
 58            new_name="session_id_old",
 59        ),
 60        migrations.AddField(
 61            model_name="accesstoken",
 62            name="session",
 63            field=models.ForeignKey(
 64                default=None,
 65                null=True,
 66                on_delete=django.db.models.deletion.SET_DEFAULT,
 67                to="authentik_core.authenticatedsession",
 68            ),
 69        ),
 70        migrations.AddField(
 71            model_name="authorizationcode",
 72            name="session",
 73            field=models.ForeignKey(
 74                default=None,
 75                null=True,
 76                on_delete=django.db.models.deletion.SET_DEFAULT,
 77                to="authentik_core.authenticatedsession",
 78            ),
 79        ),
 80        migrations.AddField(
 81            model_name="devicetoken",
 82            name="session",
 83            field=models.ForeignKey(
 84                default=None,
 85                null=True,
 86                on_delete=django.db.models.deletion.SET_DEFAULT,
 87                to="authentik_core.authenticatedsession",
 88            ),
 89        ),
 90        migrations.AddField(
 91            model_name="refreshtoken",
 92            name="session",
 93            field=models.ForeignKey(
 94                default=None,
 95                null=True,
 96                on_delete=django.db.models.deletion.SET_DEFAULT,
 97                to="authentik_core.authenticatedsession",
 98            ),
 99        ),
100        migrations.RunPython(migrate_session),
101        migrations.RemoveField(
102            model_name="accesstoken",
103            name="session_id_old",
104        ),
105        migrations.RemoveField(
106            model_name="authorizationcode",
107            name="session_id_old",
108        ),
109        migrations.RemoveField(
110            model_name="refreshtoken",
111            name="session_id_old",
112        ),
113    ]
def migrate_session( apps: django.apps.registry.Apps, schema_editor: django.db.backends.base.schema.BaseDatabaseSchemaEditor):
12def migrate_session(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
13    AuthenticatedSession = apps.get_model("authentik_core", "authenticatedsession")
14    AuthorizationCode = apps.get_model("authentik_providers_oauth2", "authorizationcode")
15    AccessToken = apps.get_model("authentik_providers_oauth2", "accesstoken")
16    RefreshToken = apps.get_model("authentik_providers_oauth2", "refreshtoken")
17    db_alias = schema_editor.connection.alias
18
19    print(f"\nFetching session keys, this might take a couple of minutes...")
20    session_ids = {}
21    for session in progress_bar(AuthenticatedSession.objects.using(db_alias).all()):
22        session_ids[sha256(session.session_key.encode("ascii")).hexdigest()] = session.session_key
23    for model in [AuthorizationCode, AccessToken, RefreshToken]:
24        print(
25            f"\nAdding session to {model._meta.verbose_name}, this might take a couple of minutes..."
26        )
27        for code in progress_bar(model.objects.using(db_alias).all()):
28            if code.session_id_old not in session_ids:
29                continue
30            code.session = (
31                AuthenticatedSession.objects.using(db_alias)
32                .filter(session_key=session_ids[code.session_id_old])
33                .first()
34            )
35            code.save()
class Migration(django.db.migrations.migration.Migration):
 38class Migration(migrations.Migration):
 39
 40    dependencies = [
 41        ("authentik_core", "0039_source_group_matching_mode_alter_group_name_and_more"),
 42        ("authentik_providers_oauth2", "0021_oauth2provider_encryption_key_and_more"),
 43    ]
 44
 45    operations = [
 46        migrations.RenameField(
 47            model_name="accesstoken",
 48            old_name="session_id",
 49            new_name="session_id_old",
 50        ),
 51        migrations.RenameField(
 52            model_name="authorizationcode",
 53            old_name="session_id",
 54            new_name="session_id_old",
 55        ),
 56        migrations.RenameField(
 57            model_name="refreshtoken",
 58            old_name="session_id",
 59            new_name="session_id_old",
 60        ),
 61        migrations.AddField(
 62            model_name="accesstoken",
 63            name="session",
 64            field=models.ForeignKey(
 65                default=None,
 66                null=True,
 67                on_delete=django.db.models.deletion.SET_DEFAULT,
 68                to="authentik_core.authenticatedsession",
 69            ),
 70        ),
 71        migrations.AddField(
 72            model_name="authorizationcode",
 73            name="session",
 74            field=models.ForeignKey(
 75                default=None,
 76                null=True,
 77                on_delete=django.db.models.deletion.SET_DEFAULT,
 78                to="authentik_core.authenticatedsession",
 79            ),
 80        ),
 81        migrations.AddField(
 82            model_name="devicetoken",
 83            name="session",
 84            field=models.ForeignKey(
 85                default=None,
 86                null=True,
 87                on_delete=django.db.models.deletion.SET_DEFAULT,
 88                to="authentik_core.authenticatedsession",
 89            ),
 90        ),
 91        migrations.AddField(
 92            model_name="refreshtoken",
 93            name="session",
 94            field=models.ForeignKey(
 95                default=None,
 96                null=True,
 97                on_delete=django.db.models.deletion.SET_DEFAULT,
 98                to="authentik_core.authenticatedsession",
 99            ),
100        ),
101        migrations.RunPython(migrate_session),
102        migrations.RemoveField(
103            model_name="accesstoken",
104            name="session_id_old",
105        ),
106        migrations.RemoveField(
107            model_name="authorizationcode",
108            name="session_id_old",
109        ),
110        migrations.RemoveField(
111            model_name="refreshtoken",
112            name="session_id_old",
113        ),
114    ]

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', '0039_source_group_matching_mode_alter_group_name_and_more'), ('authentik_providers_oauth2', '0021_oauth2provider_encryption_key_and_more')]
operations = [<RenameField model_name='accesstoken', old_name='session_id', new_name='session_id_old'>, <RenameField model_name='authorizationcode', old_name='session_id', new_name='session_id_old'>, <RenameField model_name='refreshtoken', old_name='session_id', new_name='session_id_old'>, <AddField model_name='accesstoken', name='session', field=<django.db.models.fields.related.ForeignKey>>, <AddField model_name='authorizationcode', name='session', field=<django.db.models.fields.related.ForeignKey>>, <AddField model_name='devicetoken', name='session', field=<django.db.models.fields.related.ForeignKey>>, <AddField model_name='refreshtoken', name='session', field=<django.db.models.fields.related.ForeignKey>>, <RunPython <function migrate_session>>, <RemoveField model_name='accesstoken', name='session_id_old'>, <RemoveField model_name='authorizationcode', name='session_id_old'>, <RemoveField model_name='refreshtoken', name='session_id_old'>]