@@ -66,6 +66,7 @@ def authenticate(self, username=None, password=None):
6666# Two regular django views to interact with the login system
6767####
6868
69+
6970# Handle login requests by sending them off to the main site
7071def login (request ):
7172 if 'next' in request .GET :
@@ -76,14 +77,21 @@ def login(request):
7677 # Now encrypt it
7778 r = Random .new ()
7879 iv = r .read (16 )
79- encryptor = AES .new (SHA .new (settings .SECRET_KEY .encode ('ascii' )).digest ()[:16 ], AES .MODE_CBC , iv )
80- cipher = encryptor .encrypt (s .encode ('ascii' ) + b' ' * (16 - (len (s ) % 16 ))) # pad to 16 bytes
81-
82- return HttpResponseRedirect ("%s?d=%s$%s" % (
83- settings .PGAUTH_REDIRECT ,
84- base64 .b64encode (iv , b"-_" ).decode ('utf8' ),
85- base64 .b64encode (cipher , b"-_" ).decode ('utf8' ),
86- ))
80+ encryptor = AES .new (
81+ SHA .new (settings .SECRET_KEY .encode ('ascii' )).digest ()[:16 ], AES .MODE_CBC , iv
82+ )
83+ cipher = encryptor .encrypt (
84+ s .encode ('ascii' ) + b' ' * (16 - (len (s ) % 16 ))
85+ ) # pad to 16 bytes
86+
87+ return HttpResponseRedirect (
88+ "%s?d=%s$%s"
89+ % (
90+ settings .PGAUTH_REDIRECT ,
91+ base64 .b64encode (iv , b"-_" ).decode ('utf8' ),
92+ base64 .b64encode (cipher , b"-_" ).decode ('utf8' ),
93+ )
94+ )
8795 else :
8896 return HttpResponseRedirect (settings .PGAUTH_REDIRECT )
8997
@@ -110,10 +118,16 @@ def auth_receive(request):
110118
111119 # Set up an AES object and decrypt the data we received
112120 try :
113- decryptor = AES .new (base64 .b64decode (settings .PGAUTH_KEY ),
114- AES .MODE_CBC ,
115- base64 .b64decode (str (request .GET ['i' ]), "-_" ))
116- s = decryptor .decrypt (base64 .b64decode (str (request .GET ['d' ]), "-_" )).rstrip (b' ' ).decode ('utf8' )
121+ decryptor = AES .new (
122+ base64 .b64decode (settings .PGAUTH_KEY ),
123+ AES .MODE_CBC ,
124+ base64 .b64decode (str (request .GET ['i' ]), "-_" ),
125+ )
126+ s = (
127+ decryptor .decrypt (base64 .b64decode (str (request .GET ['d' ]), "-_" ))
128+ .rstrip (b' ' )
129+ .decode ('utf8' )
130+ )
117131 except UnicodeDecodeError :
118132 return HttpResponse ("Badly encoded data found" , 400 )
119133 except Exception :
@@ -126,7 +140,7 @@ def auth_receive(request):
126140 return HttpResponse ("Invalid encrypted data received." , status = 400 )
127141
128142 # Check the timestamp in the authentication
129- if ( int (data ['t' ][0 ]) < time .time () - 10 ) :
143+ if int (data ['t' ][0 ]) < time .time () - 10 :
130144 return HttpResponse ("Authentication token too old." , status = 400 )
131145
132146 # Update the user record (if any)
@@ -153,7 +167,8 @@ def auth_receive(request):
153167 # somehow fix that live, give a proper error message and
154168 # have somebody look at it manually.
155169 if User .objects .filter (email = data ['e' ][0 ]).exists ():
156- return HttpResponse ("""A user with email %s already exists, but with
170+ return HttpResponse (
171+ """A user with email %s already exists, but with
157172a different username than %s.
158173
159174This is almost certainly caused by some legacy data in our database.
@@ -162,7 +177,10 @@ def auth_receive(request):
162177for you.
163178
164179We apologize for the inconvenience.
165- """ % (data ['e' ][0 ], data ['u' ][0 ]), content_type = 'text/plain' )
180+ """
181+ % (data ['e' ][0 ], data ['u' ][0 ]),
182+ content_type = 'text/plain' ,
183+ )
166184
167185 if getattr (settings , 'PGAUTH_CREATEUSER_CALLBACK' , None ):
168186 res = getattr (settings , 'PGAUTH_CREATEUSER_CALLBACK' )(
@@ -176,12 +194,13 @@ def auth_receive(request):
176194 if res :
177195 return res
178196
179- user = User (username = data ['u' ][0 ],
180- first_name = data ['f' ][0 ],
181- last_name = data ['l' ][0 ],
182- email = data ['e' ][0 ],
183- password = 'setbypluginnotasha1' ,
184- )
197+ user = User (
198+ username = data ['u' ][0 ],
199+ first_name = data ['f' ][0 ],
200+ last_name = data ['l' ][0 ],
201+ email = data ['e' ][0 ],
202+ password = 'setbypluginnotasha1' ,
203+ )
185204 user .save ()
186205
187206 auth_user_created_from_upstream .send (sender = auth_receive , user = user )
@@ -193,17 +212,21 @@ def auth_receive(request):
193212 django_login (request , user )
194213
195214 # Signal that we have information about this user
196- auth_user_data_received .send (sender = auth_receive , user = user , userdata = {
197- 'secondaryemails' : data ['se' ][0 ].split (',' ) if 'se' in data else []
198- })
215+ auth_user_data_received .send (
216+ sender = auth_receive ,
217+ user = user ,
218+ userdata = {'secondaryemails' : data ['se' ][0 ].split (',' ) if 'se' in data else []},
219+ )
199220
200221 # Finally, check of we have a data package that tells us where to
201222 # redirect the user.
202223 if 'd' in data :
203224 (ivs , datas ) = data ['d' ][0 ].split ('$' )
204- decryptor = AES .new (SHA .new (settings .SECRET_KEY .encode ('ascii' )).digest ()[:16 ],
205- AES .MODE_CBC ,
206- base64 .b64decode (ivs , b"-_" ))
225+ decryptor = AES .new (
226+ SHA .new (settings .SECRET_KEY .encode ('ascii' )).digest ()[:16 ],
227+ AES .MODE_CBC ,
228+ base64 .b64decode (ivs , b"-_" ),
229+ )
207230 s = decryptor .decrypt (base64 .b64decode (datas , "-_" )).rstrip (b' ' ).decode ('utf8' )
208231 try :
209232 rdata = parse_qs (s , strict_parsing = True )
@@ -215,7 +238,9 @@ def auth_receive(request):
215238 # No redirect specified, see if we have it in our settings
216239 if hasattr (settings , 'PGAUTH_REDIRECT_SUCCESS' ):
217240 return HttpResponseRedirect (settings .PGAUTH_REDIRECT_SUCCESS )
218- return HttpResponse ("Authentication successful, but don't know where to redirect!" , status = 500 )
241+ return HttpResponse (
242+ "Authentication successful, but don't know where to redirect!" , status = 500
243+ )
219244
220245
221246# Receive API calls from upstream, such as push changes to users
@@ -267,7 +292,8 @@ def _conditionally_update_record(rectype, recordkey, structkey, fieldmap, struct
267292 for u in pushstruct .get ('users' , []):
268293 user = _conditionally_update_record (
269294 User ,
270- 'username' , 'username' ,
295+ 'username' ,
296+ 'username' ,
271297 {
272298 'firstname' : 'first_name' ,
273299 'lastname' : 'last_name' ,
@@ -278,9 +304,20 @@ def _conditionally_update_record(rectype, recordkey, structkey, fieldmap, struct
278304
279305 # Signal that we have information about this user (only if it exists)
280306 if user :
281- auth_user_data_received .send (sender = auth_api , user = user , userdata = {
282- k : u [k ] for k in u .keys () if k not in ['firstname' , 'lastname' , 'email' , ]
283- })
307+ auth_user_data_received .send (
308+ sender = auth_api ,
309+ user = user ,
310+ userdata = {
311+ k : u [k ]
312+ for k in u .keys ()
313+ if k
314+ not in [
315+ 'firstname' ,
316+ 'lastname' ,
317+ 'email' ,
318+ ]
319+ },
320+ )
284321
285322 return HttpResponse ("OK" , status = 200 )
286323
@@ -311,9 +348,9 @@ def user_search(searchterm=None, userid=None):
311348 (ivs , datas ) = r .text .encode ('utf8' ).split (b'&' )
312349
313350 # Decryption time
314- decryptor = AES .new (base64 . b64decode ( settings . PGAUTH_KEY ),
315- AES .MODE_CBC ,
316- base64 . b64decode ( ivs , "-_" ) )
351+ decryptor = AES .new (
352+ base64 . b64decode ( settings . PGAUTH_KEY ), AES .MODE_CBC , base64 . b64decode ( ivs , "-_" )
353+ )
317354 s = decryptor .decrypt (base64 .b64decode (datas , "-_" )).rstrip (b' ' ).decode ('utf8' )
318355 j = json .loads (s )
319356
@@ -324,9 +361,11 @@ def user_search(searchterm=None, userid=None):
324361def subscribe_to_user_changes (userid ):
325362 socket .setdefaulttimeout (10 )
326363
327- body = json .dumps ({
328- 'u' : userid ,
329- })
364+ body = json .dumps (
365+ {
366+ 'u' : userid ,
367+ }
368+ )
330369
331370 h = hmac .digest (
332371 base64 .b64decode (settings .PGAUTH_KEY ),
0 commit comments