~ubuntu-branches/debian/sid/social-auth-core/sid

« back to all changes in this revision

Viewing changes to social_core/pipeline/social_auth.py

  • Committer: Package Import Robot
  • Author(s): Andre Bianchi
  • Date: 2018-02-22 19:49:12 UTC
  • Revision ID: package-import@ubuntu.com-20180222194912-4lqv8mlhnqc4ncd3
Tags: upstream-1.7.0
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from ..exceptions import AuthAlreadyAssociated, AuthException, AuthForbidden
 
2
 
 
3
 
 
4
def social_details(backend, details, response, *args, **kwargs):
 
5
    return {'details': dict(backend.get_user_details(response), **details)}
 
6
 
 
7
 
 
8
def social_uid(backend, details, response, *args, **kwargs):
 
9
    return {'uid': backend.get_user_id(details, response)}
 
10
 
 
11
 
 
12
def auth_allowed(backend, details, response, *args, **kwargs):
 
13
    if not backend.auth_allowed(response, details):
 
14
        raise AuthForbidden(backend)
 
15
 
 
16
 
 
17
def social_user(backend, uid, user=None, *args, **kwargs):
 
18
    provider = backend.name
 
19
    social = backend.strategy.storage.user.get_social_auth(provider, uid)
 
20
    if social:
 
21
        if user and social.user != user:
 
22
            msg = 'This {0} account is already in use.'.format(provider)
 
23
            raise AuthAlreadyAssociated(backend, msg)
 
24
        elif not user:
 
25
            user = social.user
 
26
    return {'social': social,
 
27
            'user': user,
 
28
            'is_new': user is None,
 
29
            'new_association': social is None}
 
30
 
 
31
 
 
32
def associate_user(backend, uid, user=None, social=None, *args, **kwargs):
 
33
    if user and not social:
 
34
        try:
 
35
            social = backend.strategy.storage.user.create_social_auth(
 
36
                user, uid, backend.name
 
37
            )
 
38
        except Exception as err:
 
39
            if not backend.strategy.storage.is_integrity_error(err):
 
40
                raise
 
41
            # Protect for possible race condition, those bastard with FTL
 
42
            # clicking capabilities, check issue #131:
 
43
            #   https://github.com/omab/django-social-auth/issues/131
 
44
            return social_user(backend, uid, user, *args, **kwargs)
 
45
        else:
 
46
            return {'social': social,
 
47
                    'user': social.user,
 
48
                    'new_association': True}
 
49
 
 
50
 
 
51
def associate_by_email(backend, details, user=None, *args, **kwargs):
 
52
    """
 
53
    Associate current auth with a user with the same email address in the DB.
 
54
 
 
55
    This pipeline entry is not 100% secure unless you know that the providers
 
56
    enabled enforce email verification on their side, otherwise a user can
 
57
    attempt to take over another user account by using the same (not validated)
 
58
    email address on some provider.  This pipeline entry is disabled by
 
59
    default.
 
60
    """
 
61
    if user:
 
62
        return None
 
63
 
 
64
    email = details.get('email')
 
65
    if email:
 
66
        # Try to associate accounts registered with the same email address,
 
67
        # only if it's a single object. AuthException is raised if multiple
 
68
        # objects are returned.
 
69
        users = list(backend.strategy.storage.user.get_users_by_email(email))
 
70
        if len(users) == 0:
 
71
            return None
 
72
        elif len(users) > 1:
 
73
            raise AuthException(
 
74
                backend,
 
75
                'The given email address is associated with another account'
 
76
            )
 
77
        else:
 
78
            return {'user': users[0],
 
79
                    'is_new': False}
 
80
 
 
81
 
 
82
def load_extra_data(backend, details, response, uid, user, *args, **kwargs):
 
83
    social = kwargs.get('social') or \
 
84
             backend.strategy.storage.user.get_social_auth(backend.name, uid)
 
85
    if social:
 
86
        extra_data = backend.extra_data(user, uid, response, details,
 
87
                                        *args, **kwargs)
 
88
        social.set_extra_data(extra_data)