Browse Source

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

Danielle McLean 7 months ago
parent
commit
4fd2ff826a
Signed by: Danielle McLean <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 @@
1
+# Generated by Django 2.0.7 on 2018-07-11 03:07
2
+
3
+import computed_property.fields
4
+from django.db import migrations
5
+
6
+
7
+class Migration(migrations.Migration):
8
+
9
+    dependencies = [
10
+        ('users', '0014_auto_20180711_1248'),
11
+    ]
12
+
13
+    operations = [
14
+        migrations.AddField(
15
+            model_name='user',
16
+            name='openid_sha256',
17
+            field=computed_property.fields.ComputedCharField(compute_from='calc_openid_sha256', default='', editable=False,
18
+                                                             help_text="SHA-256 hash of the user's OpenID URL, used for Libravatar", max_length=64, unique=True),
19
+            preserve_default=False,
20
+        ),
21
+    ]

+ 8
- 0
users/models.py View File

@@ -70,6 +70,10 @@ class User(ModelMeta, AbstractUser):
70 70
         compute_from='calc_email_sha256', max_length=64, unique=True,
71 71
         help_text="SHA-256 hash of the user's email, used for Libravatar"
72 72
     )
73
+    openid_sha256 = ComputedCharField(
74
+        compute_from='calc_openid_sha256', max_length=64, unique=True,
75
+        help_text="SHA-256 hash of the user's OpenID URL, used for Libravatar"
76
+    )
73 77
 
74 78
     @property
75 79
     def calc_email_md5(self):
@@ -79,6 +83,10 @@ class User(ModelMeta, AbstractUser):
79 83
     def calc_email_sha256(self):
80 84
         return sha256(self.email.lower().encode('utf-8')).hexdigest()
81 85
 
86
+    @property
87
+    def calc_openid_sha256(self):
88
+        return sha256(self.full_url.encode('utf-8')).hexdigest()
89
+
82 90
     @property
83 91
     def name(self):
84 92
         return '{0} {1}'.format(self.first_name, self.last_name)

+ 4
- 3
users/views.py View File

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

Loading…
Cancel
Save