Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #224 from mozilla-services/189
Browse files Browse the repository at this point in the history
Add FXA Login Redirect #189
  • Loading branch information
jaredlockhart committed Jan 6, 2016
2 parents 53c41d7 + 719f101 commit 175f0ef
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
22 changes: 22 additions & 0 deletions leaderboard/fxa/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@
from leaderboard.fxa.tests.test_client import MockRequestTestMixin


class TestFXALoginView(TestCase):

def test_login_view_redirects_to_fxa_url(self):
test_settings = {
'FXA_CLIENT_ID': 'fxa_client_id',
'FXA_SCOPES': 'leaderboard,profile',
'FXA_OAUTH_URI': 'http://example.com/v1/oauth/',
'FXA_PROFILE_URI': 'http://example.com/v1/profile/',
}

with self.settings(**test_settings):
response = self.client.get(reverse('fxa-login'))

self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, (
'http://example.com/v1/oauth/authorization?action='
'signin&scope=leaderboard%2Cprofile&state=99&redirect_uri='
'http%3A%2F%2Ftestserver%2Fapi%2Fv1%2Ffxa%2Fredirect%2F'
'&client_id=fxa_client_id'
))


class TestFXAConfigView(TestCase):

def test_config_view_returns_fxa_settings(self):
Expand Down
4 changes: 3 additions & 1 deletion leaderboard/fxa/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.conf.urls import url

from leaderboard.fxa.views import FXAConfigView, FXARedirectView
from leaderboard.fxa.views import FXALoginView, FXAConfigView, FXARedirectView

urlpatterns = [
url('^login/', FXALoginView.as_view(),
name='fxa-login'),
url('^config/', FXAConfigView.as_view(),
name='fxa-config'),
url('^redirect/', FXARedirectView.as_view(),
Expand Down
26 changes: 26 additions & 0 deletions leaderboard/fxa/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import urlparse
import urllib
from uuid import uuid4

from django.views.generic.base import View
from django.core.urlresolvers import reverse
from django.conf import settings
from django.shortcuts import redirect
from rest_framework.exceptions import ValidationError
from rest_framework.views import APIView
from rest_framework.response import Response
Expand All @@ -10,6 +14,28 @@
from leaderboard.fxa.client import FXAClientMixin, FXAException


class FXALoginView(View):

def get(self, request):
login_params = {
'action': 'signin',
'client_id': settings.FXA_CLIENT_ID,
'scope': settings.FXA_SCOPE,
'state': 99,
'redirect_uri': request.build_absolute_uri(
reverse('fxa-redirect')),
}

login_url = '{url}?{query}'.format(
url=urlparse.urljoin(
settings.FXA_OAUTH_URI,
'authorization',
),
query=urllib.urlencode(login_params),
)
return redirect(login_url)


class FXAConfigView(APIView):

def get(self, request):
Expand Down
5 changes: 5 additions & 0 deletions leaderboard/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
# observations within that tile for a 24 hour period
CONTRIBUTION_RECORD_DURATION = 24 * 60 * 60

# FXA Shared Settings
FXA_SCOPE = 'leaderboard,profile'

# Travis settings
if 'TRAVIS' in os.environ:
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'travis'
Expand All @@ -158,6 +162,7 @@
FXA_OAUTH_URI = 'travis'
FXA_PROFILE_URI = 'travis'

# Docker build settings
if 'DOCKER_BUILD' in os.environ:
SECRET_KEY = 'docker'

Expand Down

0 comments on commit 175f0ef

Please sign in to comment.