pipeline.py 27.6 KB
Newer Older
1
"""Auth pipeline definitions.
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
Auth pipelines handle the process of authenticating a user. They involve a
consumer system and a provider service. The general pattern is:

    1. The consumer system exposes a URL endpoint that starts the process.
    2. When a user visits that URL, the client system redirects the user to a
       page served by the provider. The user authenticates with the provider.
       The provider handles authentication failure however it wants.
    3. On success, the provider POSTs to a URL endpoint on the consumer to
       invoke the pipeline. It sends back an arbitrary payload of data about
       the user.
    4. The pipeline begins, executing each function in its stack. The stack is
       defined on django's settings object's SOCIAL_AUTH_PIPELINE. This is done
       in settings._set_global_settings.
    5. Each pipeline function is variadic. Most pipeline functions are part of
       the pythons-social-auth library; our extensions are defined below. The
       pipeline is the same no matter what provider is used.
    6. Pipeline functions can return a dict to add arguments to the function
       invoked next. They can return None if this is not necessary.
    7. Pipeline functions may be decorated with @partial.partial. This pauses
       the pipeline and serializes its state onto the request's session. When
       this is done they may redirect to other edX handlers to execute edX
       account registration/sign in code.
    8. In that code, redirecting to get_complete_url() resumes the pipeline.
       This happens by hitting a handler exposed by the consumer system.
    9. In this way, execution moves between the provider, the pipeline, and
       arbitrary consumer system code.

Gotcha alert!:

Bear in mind that when pausing and resuming a pipeline function decorated with
@partial.partial, execution resumes by re-invoking the decorated function
instead of invoking the next function in the pipeline stack. For example, if
you have a pipeline of

    A
    B
    C

with an implementation of

    @partial.partial
    def B(*args, **kwargs):
        [...]

B will be invoked twice: once when initially proceeding through the pipeline
before it is paused, and once when other code finishes and the pipeline
resumes. Consequently, many decorated functions will first invoke a predicate
to determine if they are in their first or second execution (usually by
checking side-effects from the first run).

This is surprising but important behavior, since it allows a single function in
the pipeline to consolidate all the operations needed to establish invariants
rather than spreading them across two functions in the pipeline.

See http://psa.matiasaguirre.net/docs/pipeline.html for more docs.
"""

60 61 62 63
import base64
import hashlib
import hmac
import json
64
import random
65
import string
66 67
from collections import OrderedDict
import urllib
Julia Hansbrough committed
68 69
import analytics
from eventtracking import tracker
70

71
from django.conf import settings
72 73
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
74
from django.http import HttpResponseBadRequest
75 76 77
from django.shortcuts import redirect
from social.apps.django_app.default import models
from social.exceptions import AuthException
78
from social.pipeline import partial
79
from social.pipeline.social_auth import associate_by_email
80

81
import student
82

Julia Hansbrough committed
83 84
from logging import getLogger

85 86 87
from . import provider


88 89 90 91 92 93 94 95 96
# These are the query string params you can pass
# to the URL that starts the authentication process.
#
# `AUTH_ENTRY_KEY` is required and indicates how the user
# enters the authentication process.
#
# `AUTH_REDIRECT_KEY` provides an optional URL to redirect
# to upon successful authentication
# (if not provided, defaults to `_SOCIAL_AUTH_LOGIN_REDIRECT_URL`)
97
AUTH_ENTRY_KEY = 'auth_entry'
98 99
AUTH_REDIRECT_KEY = 'next'

100 101

# The following are various possible values for the AUTH_ENTRY_KEY.
102 103
AUTH_ENTRY_LOGIN = 'login'
AUTH_ENTRY_REGISTER = 'register'
104
AUTH_ENTRY_ACCOUNT_SETTINGS = 'account_settings'
105

106 107 108 109
# Entry modes into the authentication process by a remote API call (as opposed to a browser session).
AUTH_ENTRY_LOGIN_API = 'login_api'
AUTH_ENTRY_REGISTER_API = 'register_api'

110 111 112 113 114 115 116 117 118 119 120 121
# AUTH_ENTRY_CUSTOM: Custom auth entry point for post-auth integrations.
# This should be a dict where the key is a word passed via ?auth_entry=, and the
# value is a dict with an arbitrary 'secret_key' and a 'url'.
# This can be used as an extension point to inject custom behavior into the auth
# process, replacing the registration/login form that would normally be seen
# immediately after the user has authenticated with the third party provider.
# If a custom 'auth_entry' query parameter is used, then once the user has
# authenticated with a specific backend/provider, they will be redirected to the
# URL specified with this setting, rather than to the built-in
# registration/login form/logic.
AUTH_ENTRY_CUSTOM = getattr(settings, 'THIRD_PARTY_AUTH_CUSTOM_AUTH_FORMS', {})

122 123 124 125

def is_api(auth_entry):
    """Returns whether the auth entry point is via an API call."""
    return (auth_entry == AUTH_ENTRY_LOGIN_API) or (auth_entry == AUTH_ENTRY_REGISTER_API)
126

127 128 129 130 131 132 133 134
# URLs associated with auth entry points
# These are used to request additional user information
# (for example, account credentials when logging in),
# and when the user cancels the auth process
# (e.g., refusing to grant permission on the provider's login page).
# We don't use "reverse" here because doing so may cause modules
# to load that depend on this module.
AUTH_DISPATCH_URLS = {
135 136
    AUTH_ENTRY_LOGIN: '/login',
    AUTH_ENTRY_REGISTER: '/register',
137
    AUTH_ENTRY_ACCOUNT_SETTINGS: '/account/settings',
138 139
}

140 141
_AUTH_ENTRY_CHOICES = frozenset([
    AUTH_ENTRY_LOGIN,
142
    AUTH_ENTRY_REGISTER,
143
    AUTH_ENTRY_ACCOUNT_SETTINGS,
144 145
    AUTH_ENTRY_LOGIN_API,
    AUTH_ENTRY_REGISTER_API,
146
] + AUTH_ENTRY_CUSTOM.keys())
147

148 149 150
_DEFAULT_RANDOM_PASSWORD_LENGTH = 12
_PASSWORD_CHARSET = string.letters + string.digits

Julia Hansbrough committed
151 152
logger = getLogger(__name__)

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

class AuthEntryError(AuthException):
    """Raised when auth_entry is missing or invalid on URLs.

    auth_entry tells us whether the auth flow was initiated to register a new
    user (in which case it has the value of AUTH_ENTRY_REGISTER) or log in an
    existing user (in which case it has the value of AUTH_ENTRY_LOGIN).

    This is necessary because the edX code we hook into the pipeline to
    redirect to the existing auth flows needs to know what case we are in in
    order to format its output correctly (for example, the register code is
    invoked earlier than the login code, and it needs to know if the login flow
    was requested to dispatch correctly).
    """


class ProviderUserState(object):
    """Object representing the provider state (attached or not) for a user.

    This is intended only for use when rendering templates. See for example
    lms/templates/dashboard.html.
    """

176
    def __init__(self, enabled_provider, user, association):
177
        # Boolean. Whether the user has an account associated with the provider
178 179 180 181 182 183 184 185 186
        self.has_account = association is not None
        if self.has_account:
            # UserSocialAuth row ID
            self.association_id = association.id
            # Identifier of this user according to the remote provider:
            self.remote_id = enabled_provider.get_remote_id_from_social_auth(association)
        else:
            self.association_id = None
            self.remote_id = None
187 188 189 190 191 192 193 194
        # provider.BaseProvider child. Callers must verify that the provider is
        # enabled.
        self.provider = enabled_provider
        # django.contrib.auth.models.User.
        self.user = user

    def get_unlink_form_name(self):
        """Gets the name used in HTML forms that unlink a provider account."""
195
        return self.provider.provider_id + '_unlink_form'
196 197 198 199 200 201 202


def get(request):
    """Gets the running pipeline from the passed request."""
    return request.session.get('partial_pipeline')


203
def get_authenticated_user(auth_provider, username, uid):
204 205 206 207 208 209 210 211
    """Gets a saved user authenticated by a particular backend.

    Between pipeline steps User objects are not saved. We need to reconstitute
    the user and set its .backend, which is ordinarily monkey-patched on by
    Django during authenticate(), so it will function like a user returned by
    authenticate().

    Args:
212
        auth_provider: the third_party_auth provider in use for the current pipeline.
213
        username: string. Username of user to get.
214
        uid: string. The user ID according to the third party.
215 216 217

    Returns:
        User if user is found and has a social auth from the passed
218
        provider.
219 220 221 222 223 224

    Raises:
        User.DoesNotExist: if no user matching user is found, or the matching
        user has no social auth associated with the given backend.
        AssertionError: if the user is not authenticated.
    """
225
    match = models.DjangoStorage.user.get_social_auth(provider=auth_provider.backend_name, uid=uid)
226

227
    if not match or match.user.username != username:
228 229
        raise User.DoesNotExist

230 231
    user = match.user
    user.backend = auth_provider.get_authentication_backend()
232 233 234
    return user


235 236 237
def _get_enabled_provider(provider_id):
    """Gets an enabled provider by its provider_id member or throws."""
    enabled_provider = provider.Registry.get(provider_id)
238 239

    if not enabled_provider:
240
        raise ValueError('Provider %s not enabled' % provider_id)
241 242 243 244

    return enabled_provider


245 246
def _get_url(view_name, backend_name, auth_entry=None, redirect_url=None,
             extra_params=None, url_params=None):
247
    """Creates a URL to hook into social auth endpoints."""
248 249 250
    url_params = url_params or {}
    url_params['backend'] = backend_name
    url = reverse(view_name, kwargs=url_params)
251

252
    query_params = OrderedDict()
253
    if auth_entry:
254 255 256 257
        query_params[AUTH_ENTRY_KEY] = auth_entry

    if redirect_url:
        query_params[AUTH_REDIRECT_KEY] = redirect_url
258

259 260 261
    if extra_params:
        query_params.update(extra_params)

262 263 264 265
    return u"{url}?{params}".format(
        url=url,
        params=urllib.urlencode(query_params)
    )
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280


def get_complete_url(backend_name):
    """Gets URL for the endpoint that returns control to the auth pipeline.

    Args:
        backend_name: string. Name of the python-social-auth backend from the
            currently-running pipeline.

    Returns:
        String. URL that finishes the auth pipeline for a provider.

    Raises:
        ValueError: if no provider is enabled with the given backend_name.
    """
281
    if not any(provider.Registry.get_enabled_by_backend_name(backend_name)):
282 283 284 285 286
        raise ValueError('Provider with backend %s not enabled' % backend_name)

    return _get_url('social:complete', backend_name)


287
def get_disconnect_url(provider_id, association_id):
288 289 290
    """Gets URL for the endpoint that starts the disconnect pipeline.

    Args:
291
        provider_id: string identifier of the models.ProviderConfig child you want
292
            to disconnect from.
293 294
        association_id: int. Optional ID of a specific row in the UserSocialAuth
            table to disconnect (useful if multiple providers use a common backend)
295 296 297 298 299

    Returns:
        String. URL that starts the disconnection pipeline.

    Raises:
300
        ValueError: if no provider is enabled with the given ID.
301
    """
302
    backend_name = _get_enabled_provider(provider_id).backend_name
303 304 305 306
    if association_id:
        return _get_url('social:disconnect_individual', backend_name, url_params={'association_id': association_id})
    else:
        return _get_url('social:disconnect', backend_name)
307 308


309
def get_login_url(provider_id, auth_entry, redirect_url=None):
310 311 312
    """Gets the login URL for the endpoint that kicks off auth with a provider.

    Args:
313 314
        provider_id: string identifier of the models.ProviderConfig child you want
            to disconnect from.
315 316 317 318
        auth_entry: string. Query argument specifying the desired entry point
            for the auth pipeline. Used by the pipeline for later branching.
            Must be one of _AUTH_ENTRY_CHOICES.

319 320 321 322
    Keyword Args:
        redirect_url (string): If provided, redirect to this URL at the end
            of the authentication process.

323 324 325 326
    Returns:
        String. URL that starts the auth pipeline for a provider.

    Raises:
327
        ValueError: if no provider is enabled with the given provider_id.
328 329
    """
    assert auth_entry in _AUTH_ENTRY_CHOICES
330
    enabled_provider = _get_enabled_provider(provider_id)
331 332
    return _get_url(
        'social:begin',
333
        enabled_provider.backend_name,
334 335
        auth_entry=auth_entry,
        redirect_url=redirect_url,
336
        extra_params=enabled_provider.get_url_params(),
337
    )
338 339 340 341 342 343 344 345 346 347


def get_duplicate_provider(messages):
    """Gets provider from message about social account already in use.

    python-social-auth's exception middleware uses the messages module to
    record details about duplicate account associations. It records exactly one
    message there is a request to associate a social account S with an edX
    account E if S is already associated with an edX account E'.

348 349 350
    This messaging approach is stringly-typed and the particular string is
    unfortunately not in a reusable constant.

351
    Returns:
352
        string name of the python-social-auth backend that has the duplicate
353 354
        account, or None if there is no duplicate (and hence no error).
    """
355
    social_auth_messages = [m for m in messages if m.message.endswith('is already in use.')]
356 357 358 359 360

    if not social_auth_messages:
        return

    assert len(social_auth_messages) == 1
361 362
    backend_name = social_auth_messages[0].extra_tags.split()[1]
    return backend_name
363 364 365 366 367 368 369 370 371 372 373 374 375


def get_provider_user_states(user):
    """Gets list of states of provider-user combinations.

    Args:
        django.contrib.auth.User. The user to get states for.

    Returns:
        List of ProviderUserState. The list of states of a user's account with
            each enabled provider.
    """
    states = []
376
    found_user_auths = list(models.DjangoStorage.user.get_social_auth_for_user(user))
377 378

    for enabled_provider in provider.Registry.enabled():
379
        association = None
380 381
        for auth in found_user_auths:
            if enabled_provider.match_social_auth(auth):
382
                association = auth
383
                break
384
        if enabled_provider.accepts_logins or association:
385
            states.append(
386
                ProviderUserState(enabled_provider, user, association)
387
            )
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

    return states


def make_random_password(length=None, choice_fn=random.SystemRandom().choice):
    """Makes a random password.

    When a user creates an account via a social provider, we need to create a
    placeholder password for them to satisfy the ORM's consistency and
    validation requirements. Users don't know (and hence cannot sign in with)
    this password; that's OK because they can always use the reset password
    flow to set it to a known value.

    Args:
        choice_fn: function or method. Takes an iterable and returns a random
            element.
        length: int. Number of chars in the returned value. None to use default.

    Returns:
        String. The resulting password.
    """
    length = length if length is not None else _DEFAULT_RANDOM_PASSWORD_LENGTH
    return ''.join(choice_fn(_PASSWORD_CHARSET) for _ in xrange(length))


def running(request):
    """Returns True iff request is running a third-party auth pipeline."""
    return request.session.get('partial_pipeline') is not None  # Avoid False for {}.


# Pipeline functions.
# Signatures are set by python-social-auth; prepending 'unused_' causes
# TypeError on dispatch to the auth backend's authenticate().
421
# pylint: disable=unused-argument
422 423 424 425 426 427


def parse_query_params(strategy, response, *args, **kwargs):
    """Reads whitelisted query params, transforms them into pipeline args."""
    auth_entry = strategy.session.get(AUTH_ENTRY_KEY)
    if not (auth_entry and auth_entry in _AUTH_ENTRY_CHOICES):
428
        raise AuthEntryError(strategy.request.backend, 'auth_entry missing or invalid')
429

430
    return {'auth_entry': auth_entry}
431

432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
def set_pipeline_timeout(strategy, user, *args, **kwargs):
    """
    Set a short session timeout while the pipeline runs, to improve security.

    Consider the following attack:
    1. Attacker on a public computer visits edX and initiates the third-party login flow
    2. Attacker logs into their own third-party account
    3. Attacker closes the window and does not complete the login flow
    4. Victim on the same computer logs into edX with username/password
    5. edX links attacker's third-party account with victim's edX account
    6. Attacker logs into victim's edX account using attacker's own third-party account

    We have two features of the pipeline designed to prevent this attack:
    * This method shortens the Django session timeout during the pipeline. This should mean that
      if there is a reasonable delay between steps 3 and 4, the session and pipeline will be
      reset, and the attack foiled.
      Configure the timeout with the SOCIAL_AUTH_PIPELINE_TIMEOUT setting (Default: 600 seconds)
    * On step 4, the login page displays an obvious message to the user, saying "You've
      successfully signed into (Google), but your (Google) account isn't linked with an edX
      account. To link your accounts, login now using your edX password.".
    """
    if strategy.request and not user:  # If user is set, we're currently logged in (and/or linked) so it doesn't matter.
        strategy.request.session.set_expiry(strategy.setting('PIPELINE_TIMEOUT', 600))
        # We don't need to reset this timeout later. Because the user is not logged in and this
        # account is not yet linked to an edX account, either the normal 'login' or 'register'
        # code must occur during the subsequent ensure_user_information step, and those methods
        # will change the session timeout to the "normal" value according to the "Remember Me"
        # choice of the user.


463
def redirect_to_custom_form(request, auth_entry, kwargs):
464 465 466 467 468 469 470
    """
    If auth_entry is found in AUTH_ENTRY_CUSTOM, this is used to send provider
    data to an external server's registration/login page.

    The data is sent as a base64-encoded values in a POST request and includes
    a cryptographic checksum in case the integrity of the data is important.
    """
471 472
    backend_name = request.backend.name
    provider_id = provider.Registry.get_from_pipeline({'backend': backend_name, 'kwargs': kwargs}).provider_id
473 474 475 476 477 478
    form_info = AUTH_ENTRY_CUSTOM[auth_entry]
    secret_key = form_info['secret_key']
    if isinstance(secret_key, unicode):
        secret_key = secret_key.encode('utf-8')
    custom_form_url = form_info['url']
    data_str = json.dumps({
479 480 481 482
        "auth_entry": auth_entry,
        "backend_name": backend_name,
        "provider_id": provider_id,
        "user_details": kwargs['details'],
483 484 485 486 487 488 489 490 491 492 493 494
    })
    digest = hmac.new(secret_key, msg=data_str, digestmod=hashlib.sha256).digest()
    # Store the data in the session temporarily, then redirect to a page that will POST it to
    # the custom login/register page.
    request.session['tpa_custom_auth_entry_data'] = {
        'data': base64.b64encode(data_str),
        'hmac': base64.b64encode(digest),
        'post_url': custom_form_url,
    }
    return redirect(reverse('tpa_post_to_custom_auth_form'))


495
@partial.partial
496 497
def ensure_user_information(strategy, auth_entry, backend=None, user=None, social=None,
                            allow_inactive_user=False, *args, **kwargs):
498 499 500 501
    """
    Ensure that we have the necessary information about a user (either an
    existing account or registration data) to proceed with the pipeline.
    """
502 503

    # We're deliberately verbose here to make it clear what the intended
504
    # dispatch behavior is for the various pipeline entry points, given the
505 506 507 508 509 510 511 512 513
    # current state of the pipeline. Keep in mind the pipeline is re-entrant
    # and values will change on repeated invocations (for example, the first
    # time through the login flow the user will be None so we dispatch to the
    # login form; the second time it will have a value so we continue to the
    # next pipeline step directly).
    #
    # It is important that we always execute the entire pipeline. Even if
    # behavior appears correct without executing a step, it means important
    # invariants have been violated and future misbehavior is likely.
514 515
    def dispatch_to_login():
        """Redirects to the login page."""
516
        return redirect(AUTH_DISPATCH_URLS[AUTH_ENTRY_LOGIN])
517

518 519
    def dispatch_to_register():
        """Redirects to the registration page."""
520
        return redirect(AUTH_DISPATCH_URLS[AUTH_ENTRY_REGISTER])
John Cox committed
521

522 523 524 525 526
    def should_force_account_creation():
        """ For some third party providers, we auto-create user accounts """
        current_provider = provider.Registry.get_from_pipeline({'backend': backend.name, 'kwargs': kwargs})
        return current_provider and current_provider.skip_email_verification

527 528
    if not user:
        if auth_entry in [AUTH_ENTRY_LOGIN_API, AUTH_ENTRY_REGISTER_API]:
529
            return HttpResponseBadRequest()
530
        elif auth_entry == AUTH_ENTRY_LOGIN:
531 532
            # User has authenticated with the third party provider but we don't know which edX
            # account corresponds to them yet, if any.
533 534
            if should_force_account_creation():
                return dispatch_to_register()
535
            return dispatch_to_login()
536
        elif auth_entry == AUTH_ENTRY_REGISTER:
537 538
            # User has authenticated with the third party provider and now wants to finish
            # creating their edX account.
539
            return dispatch_to_register()
540 541
        elif auth_entry == AUTH_ENTRY_ACCOUNT_SETTINGS:
            raise AuthEntryError(backend, 'auth_entry is wrong. Settings requires a user.')
542 543
        elif auth_entry in AUTH_ENTRY_CUSTOM:
            # Pass the username, email, etc. via query params to the custom entry page:
544
            return redirect_to_custom_form(strategy.request, auth_entry, kwargs)
545 546 547 548 549 550 551 552 553
        else:
            raise AuthEntryError(backend, 'auth_entry invalid')

    if not user.is_active:
        # The user account has not been verified yet.
        if allow_inactive_user:
            # This parameter is used by the auth_exchange app, which always allows users to
            # login, whether or not their account is validated.
            pass
554 555 556 557 558 559 560 561 562 563
        elif social is None:
            # The user has just registered a new account as part of this pipeline. Their account
            # is inactive but we allow the login to continue, because if we pause again to force
            # the user to activate their account via email, the pipeline may get lost (e.g.
            # email takes too long to arrive, user opens the activation email on a different
            # device, etc.). This is consistent with first party auth and ensures that the
            # pipeline completes fully, which is critical.
            pass
        else:
            # This is an existing account, linked to a third party provider but not activated.
564 565 566
            # Double-check these criteria:
            assert user is not None
            assert social is not None
567 568 569 570 571 572 573 574
            # We now also allow them to login again, because if they had entered their email
            # incorrectly then there would be no way for them to recover the account, nor
            # register anew via SSO. See SOL-1324 in JIRA.
            # However, we will log a warning for this case:
            logger.warning(
                'User "%s" is using third_party_auth to login but has not yet activated their account. ',
                user.username
            )
575

576

Julia Hansbrough committed
577
@partial.partial
Will Daly committed
578
def set_logged_in_cookies(backend=None, user=None, strategy=None, auth_entry=None, *args, **kwargs):
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    """This pipeline step sets the "logged in" cookie for authenticated users.

    Some installations have a marketing site front-end separate from
    edx-platform.  Those installations sometimes display different
    information for logged in versus anonymous users (e.g. a link
    to the student dashboard instead of the login page.)

    Since social auth uses Django's native `login()` method, it bypasses
    our usual login view that sets this cookie.  For this reason, we need
    to set the cookie ourselves within the pipeline.

    The procedure for doing this is a little strange.  On the one hand,
    we need to send a response to the user in order to set the cookie.
    On the other hand, we don't want to drop the user out of the pipeline.

    For this reason, we send a redirect back to the "complete" URL,
    so users immediately re-enter the pipeline.  The redirect response
    contains a header that sets the logged in cookie.

    If the user is not logged in, or the logged in cookie is already set,
    the function returns `None`, indicating that control should pass
    to the next pipeline step.

    """
603
    if not is_api(auth_entry) and user is not None and user.is_authenticated():
604
        request = strategy.request if strategy else None
605
        # n.b. for new users, user.is_active may be False at this point; set the cookie anyways.
606 607 608 609
        if request is not None:
            # Check that the cookie isn't already set.
            # This ensures that we allow the user to continue to the next
            # pipeline step once he/she has the cookie set by this step.
Will Daly committed
610
            has_cookie = student.cookies.is_logged_in_cookie_set(request)
611 612 613 614 615 616 617 618 619 620
            if not has_cookie:
                try:
                    redirect_url = get_complete_url(backend.name)
                except ValueError:
                    # If for some reason we can't get the URL, just skip this step
                    # This may be overly paranoid, but it's far more important that
                    # the user log in successfully than that the cookie is set.
                    pass
                else:
                    response = redirect(redirect_url)
Will Daly committed
621
                    return student.cookies.set_logged_in_cookies(request, response, user)
622 623


Julia Hansbrough committed
624
@partial.partial
625
def login_analytics(strategy, auth_entry, *args, **kwargs):
626
    """ Sends login info to Segment """
Julia Hansbrough committed
627

628
    event_name = None
629
    if auth_entry == AUTH_ENTRY_LOGIN:
630
        event_name = 'edx.bi.user.account.authenticated'
631
    elif auth_entry in [AUTH_ENTRY_ACCOUNT_SETTINGS]:
632
        event_name = 'edx.bi.user.account.linked'
Julia Hansbrough committed
633

634
    if event_name is not None and hasattr(settings, 'LMS_SEGMENT_KEY') and settings.LMS_SEGMENT_KEY:
Julia Hansbrough committed
635 636 637 638 639 640
        tracking_context = tracker.get_tracker().resolve_context()
        analytics.track(
            kwargs['user'].id,
            event_name,
            {
                'category': "conversion",
641
                'label': None,
642
                'provider': kwargs['backend'].name
Julia Hansbrough committed
643 644
            },
            context={
645
                'ip': tracking_context.get('ip'),
Julia Hansbrough committed
646
                'Google Analytics': {
647
                    'clientId': tracking_context.get('client_id')
Julia Hansbrough committed
648 649 650
                }
            }
        )
Julia Hansbrough committed
651

652

653
@partial.partial
654
def associate_by_email_if_login_api(auth_entry, backend, details, user, *args, **kwargs):
655 656 657 658 659 660 661 662
    """
    This pipeline step associates the current social auth with the user with the
    same email address in the database.  It defers to the social library's associate_by_email
    implementation, which verifies that only a single database user is associated with the email.

    This association is done ONLY if the user entered the pipeline through a LOGIN API.
    """
    if auth_entry == AUTH_ENTRY_LOGIN_API:
663
        association_response = associate_by_email(backend, details, user, *args, **kwargs)
664 665 666 667 668 669 670 671 672 673
        if (
            association_response and
            association_response.get('user') and
            association_response['user'].is_active
        ):
            # Only return the user matched by email if their email has been activated.
            # Otherwise, an illegitimate user can create an account with another user's
            # email address and the legitimate user would now login to the illegitimate
            # account.
            return association_response