From 08c56f5408029293999069cb797272dd7974f4d9 Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Fri, 6 Sep 2024 14:53:06 -0400 Subject: [PATCH 1/5] add a forward migration function that computes checksum for existing tokens --- .../migrations/0012_add_token_checksum.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/oauth2_provider/migrations/0012_add_token_checksum.py b/oauth2_provider/migrations/0012_add_token_checksum.py index 7f62955e3..ca05473eb 100644 --- a/oauth2_provider/migrations/0012_add_token_checksum.py +++ b/oauth2_provider/migrations/0012_add_token_checksum.py @@ -4,6 +4,16 @@ from django.db import migrations, models from oauth2_provider.settings import oauth2_settings +def forwards_func(apps, schema_editor): + """ + Forward migration touches every "old" accesstoken.token which will cause the checksum to be computed. + """ + AccessToken = apps.get_model(oauth2_settings.ACCESS_TOKEN_MODEL) + accesstokens = AccessToken._default_manager.all() + for accesstoken in accesstokens: + accesstoken.save(update_fields=['token_checksum']) + + class Migration(migrations.Migration): dependencies = [ ("oauth2_provider", "0011_refreshtoken_token_family"), @@ -15,7 +25,7 @@ class Migration(migrations.Migration): model_name="accesstoken", name="token_checksum", field=oauth2_provider.models.TokenChecksumField( - blank=True, db_index=True, max_length=64, unique=True + blank=True, null=True, db_index=True, max_length=64, unique=True ), ), migrations.AlterField( @@ -23,4 +33,5 @@ class Migration(migrations.Migration): name="token", field=models.TextField(), ), + migrations.RunPython(forwards_func), ] From 5e056b53fce884c25e3dc11374be777765c9b4e9 Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Fri, 6 Sep 2024 17:04:03 -0400 Subject: [PATCH 2/5] split up the AddField and AlterField with unique index to happen after the forward migration of existing tokens --- oauth2_provider/migrations/0012_add_token_checksum.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/oauth2_provider/migrations/0012_add_token_checksum.py b/oauth2_provider/migrations/0012_add_token_checksum.py index ca05473eb..476c3b402 100644 --- a/oauth2_provider/migrations/0012_add_token_checksum.py +++ b/oauth2_provider/migrations/0012_add_token_checksum.py @@ -24,14 +24,17 @@ class Migration(migrations.Migration): migrations.AddField( model_name="accesstoken", name="token_checksum", - field=oauth2_provider.models.TokenChecksumField( - blank=True, null=True, db_index=True, max_length=64, unique=True - ), + field=oauth2_provider.models.TokenChecksumField(blank=True, null=True, max_length=64), ), migrations.AlterField( model_name="accesstoken", name="token", field=models.TextField(), ), - migrations.RunPython(forwards_func), + migrations.RunPython(forwards_func, migrations.RunPython.noop), + migrations.AlterField( + model_name='accesstoken', + name='token_checksum', + field=oauth2_provider.models.TokenChecksumField(blank=False, max_length=64, db_index=True, unique=True), + ), ] From 846ad903a35ecbf2922cf67edb446015399377f5 Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Fri, 6 Sep 2024 17:07:19 -0400 Subject: [PATCH 3/5] Change AccessToken model to set blank=False. The customized migration will hopefully sort things out. --- oauth2_provider/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2_provider/models.py b/oauth2_provider/models.py index 831fc551f..a987b4435 100644 --- a/oauth2_provider/models.py +++ b/oauth2_provider/models.py @@ -392,7 +392,7 @@ class AbstractAccessToken(models.Model): token = models.TextField() token_checksum = TokenChecksumField( max_length=64, - blank=True, + blank=False, unique=True, db_index=True, ) From c1646fbc288b1bb35cf4ffee9c6ada3411475b7c Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Sat, 7 Sep 2024 07:07:56 -0400 Subject: [PATCH 4/5] changelog --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ba7e52f8..c4d702d77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security --> +## [unreleased] +### Added +### Changed +### Deprecated +### Removed +### Fixed +* #1491 Fix migration error when there are pre-existing Access Tokens. + +### Security + ## [3.0.0] - 2024-09-05 ### WARNING - POTENTIAL BREAKING CHANGES From ebb3f8fbc0ab4137114aa26780ee60ab2275fba4 Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Sat, 7 Sep 2024 07:26:19 -0400 Subject: [PATCH 5/5] release 3.0.1 --- CHANGELOG.md | 8 +------- oauth2_provider/__init__.py | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4d702d77..483336b04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,16 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security --> -## [unreleased] -### Added -### Changed -### Deprecated -### Removed +## [3.0.1] - 2024-09-07 ### Fixed * #1491 Fix migration error when there are pre-existing Access Tokens. -### Security - ## [3.0.0] - 2024-09-05 ### WARNING - POTENTIAL BREAKING CHANGES diff --git a/oauth2_provider/__init__.py b/oauth2_provider/__init__.py index 528787cfc..055276878 100644 --- a/oauth2_provider/__init__.py +++ b/oauth2_provider/__init__.py @@ -1 +1 @@ -__version__ = "3.0.0" +__version__ = "3.0.1"