Skip to content

Commit a78914c

Browse files
committed
Fix timestamp format in simpleauth
Testing the recent changes to utils.redisobjs on staging, attempting to log into the inventory app resulted in a TypeError when comparing the stored (Redis) last login timestamp with the timestamp in the HTTP header. I realized that the changes around the new 'encoded_obj' faux Redis type meant it was now interpreting the timestamp as a string when pulling it from Redis. I updated the api.simpleauth authentication class so that it explicitly converts BOTH timestamps to floats when comparing them and then explicitly stores the timestamp in Redis as a string. I also did a little code cleanup.
1 parent 3c88218 commit a78914c

1 file changed

Lines changed: 16 additions & 26 deletions

File tree

django/sierra/api/simpleauth.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,30 @@
1515

1616

1717
class SimpleSignatureAuthentication(authentication.BaseAuthentication):
18+
1819
def authenticate(self, request):
19-
ret_val = None
2020
username = request.META.get('HTTP_X_USERNAME', None)
2121
timestamp = request.META.get('HTTP_X_TIMESTAMP', None)
22-
client_signature = request.META.get('HTTP_AUTHORIZATION', 'Basic ')
23-
client_signature = client_signature.split('Basic ')[1]
22+
client_signature = request.META.get(
23+
'HTTP_AUTHORIZATION', 'Basic '
24+
).split('Basic ')[1]
2425
body = ensure_str(request.body)
26+
bad_credentials_msg = 'Incorrect username or password'
27+
invalid_timestamp_msg = 'Timestamp invalid.'
2528

2629
if username and timestamp and client_signature:
2730
try:
2831
api_user = models.APIUser.objects.get(user__username=username)
2932
except models.APIUser.DoesNotExist:
30-
raise exceptions.AuthenticationFailed('Incorrect username or '
31-
'password.')
32-
33+
raise exceptions.AuthenticationFailed(bad_credentials_msg)
3334
user_timestamp = ro.RedisObject('user_timestamp', username)
3435
last_ts = user_timestamp.get() or 0
35-
36-
if last_ts >= float(timestamp):
37-
raise exceptions.AuthenticationFailed('Timestamp invalid.')
38-
39-
secret = api_user.secret
40-
user = api_user.user
41-
42-
hasher = hashlib.sha256('{}{}{}{}'.format(username, secret,
43-
timestamp,
44-
body).encode('utf-8'))
45-
server_signature = hasher.hexdigest()
46-
47-
if server_signature != client_signature:
48-
raise exceptions.AuthenticationFailed('Incorrect username or '
49-
'password.')
50-
else:
51-
user_timestamp.set(float(timestamp))
52-
ret_val = (user, None)
53-
54-
return ret_val
36+
if float(last_ts) >= float(timestamp):
37+
raise exceptions.AuthenticationFailed(invalid_timestamp_msg)
38+
server_signature = hashlib.sha256(
39+
f'{username}{api_user.secret}{timestamp}{body}'.encode('utf-8')
40+
).hexdigest()
41+
if server_signature == client_signature:
42+
user_timestamp.set(str(timestamp))
43+
return (api_user.user, None)
44+
raise exceptions.AuthenticationFailed(bad_credentials_msg)

0 commit comments

Comments
 (0)