-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Save last_4 chars of current app tokens and store their hashes * Update app mutations, commands and tests * Update changelog Co-authored-by: Marcin Gębala <5421321+maarcingebala@users.noreply.github.com>
- Loading branch information
1 parent
c622023
commit 81d02e7
Showing
22 changed files
with
237 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,23 @@ | ||
# Generated by Django 3.2.12 on 2022-03-29 07:49 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("app", "0008_appextension_target"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="apptoken", | ||
name="token_last_4", | ||
field=models.CharField(max_length=4, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="apptoken", | ||
name="auth_token", | ||
field=models.CharField(max_length=128, unique=True), | ||
), | ||
] |
This file contains 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,48 @@ | ||
# Generated by Django 3.2.12 on 2022-03-29 10:04 | ||
|
||
from django.contrib.auth.hashers import make_password | ||
from django.db import migrations | ||
|
||
BATCH_SIZE = 10000 | ||
|
||
|
||
def update_app_tokens(apps, schema_editor): | ||
AppToken = apps.get_model("app", "AppToken") | ||
queryset = AppToken.objects.all() | ||
for batch_pks in queryset_in_batches(queryset): | ||
app_tokens = AppToken.objects.filter(pk__in=batch_pks) | ||
for app_token in app_tokens: | ||
app_token.token_last_4 = app_token.auth_token[-4:] | ||
app_token.auth_token = make_password(app_token.auth_token) | ||
|
||
AppToken.objects.bulk_update(app_tokens, ["auth_token", "token_last_4"]) | ||
|
||
|
||
def queryset_in_batches(queryset): | ||
"""Slice a queryset into batches. | ||
Input queryset should be sorted be pk. | ||
""" | ||
start_pk = 0 | ||
|
||
while True: | ||
qs = queryset.filter(pk__gt=start_pk)[:BATCH_SIZE] | ||
pks = list(qs.values_list("pk", flat=True)) | ||
|
||
if not pks: | ||
break | ||
|
||
yield pks | ||
|
||
start_pk = pks[-1] | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("app", "0009_apptoken_token_last_4"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_app_tokens, migrations.RunPython.noop), | ||
] |
This file contains 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,18 @@ | ||
# Generated by Django 3.2.12 on 2022-03-29 10:09 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("app", "0010_update_app_tokens"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="apptoken", | ||
name="token_last_4", | ||
field=models.CharField(max_length=4), | ||
), | ||
] |
This file contains 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 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 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 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 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
Oops, something went wrong.