6
6
7
7
from django .core .management .base import BaseCommand
8
8
9
- from apps .authentication .models import OnlineUser
9
+ from apps .authentication .models import Email , OnlineUser
10
10
11
11
ws = re .compile (r"\s+" )
12
12
@@ -36,10 +36,12 @@ def extract_phone_number(u: OnlineUser) -> Optional[str]:
36
36
37
37
def create_auth0_user (u : OnlineUser ):
38
38
if not u .has_usable_password :
39
- print (f"Skipping { u } , no usable password" )
39
+ print (f"Skipping { u . pk } , no usable password" )
40
40
return None
41
41
if u .email is None or len (u .email ) == 0 :
42
- print (f"Skipping { u } , no email" )
42
+ emails = Email .objects .filter (user = u )
43
+ # print(u.email_user)
44
+ print (f"Skipping { u .pk } , no email or? { emails } " )
43
45
return None
44
46
# if u.auth0_subject is not None:
45
47
# print(f"Skipping {u}, already migrated")
@@ -48,7 +50,7 @@ def create_auth0_user(u: OnlineUser):
48
50
try :
49
51
algorithm , iterations , salt , hash = u .password .split ("$" , 3 )
50
52
except ValueError as e :
51
- print (f"{ e = } \n { u = } \n { u .password = } " )
53
+ print (f"{ e } \n { u . pk = } \n { u .password = } " )
52
54
return None
53
55
54
56
# thank you https://community.auth0.com/t/wrong-password-for-imported-users-from-django/61105
@@ -58,24 +60,34 @@ def create_auth0_user(u: OnlineUser):
58
60
# we probably only use pbkdf2_sha256, in auth0 they use -
59
61
algorithm = algorithm .replace ("_" , "-" )
60
62
61
- id = str (uuid4 ())
62
- u .auth0_subject = f"auth0|{ id } "
63
+ user_previously_exported = True
64
+ if not u .auth0_subject :
65
+ user_previously_exported = False
66
+ id = str (uuid4 ())
67
+ u .auth0_subject = f"auth0|{ id } "
63
68
64
69
auth0_user = {
65
- "user_id" : id ,
70
+ "user_id" : u . auth0_subject . split ( "|" )[ 1 ] ,
66
71
"email" : u .email ,
67
72
"email_verified" : u .is_active ,
68
73
"given_name" : u .first_name ,
69
74
"family_name" : u .last_name ,
70
- "name" : f"{ u .first_name } { u .last_name } " ,
71
- "custom_password_hash" : {
75
+ "user_metadata" : {},
76
+ "app_metadata" : {
77
+ "ow4_userid" : u .pk ,
78
+ },
79
+ }
80
+
81
+ if not user_previously_exported :
82
+ # we do not want to export passwords of existing users in auth0
83
+ # auth0 then just errors out, and users might not remember their passwords
84
+ auth0_user ["custom_password_hash" ] = {
72
85
"algorithm" : "pbkdf2" ,
73
86
"hash" : {
74
87
"encoding" : "utf-8" ,
75
88
"value" : f"$pbkdf2-sha256$i={ iterations } ,l=32${ salt } ${ hash } " ,
76
89
},
77
- },
78
- }
90
+ }
79
91
80
92
if len (u .first_name ) == 0 :
81
93
del auth0_user ["given_name" ]
@@ -84,21 +96,23 @@ def create_auth0_user(u: OnlineUser):
84
96
del auth0_user ["family_name" ]
85
97
86
98
if num := extract_phone_number (u ):
87
- auth0_user ["mfa_factors" ] = [{"phone" : {"value" : num }}]
99
+ auth0_user ["user_metadata" ]["phone" ] = num
100
+
101
+ if len (auth0_user ["user_metadata" ]) == 0 :
102
+ del auth0_user ["user_metadata" ]
88
103
89
104
return (u , auth0_user )
90
105
91
106
92
107
class Command (BaseCommand ):
93
108
def handle (self , * args , ** options ):
94
- users = [
95
- create_auth0_user (u ) for u in OnlineUser .objects .iterator (chunk_size = 100 )
96
- ]
109
+ qs = OnlineUser .objects .filter (is_active = True )
110
+ users = [create_auth0_user (u ) for u in qs .iterator (chunk_size = 100 )]
97
111
users = [u for u in users if u is not None ]
98
112
N = 700
99
- for i in range (int (OnlineUser . objects . count () / 700 ) ):
113
+ for i in range (int (qs . count () / N ) + 1 ):
100
114
chunk = users [i * N : i * N + N ]
101
115
OnlineUser .objects .bulk_update ([u for (u , _ ) in chunk ], ["auth0_subject" ])
102
116
file = json .dumps ([a0u for (_ , a0u ) in chunk ])
103
- with open (f"auth0_users_staging_ { i } .json" , "w" ) as f :
117
+ with open (f"auth0_users_prod_ { i } .json" , "w" ) as f :
104
118
f .write (file )
0 commit comments