Skip to content

Commit

Permalink
add manual redirect_uri
Browse files Browse the repository at this point in the history
  • Loading branch information
st4lk committed May 27, 2015
1 parent ec64d00 commit f5213c2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ Quick start

User model is taken from [`settings.AUTH_USER_MODEL`](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model).

At input there is also non-required field `redirect_uri`.
If given, server will use this redirect uri in requests, instead of uri
got from settings.
This redirect_uri must be equal in front-end request and in back-end request.
Back-end will not do any redirect in fact.


OAuth 2.0 workflow with rest-social-auth
-----------------------------------------
Expand Down Expand Up @@ -224,6 +230,9 @@ Settings

REST_SOCIAL_OAUTH_ABSOLUTE_REDIRECT_URI = 'http://myproject.com/'

This settings has higher priority than `REST_SOCIAL_OAUTH_REDIRECT_URI`.
Also, `redirect_uri` from request has higher priority than any setting.



Customization
Expand Down
1 change: 1 addition & 0 deletions rest_social_auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class SocialAuthInputSerializer(serializers.Serializer):
provider = serializers.CharField()
code = serializers.CharField()
redirect_uri = serializers.URLField(required=False)


class UserSerializer(serializers.ModelSerializer):
Expand Down
18 changes: 13 additions & 5 deletions rest_social_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ def load_strategy(request=None):
@psa(REDIRECT_URI, load_strategy=load_strategy)
def register_by_auth_token(request, backend, *args, **kwargs):
user = request.user
absolute_redirect_uri = getattr(settings,
'REST_SOCIAL_OAUTH_ABSOLUTE_REDIRECT_URI', None)
if absolute_redirect_uri:
request.backend.redirect_uri = absolute_redirect_uri
redirect_uri = kwargs.pop('manual_redirect_uri', None)
if redirect_uri:
request.backend.redirect_uri = redirect_uri
is_authenticated = user_is_authenticated(user)
user = is_authenticated and user or None
# skip checking state by setting following params to False
Expand Down Expand Up @@ -73,8 +72,11 @@ def post(self, request, *args, **kwargs):
serializer_in.is_valid(raise_exception=True)
self.set_input_data(request, serializer_in.validated_data.copy())
provider = request.auth_data.pop('provider')
manual_redirect_uri = request.auth_data.pop('redirect_uri', None)
manual_redirect_uri = self.get_redirect_uri(manual_redirect_uri)
try:
user = register_by_auth_token(request, provider)
user = register_by_auth_token(request, provider,
manual_redirect_uri=manual_redirect_uri)
except AuthException as e:
return self.respond_error(e, provider)
resp_data = self.get_serializer_class_out()(instance=user)
Expand All @@ -94,6 +96,12 @@ def set_input_data(self, request, auth_data):
"""
request.auth_data = auth_data

def get_redirect_uri(self, manual_redirect_uri):
if not manual_redirect_uri:
manual_redirect_uri = getattr(settings,
'REST_SOCIAL_OAUTH_ABSOLUTE_REDIRECT_URI', None)
return manual_redirect_uri

def respond_error(self, error, provider):
return Response(status=status.HTTP_400_BAD_REQUEST)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_social.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def setUp(self):

def tearDown(self):
HTTPretty.disable()
HTTPretty.reset()
self.backend = None
self.strategy = None
self.name = None
Expand Down Expand Up @@ -96,6 +97,15 @@ def test_login_absolute_redirect(self):
url_params = dict(parse_qsl(urlparse(HTTPretty.latest_requests[0].path).query))
self.assertEqual('http://myproject.com/', url_params['redirect_uri'])

@override_settings(REST_SOCIAL_OAUTH_ABSOLUTE_REDIRECT_URI='http://myproject.com/')
def test_login_manual_redirect(self):
resp = self.client.post(reverse('login_social_session'),
data={'provider': 'facebook', 'code': '3D52VoM1uiw94a1ETnGvYlCw',
'redirect_uri': 'http://manualdomain.com/'})
self.assertEqual(resp.status_code, 200)
url_params = dict(parse_qsl(urlparse(HTTPretty.latest_requests[0].path).query))
self.assertEqual('http://manualdomain.com/', url_params['redirect_uri'])


class TestSocialAuthError(APITestCase, BaseFacebookAPITestCase):
access_token_status = 400
Expand Down

0 comments on commit f5213c2

Please sign in to comment.