Browse Source

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

tags/v1.10.1
Danielle McLean 1 year ago
parent
commit
4fd2ff826a
Signed by: 00dani <dani@00dani.me> GPG Key ID: 8EB789DDF3ABD240
3 changed files with 33 additions and 3 deletions
  1. +21
    -0
      users/migrations/0015_user_openid_sha256.py
  2. +8
    -0
      users/models.py
  3. +4
    -3
      users/views.py

+ 21
- 0
users/migrations/0015_user_openid_sha256.py 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,
),
]

+ 8
- 0
users/models.py 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)


+ 4
- 3
users/views.py 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)



Loading…
Cancel
Save