Skip to content

Commit 945008d

Browse files
Checking for pre-existing accounts from a different flow when using social connect
1 parent 42d039b commit 945008d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

rest_auth/registration/serializers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.http import HttpRequest
22
from django.conf import settings
33
from django.utils.translation import ugettext_lazy as _
4+
from django.contrib.auth import get_user_model
45

56
try:
67
from allauth.account import app_settings as allauth_settings
@@ -111,6 +112,20 @@ def validate(self, attrs):
111112
raise serializers.ValidationError(_('Incorrect value'))
112113

113114
if not login.is_existing:
115+
# We have an account already signed up in a different flow
116+
# with the same email address: raise an exception.
117+
# This needs to be handled in the frontend. We can not just
118+
# link up the accounts due to security constraints
119+
if(allauth_settings.UNIQUE_EMAIL):
120+
# Do we have an account already with this email address?
121+
existing_account = get_user_model().objects.filter(
122+
email=login.user.email,
123+
).count()
124+
if(existing_account != 0):
125+
# There is an account already
126+
raise serializers.ValidationError(
127+
_("A user is already registered with this e-mail address."))
128+
114129
login.lookup()
115130
login.save(request, connect=True)
116131
attrs['user'] = login.account.user

rest_auth/tests/requirements.pip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ django-allauth>=0.25.0
22
responses>=0.3.0
33
flake8==2.4.0
44
djangorestframework-jwt>=1.7.2
5+
djangorestframework>=3.6.4

rest_auth/tests/test_social.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,12 @@ def test_edge_case(self):
275275
'access_token': 'abc123'
276276
}
277277

278-
self.post(self.fb_login_url, data=payload, status_code=200)
279-
self.assertIn('key', self.response.json.keys())
278+
# You should not have access to an account created through register
279+
# by loging in through FB with an account that has the same
280+
# email address.
281+
self.post(self.fb_login_url, data=payload, status_code=400)
282+
# self.post(self.fb_login_url, data=payload, status_code=200)
283+
# self.assertIn('key', self.response.json.keys())
280284

281285
@responses.activate
282286
@override_settings(

0 commit comments

Comments
 (0)