Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 39 additions & 24 deletions pgcommitfest/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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 []

Expand All @@ -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
Expand Down
Loading