Skip to content

Commit e66c024

Browse files
committed
Updated PersonalApiKey models with correct python3 unicode/bytes handling.
- Legacy-Id: 16322
1 parent d40a5ac commit e66c024

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

ietf/person/models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from django.db import models
1717
from django.contrib.auth.models import User
1818
from django.template.loader import render_to_string
19+
from django.utils.encoding import smart_bytes
1920
from django.utils.text import slugify
2021
from simple_history.models import HistoricalRecords
2122

@@ -336,16 +337,18 @@ class PersonalApiKey(models.Model):
336337
def validate_key(cls, s):
337338
import struct, hashlib, base64
338339
try:
339-
key = base64.urlsafe_b64decode(six.binary_type(s))
340-
except TypeError:
340+
key = base64.urlsafe_b64decode(s)
341+
except TypeError as e:
341342
return None
343+
342344
id, salt, hash = struct.unpack(KEY_STRUCT, key)
343345
k = cls.objects.filter(id=id)
344346
if not k.exists():
345347
return None
346348
k = k.first()
347349
check = hashlib.sha256()
348350
for v in (str(id), str(k.person.id), k.created.isoformat(), k.endpoint, str(k.valid), salt, settings.SECRET_KEY):
351+
v = smart_bytes(v)
349352
check.update(v)
350353
return k if check.digest() == hash else None
351354

@@ -355,9 +358,10 @@ def hash(self):
355358
hash = hashlib.sha256()
356359
# Hash over: ( id, person, created, endpoint, valid, salt, secret )
357360
for v in (str(self.id), str(self.person.id), self.created.isoformat(), self.endpoint, str(self.valid), self.salt, settings.SECRET_KEY):
361+
v = smart_bytes(v)
358362
hash.update(v)
359363
key = struct.pack(KEY_STRUCT, self.id, six.binary_type(self.salt), hash.digest())
360-
self._cached_hash = base64.urlsafe_b64encode(key)
364+
self._cached_hash = base64.urlsafe_b64encode(key).decode('ascii')
361365
return self._cached_hash
362366

363367
def __unicode__(self):

0 commit comments

Comments
 (0)