-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add migration management command from pennersr/django-allauth#3420 * Add minimal smoke test for management command * Bump version * Update changelog * Update readme --------- Co-authored-by: Raymond Penners <raymond.penners@intenct.nl>
- Loading branch information
Showing
8 changed files
with
107 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from __future__ import annotations | ||
|
||
__version__ = "0.11.0" | ||
__version__ = "0.12.0" |
Empty file.
Empty file.
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,40 @@ | ||
from __future__ import annotations | ||
|
||
import base64 | ||
|
||
from allauth.mfa.adapter import get_adapter | ||
from allauth.mfa.models import Authenticator | ||
from django.core.management.base import BaseCommand | ||
from django_otp.plugins.otp_static.models import StaticDevice | ||
from django_otp.plugins.otp_totp.models import TOTPDevice | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, **options): | ||
adapter = get_adapter() | ||
authenticators = [] | ||
for totp in TOTPDevice.objects.filter(confirmed=True).iterator(): | ||
recovery_codes = set() | ||
for sdevice in StaticDevice.objects.filter( | ||
confirmed=True, | ||
user_id=totp.user_id, | ||
).iterator(): | ||
recovery_codes.update(sdevice.token_set.values_list("token", flat=True)) | ||
secret = base64.b32encode(bytes.fromhex(totp.key)).decode("ascii") | ||
totp_authenticator = Authenticator( | ||
user_id=totp.user_id, | ||
type=Authenticator.Type.TOTP, | ||
data={"secret": adapter.encrypt(secret)}, | ||
) | ||
authenticators.append(totp_authenticator) | ||
authenticators.append( | ||
Authenticator( | ||
user_id=totp.user_id, | ||
type=Authenticator.Type.RECOVERY_CODES, | ||
data={ | ||
"migrated_codes": [adapter.encrypt(c) for c in recovery_codes], | ||
}, | ||
), | ||
) | ||
Authenticator.objects.bulk_create(authenticators) | ||
self.stdout.write(f"Created {len(authenticators)} Authenticators") |
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