@@ -69,27 +69,27 @@ def authenticate(self, username=None, password=None):
69
69
70
70
# Handle login requests by sending them off to the main site
71
71
def login (request ):
72
- if ' next' in request .GET :
72
+ if " next" in request .GET :
73
73
# Put together an url-encoded dict of parameters we're getting back,
74
74
# including a small nonce at the beginning to make sure it doesn't
75
75
# encrypt the same way every time.
76
- s = "t=%s&%s" % (int (time .time ()), urlencode ({'r' : request .GET [' next' ]}))
76
+ s = "t=%s&%s" % (int (time .time ()), urlencode ({"r" : request .GET [" next" ]}))
77
77
# Now encrypt it
78
78
r = Random .new ()
79
79
iv = r .read (16 )
80
80
encryptor = AES .new (
81
- SHA .new (settings .SECRET_KEY .encode (' ascii' )).digest ()[:16 ], AES .MODE_CBC , iv
81
+ SHA .new (settings .SECRET_KEY .encode (" ascii" )).digest ()[:16 ], AES .MODE_CBC , iv
82
82
)
83
83
cipher = encryptor .encrypt (
84
- s .encode (' ascii' ) + b' ' * (16 - (len (s ) % 16 ))
84
+ s .encode (" ascii" ) + b" " * (16 - (len (s ) % 16 ))
85
85
) # pad to 16 bytes
86
86
87
87
return HttpResponseRedirect (
88
88
"%s?d=%s$%s"
89
89
% (
90
90
settings .PGAUTH_REDIRECT ,
91
- base64 .b64encode (iv , b"-_" ).decode (' utf8' ),
92
- base64 .b64encode (cipher , b"-_" ).decode (' utf8' ),
91
+ base64 .b64encode (iv , b"-_" ).decode (" utf8" ),
92
+ base64 .b64encode (cipher , b"-_" ).decode (" utf8" ),
93
93
)
94
94
)
95
95
else :
@@ -107,26 +107,26 @@ def logout(request):
107
107
# Receive an authentication response from the main website and try
108
108
# to log the user in.
109
109
def auth_receive (request ):
110
- if 's' in request .GET and request .GET ['s' ] == "logout" :
110
+ if "s" in request .GET and request .GET ["s" ] == "logout" :
111
111
# This was a logout request
112
- return HttpResponseRedirect ('/' )
112
+ return HttpResponseRedirect ("/" )
113
113
114
- if 'i' not in request .GET :
114
+ if "i" not in request .GET :
115
115
return HttpResponse ("Missing IV in url!" , status = 400 )
116
- if 'd' not in request .GET :
116
+ if "d" not in request .GET :
117
117
return HttpResponse ("Missing data in url!" , status = 400 )
118
118
119
119
# Set up an AES object and decrypt the data we received
120
120
try :
121
121
decryptor = AES .new (
122
122
base64 .b64decode (settings .PGAUTH_KEY ),
123
123
AES .MODE_CBC ,
124
- base64 .b64decode (str (request .GET ['i' ]), "-_" ),
124
+ base64 .b64decode (str (request .GET ["i" ]), "-_" ),
125
125
)
126
126
s = (
127
- decryptor .decrypt (base64 .b64decode (str (request .GET ['d' ]), "-_" ))
128
- .rstrip (b' ' )
129
- .decode (' utf8' )
127
+ decryptor .decrypt (base64 .b64decode (str (request .GET ["d" ]), "-_" ))
128
+ .rstrip (b" " )
129
+ .decode (" utf8" )
130
130
)
131
131
except UnicodeDecodeError :
132
132
return HttpResponse ("Badly encoded data found" , 400 )
@@ -140,23 +140,23 @@ def auth_receive(request):
140
140
return HttpResponse ("Invalid encrypted data received." , status = 400 )
141
141
142
142
# Check the timestamp in the authentication
143
- if int (data ['t' ][0 ]) < time .time () - 10 :
143
+ if int (data ["t" ][0 ]) < time .time () - 10 :
144
144
return HttpResponse ("Authentication token too old." , status = 400 )
145
145
146
146
# Update the user record (if any)
147
147
try :
148
- user = User .objects .get (username = data ['u' ][0 ])
148
+ user = User .objects .get (username = data ["u" ][0 ])
149
149
# User found, let's see if any important fields have changed
150
150
changed = []
151
- if user .first_name != data ['f' ][0 ]:
152
- user .first_name = data ['f' ][0 ]
153
- changed .append (' first_name' )
154
- if user .last_name != data ['l' ][0 ]:
155
- user .last_name = data ['l' ][0 ]
156
- changed .append (' last_name' )
157
- if user .email != data ['e' ][0 ]:
158
- user .email = data ['e' ][0 ]
159
- changed .append (' email' )
151
+ if user .first_name != data ["f" ][0 ]:
152
+ user .first_name = data ["f" ][0 ]
153
+ changed .append (" first_name" )
154
+ if user .last_name != data ["l" ][0 ]:
155
+ user .last_name = data ["l" ][0 ]
156
+ changed .append (" last_name" )
157
+ if user .email != data ["e" ][0 ]:
158
+ user .email = data ["e" ][0 ]
159
+ changed .append (" email" )
160
160
if changed :
161
161
user .save (update_fields = changed )
162
162
except User .DoesNotExist :
@@ -166,7 +166,7 @@ def auth_receive(request):
166
166
# the database with a different userid. Instead of trying to
167
167
# somehow fix that live, give a proper error message and
168
168
# have somebody look at it manually.
169
- if User .objects .filter (email = data ['e' ][0 ]).exists ():
169
+ if User .objects .filter (email = data ["e" ][0 ]).exists ():
170
170
return HttpResponse (
171
171
"""A user with email %s already exists, but with
172
172
a different username than %s.
@@ -178,28 +178,28 @@ def auth_receive(request):
178
178
179
179
We apologize for the inconvenience.
180
180
"""
181
- % (data ['e' ][0 ], data ['u' ][0 ]),
182
- content_type = ' text/plain' ,
181
+ % (data ["e" ][0 ], data ["u" ][0 ]),
182
+ content_type = " text/plain" ,
183
183
)
184
184
185
- if getattr (settings , ' PGAUTH_CREATEUSER_CALLBACK' , None ):
186
- res = getattr (settings , ' PGAUTH_CREATEUSER_CALLBACK' )(
187
- data ['u' ][0 ],
188
- data ['e' ][0 ],
189
- ['f' ][0 ],
190
- data ['l' ][0 ],
185
+ if getattr (settings , " PGAUTH_CREATEUSER_CALLBACK" , None ):
186
+ res = getattr (settings , " PGAUTH_CREATEUSER_CALLBACK" )(
187
+ data ["u" ][0 ],
188
+ data ["e" ][0 ],
189
+ ["f" ][0 ],
190
+ data ["l" ][0 ],
191
191
)
192
192
# If anything is returned, we'll return that as our result.
193
193
# If None is returned, it means go ahead and create the user.
194
194
if res :
195
195
return res
196
196
197
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' ,
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
203
)
204
204
user .save ()
205
205
@@ -215,28 +215,28 @@ def auth_receive(request):
215
215
auth_user_data_received .send (
216
216
sender = auth_receive ,
217
217
user = user ,
218
- userdata = {' secondaryemails' : data ['se' ][0 ].split (',' ) if 'se' in data else []},
218
+ userdata = {" secondaryemails" : data ["se" ][0 ].split ("," ) if "se" in data else []},
219
219
)
220
220
221
221
# Finally, check of we have a data package that tells us where to
222
222
# redirect the user.
223
- if 'd' in data :
224
- (ivs , datas ) = data ['d' ][0 ].split ('$' )
223
+ if "d" in data :
224
+ (ivs , datas ) = data ["d" ][0 ].split ("$" )
225
225
decryptor = AES .new (
226
- SHA .new (settings .SECRET_KEY .encode (' ascii' )).digest ()[:16 ],
226
+ SHA .new (settings .SECRET_KEY .encode (" ascii" )).digest ()[:16 ],
227
227
AES .MODE_CBC ,
228
228
base64 .b64decode (ivs , b"-_" ),
229
229
)
230
- s = decryptor .decrypt (base64 .b64decode (datas , "-_" )).rstrip (b' ' ).decode (' utf8' )
230
+ s = decryptor .decrypt (base64 .b64decode (datas , "-_" )).rstrip (b" " ).decode (" utf8" )
231
231
try :
232
232
rdata = parse_qs (s , strict_parsing = True )
233
233
except ValueError :
234
234
return HttpResponse ("Invalid encrypted data received." , status = 400 )
235
- if 'r' in rdata :
235
+ if "r" in rdata :
236
236
# Redirect address
237
- return HttpResponseRedirect (rdata ['r' ][0 ])
237
+ return HttpResponseRedirect (rdata ["r" ][0 ])
238
238
# No redirect specified, see if we have it in our settings
239
- if hasattr (settings , ' PGAUTH_REDIRECT_SUCCESS' ):
239
+ if hasattr (settings , " PGAUTH_REDIRECT_SUCCESS" ):
240
240
return HttpResponseRedirect (settings .PGAUTH_REDIRECT_SUCCESS )
241
241
return HttpResponse (
242
242
"Authentication successful, but don't know where to redirect!" , status = 500
@@ -246,19 +246,19 @@ def auth_receive(request):
246
246
# Receive API calls from upstream, such as push changes to users
247
247
@csrf_exempt
248
248
def auth_api (request ):
249
- if ' X-pgauth-sig' not in request .headers :
249
+ if " X-pgauth-sig" not in request .headers :
250
250
return HttpResponse ("Missing signature header!" , status = 400 )
251
251
252
252
try :
253
- sig = base64 .b64decode (request .headers [' X-pgauth-sig' ])
253
+ sig = base64 .b64decode (request .headers [" X-pgauth-sig" ])
254
254
except Exception :
255
255
return HttpResponse ("Invalid signature header!" , status = 400 )
256
256
257
257
try :
258
258
h = hmac .digest (
259
259
base64 .b64decode (settings .PGAUTH_KEY ),
260
260
msg = request .body ,
261
- digest = ' sha512' ,
261
+ digest = " sha512" ,
262
262
)
263
263
if not hmac .compare_digest (h , sig ):
264
264
return HttpResponse ("Invalid signature!" , status = 401 )
@@ -286,18 +286,18 @@ def _conditionally_update_record(rectype, recordkey, structkey, fieldmap, struct
286
286
return None
287
287
288
288
# Process the received structure
289
- if pushstruct .get (' type' , None ) == ' update' :
289
+ if pushstruct .get (" type" , None ) == " update" :
290
290
# Process updates!
291
291
with transaction .atomic ():
292
- for u in pushstruct .get (' users' , []):
292
+ for u in pushstruct .get (" users" , []):
293
293
user = _conditionally_update_record (
294
294
User ,
295
- ' username' ,
296
- ' username' ,
295
+ " username" ,
296
+ " username" ,
297
297
{
298
- ' firstname' : ' first_name' ,
299
- ' lastname' : ' last_name' ,
300
- ' email' : ' email' ,
298
+ " firstname" : " first_name" ,
299
+ " lastname" : " last_name" ,
300
+ " email" : " email" ,
301
301
},
302
302
u ,
303
303
)
@@ -312,9 +312,9 @@ def _conditionally_update_record(rectype, recordkey, structkey, fieldmap, struct
312
312
for k in u .keys ()
313
313
if k
314
314
not in [
315
- ' firstname' ,
316
- ' lastname' ,
317
- ' email' ,
315
+ " firstname" ,
316
+ " lastname" ,
317
+ " email" ,
318
318
]
319
319
},
320
320
)
@@ -334,24 +334,24 @@ def user_search(searchterm=None, userid=None):
334
334
# 10 seconds is already quite long.
335
335
socket .setdefaulttimeout (10 )
336
336
if userid :
337
- q = {'u' : userid }
337
+ q = {"u" : userid }
338
338
else :
339
- q = {'s' : searchterm }
339
+ q = {"s" : searchterm }
340
340
341
341
r = requests .get (
342
- ' {0}search/' .format (settings .PGAUTH_REDIRECT ),
342
+ " {0}search/" .format (settings .PGAUTH_REDIRECT ),
343
343
params = q ,
344
344
)
345
345
if r .status_code != 200 :
346
346
return []
347
347
348
- (ivs , datas ) = r .text .encode (' utf8' ).split (b'&' )
348
+ (ivs , datas ) = r .text .encode (" utf8" ).split (b"&" )
349
349
350
350
# Decryption time
351
351
decryptor = AES .new (
352
352
base64 .b64decode (settings .PGAUTH_KEY ), AES .MODE_CBC , base64 .b64decode (ivs , "-_" )
353
353
)
354
- s = decryptor .decrypt (base64 .b64decode (datas , "-_" )).rstrip (b' ' ).decode (' utf8' )
354
+ s = decryptor .decrypt (base64 .b64decode (datas , "-_" )).rstrip (b" " ).decode (" utf8" )
355
355
j = json .loads (s )
356
356
357
357
return j
@@ -363,22 +363,22 @@ def subscribe_to_user_changes(userid):
363
363
364
364
body = json .dumps (
365
365
{
366
- 'u' : userid ,
366
+ "u" : userid ,
367
367
}
368
368
)
369
369
370
370
h = hmac .digest (
371
371
base64 .b64decode (settings .PGAUTH_KEY ),
372
- msg = bytes (body , ' utf-8' ),
373
- digest = ' sha512' ,
372
+ msg = bytes (body , " utf-8" ),
373
+ digest = " sha512" ,
374
374
)
375
375
376
376
# Ignore the result code, just post it
377
377
requests .post (
378
- ' {0}subscribe/' .format (settings .PGAUTH_REDIRECT ),
378
+ " {0}subscribe/" .format (settings .PGAUTH_REDIRECT ),
379
379
data = body ,
380
380
headers = {
381
- ' X-pgauth-sig' : base64 .b64encode (h ),
381
+ " X-pgauth-sig" : base64 .b64encode (h ),
382
382
},
383
383
)
384
384
@@ -398,15 +398,15 @@ def user_import(uid):
398
398
399
399
u = u [0 ]
400
400
401
- if User .objects .filter (username = u ['u' ]).exists ():
401
+ if User .objects .filter (username = u ["u" ]).exists ():
402
402
raise Exception ("User already exists" )
403
403
404
404
u = User (
405
- username = u ['u' ],
406
- first_name = u ['f' ],
407
- last_name = u ['l' ],
408
- email = u ['e' ],
409
- password = ' setbypluginnotsha1' ,
405
+ username = u ["u" ],
406
+ first_name = u ["f" ],
407
+ last_name = u ["l" ],
408
+ email = u ["e" ],
409
+ password = " setbypluginnotsha1" ,
410
410
)
411
411
u .save ()
412
412
0 commit comments