Example #1
0
 def test_get_fxa_oauth_token_success(self, gfc_mock):
     oauth_mock = Mock()
     gfc_mock.return_value = oauth_mock, None
     trade_code = oauth_mock.trade_code
     trade_code.return_value = {'access_token': 'goodtoken'}
     assert get_fxa_oauth_token('abc123') == 'goodtoken'
     trade_code.assert_called_with('abc123', client_id='12345', client_secret='67890')
Example #2
0
 def test_get_fxa_oauth_token_error(self, gfc_mock):
     oauth_mock = Mock()
     gfc_mock.return_value = oauth_mock, None
     trade_code = oauth_mock.trade_code
     trade_code.side_effect = fxa.errors.ClientError()
     assert not get_fxa_oauth_token('abc123')
     trade_code.assert_called_with('abc123', client_id='12345', client_secret='67890')
Example #3
0
def oauth_fxa(request):
    """
    Acts as an OAuth relier for Firefox Accounts. Currently specifically tuned to handle
    the OAuth flow for the Firefox Concert Series (Q4 2018).

    If additional OAuth flows are required in the future, please refactor this method.
    """
    if not switch('firefox_concert_series'):
        return HttpResponseRedirect(reverse('mozorg.home'))

    # expected state should be in user's cookies
    stateExpected = request.COOKIES.get('fxaOauthState', None)

    # provided state passed back from FxA - these state values should match
    stateProvided = request.GET.get('state', None)

    # code must be present - is in redirect querystring from FxA
    code = request.GET.get('code', None)

    error = False
    cookie_age = 86400  # 1 day

    # ensure all the data we need is present and valid
    if not (stateExpected and stateProvided and code):
        error = True
    elif stateExpected != stateProvided:
        error = True
    else:
        token = get_fxa_oauth_token(code)

        if not token:
            error = True
        else:
            email = get_fxa_profile_email(token)

            if not email:
                error = True
            else:
                # add email to mailing list

                # check for Firefox
                include_re = re.compile(r'\bFirefox\b', flags=re.I)
                exclude_re = re.compile(r'\b(Camino|Iceweasel|SeaMonkey)\b', flags=re.I)

                value = request.META.get('HTTP_USER_AGENT', '')
                isFx = bool(include_re.search(value) and not exclude_re.search(value))

                # add user to mailing list for future concert updates
                rsvp_ok = fxa_concert_rsvp(email, isFx)

                if not rsvp_ok:
                    error = True

    if error:
        # send user to a custom error page
        response = HttpResponseRedirect(reverse('mozorg.oauth.fxa-error'))
    else:
        # send user back to the concerts page
        response = HttpResponseRedirect(reverse('firefox.concerts'))
        response.set_cookie('fxaOauthVerified', True, max_age=cookie_age, httponly=False)

    return response
Example #4
0
def oauth_fxa(request):
    """
    Acts as an OAuth relier for Firefox Accounts. Currently specifically tuned to handle
    the OAuth flow for the Firefox Concert Series (Q4 2018).

    If additional OAuth flows are required in the future, please refactor this method.
    """
    if not switch('firefox_concert_series'):
        return HttpResponseRedirect(reverse('mozorg.home'))

    # expected state should be in user's cookies
    stateExpected = request.COOKIES.get('fxaOauthState', None)

    # provided state passed back from FxA - these state values should match
    stateProvided = request.GET.get('state', None)

    # code must be present - is in redirect querystring from FxA
    code = request.GET.get('code', None)

    error = False
    cookie_age = 86400  # 1 day

    # ensure all the data we need is present and valid
    if not (stateExpected and stateProvided and code):
        error = True
    elif stateExpected != stateProvided:
        error = True
    else:
        token = get_fxa_oauth_token(code)

        if not token:
            error = True
        else:
            email = get_fxa_profile_email(token)

            if not email:
                error = True
            else:
                # add email to mailing list

                # check for Firefox
                include_re = re.compile(r'\bFirefox\b', flags=re.I)
                exclude_re = re.compile(r'\b(Camino|Iceweasel|SeaMonkey)\b', flags=re.I)

                value = request.META.get('HTTP_USER_AGENT', '')
                isFx = bool(include_re.search(value) and not exclude_re.search(value))

                # add user to mailing list for future concert updates
                rsvp_ok = fxa_concert_rsvp(email, isFx)

                if not rsvp_ok:
                    error = True

    if error:
        # send user to a custom error page
        response = HttpResponseRedirect(reverse('mozorg.oauth.fxa-error'))
    else:
        # send user back to the concerts page
        response = HttpResponseRedirect(reverse('firefox.concerts'))
        response.set_cookie('fxaOauthVerified', True, max_age=cookie_age, httponly=False)

    return response