Support Libravatar matching by OpenID URL as well as by email address

This commit is contained in:
Danielle McLean 2018-07-11 13:13:12 +10:00
parent 6efcc450a3
commit 4fd2ff826a
Signed by untrusted user: 00dani
GPG key ID: 8EB789DDF3ABD240
3 changed files with 33 additions and 3 deletions

View file

@ -0,0 +1,21 @@
# Generated by Django 2.0.7 on 2018-07-11 03:07
import computed_property.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('users', '0014_auto_20180711_1248'),
]
operations = [
migrations.AddField(
model_name='user',
name='openid_sha256',
field=computed_property.fields.ComputedCharField(compute_from='calc_openid_sha256', default='', editable=False,
help_text="SHA-256 hash of the user's OpenID URL, used for Libravatar", max_length=64, unique=True),
preserve_default=False,
),
]

View file

@ -70,6 +70,10 @@ class User(ModelMeta, AbstractUser):
compute_from='calc_email_sha256', max_length=64, unique=True,
help_text="SHA-256 hash of the user's email, used for Libravatar"
)
openid_sha256 = ComputedCharField(
compute_from='calc_openid_sha256', max_length=64, unique=True,
help_text="SHA-256 hash of the user's OpenID URL, used for Libravatar"
)
@property
def calc_email_md5(self):
@ -79,6 +83,10 @@ class User(ModelMeta, AbstractUser):
def calc_email_sha256(self):
return sha256(self.email.lower().encode('utf-8')).hexdigest()
@property
def calc_openid_sha256(self):
return sha256(self.full_url.encode('utf-8')).hexdigest()
@property
def name(self):
return '{0} {1}'.format(self.first_name, self.last_name)

View file

@ -1,3 +1,4 @@
from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.cache import cache_page
from PIL import Image
@ -25,9 +26,9 @@ def libravatar(request, hash):
return utils.bad_req('size parameter must be between 1 and 512')
if len(hash) == 32:
where = {'email_md5': hash}
where = Q(email_md5=hash)
elif len(hash) == 64:
where = {'email_sha256': hash}
where = Q(email_sha256=hash) | Q(openid_sha256=hash)
else:
return utils.bad_req('hash must be either md5 or sha256')
@ -36,7 +37,7 @@ def libravatar(request, hash):
# for MD5 hashes, since Gravatar doesn't support SHA-256), so this ensures
# all the most likely places are checked.
try:
user = User.objects.get(**where)
user = User.objects.get(where)
except User.DoesNotExist:
return try_libravatar_org(hash, g)