From d8c598543ea69bd6cde3b6fd4a2cc4513df722e3 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Tue, 1 Dec 2026 15:34:42 +0100 Subject: [PATCH] Sync to latest version of community auth plugin --- pgcommitfest/auth.py | 63 +++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/pgcommitfest/auth.py b/pgcommitfest/auth.py index 06cc609c..a3427856 100644 --- a/pgcommitfest/auth.py +++ b/pgcommitfest/auth.py @@ -164,22 +164,27 @@ def auth_receive(request): # somehow fix that live, give a proper error message and # have somebody look at it manually. if User.objects.filter(email=data['e'][0]).exists(): + if hasattr(settings, 'ADMINS') and len(settings.ADMINS) > 0: + contact = settings.ADMINS[0][1] + else: + contact = "webmaster@postgresql.org" + return HttpResponse("""A user with email %s already exists, but with a different username than %s. This is almost certainly caused by some legacy data in our database. -Please send an email to webmaster@postgresql.org, indicating the username +Please send an email to %s, indicating the username and email address from above, and we'll manually merge the two accounts for you. We apologize for the inconvenience. -""" % (data['e'][0], data['u'][0]), content_type='text/plain') +""" % (data['e'][0], data['u'][0], contact), content_type='text/plain') if getattr(settings, 'PGAUTH_CREATEUSER_CALLBACK', None): res = getattr(settings, 'PGAUTH_CREATEUSER_CALLBACK')( data['u'][0], data['e'][0], - ['f'][0], + data['f'][0], data['l'][0], ) # If anything is returned, we'll return that as our result. @@ -304,6 +309,34 @@ def _conditionally_update_record(rectype, recordkey, structkey, fieldmap, struct return HttpResponse("OK", status=200) +def _sign_pgauth_request(bodystruct): + body = json.dumps({ + 'ts': int(time.time()), + **bodystruct, + }) + + h = hmac.digest( + base64.b64decode(settings.PGAUTH_KEY), + msg=bytes(body, 'utf-8'), + digest='sha512', + ) + + return body, base64.b64encode(h) + + +def _post_signed_pgauth(suburl, bodystruct): + body, signature = _sign_pgauth_request(bodystruct) + + return requests.post( + '{}{}'.format(settings.PGAUTH_REDIRECT, suburl), + data=body, + headers={ + 'X-pgauth-sig': signature, + }, + timeout=10, + ) + + # Perform a search in the central system. Note that the results are returned as an # array of dicts, and *not* as User objects. To be able to for example reference the # user through a ForeignKey, a User object must be materialized locally. We don't do @@ -320,11 +353,8 @@ def user_search(searchterm=None, userid=None): else: q = {'s': searchterm} - r = requests.get( - '{0}search/'.format(settings.PGAUTH_REDIRECT), - params=q, - timeout=10, - ) + r = _post_signed_pgauth('search/', q) + if r.status_code != 200: return [] @@ -350,25 +380,10 @@ def user_search(searchterm=None, userid=None): def subscribe_to_user_changes(userid): socket.setdefaulttimeout(10) - body = json.dumps({ + _post_signed_pgauth('subscribe/', { 'u': userid, }) - h = hmac.digest( - base64.b64decode(settings.PGAUTH_KEY), - msg=bytes(body, 'utf-8'), - digest='sha512', - ) - - # Ignore the result code, just post it - requests.post( - '{0}subscribe/'.format(settings.PGAUTH_REDIRECT), - data=body, - headers={ - 'X-pgauth-sig': base64.b64encode(h), - }, - ) - # Import a user into the local authentication system. Will initially # make a search for it, and if anything other than one entry is returned