2

我正在尝试在我的单页角度应用程序中启用身份验证,在前端使用Satellizer作为客户端,在后端使用django rest 框架使用python social auth。主要问题是我想使用JWT而不是会话身份验证。

我尝试将用户确认弹出窗口后得到的响应传递给social/complete/backend(python 社交身份验证视图),但没有运气。

在谷歌配置的卫星器中,我放了:

        $authProvider.google({
            clientId:'609163425136-1i7b7jlr4j4hlqtnb1gk3al2kagavcjm.apps.googleusercontent.com',
            url: 'social/complete/google-oauth2/',
            optionalUrlParams: ['display', 'state'],
            state: function() {
                return Math.random();
            }
        });

我遇到的问题是在 python social auth 中:

缺少所需的参数状态

对于google-oauth2

缺少所需的参数代码

对于脸书

这对我来说很奇怪,因为这些参数存在于请求有效负载中,然后我可以在我自己的自定义视图中获取。


我最接近解决方案的方法是编写自己的视图,在该视图中我可以state正常接受参数。

这是我处理响应并使用django-rest-framework-jwt创建新 jwt 令牌的视图:

class SocialAuthView(APIView):
    throttle_classes = ()
    permission_classes = ()
    authentication_classes = ()

    social_serializer = SocialAuthSerializer
    user_serializer = None

    def post(self, request):
        access_token_url = 'https://accounts.google.com/o/oauth2/token'
        people_api_url = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect'

        from django.conf import settings
        payload = dict(client_id=request.data['clientId'],
                       redirect_uri=request.data['redirectUri'],
                       client_secret=settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET,
                       code=request.data['code'],
                       grant_type='authorization_code')

        # Step 1. Exchange authorization code for access token.
        import requests
        import json
        r = requests.post(access_token_url, data=payload)
        token = json.loads(r.text)
        headers = {'Authorization': 'Bearer {0}'.format(token['access_token'])}

        # Step 2. Retrieve information about the current user.
        r = requests.get(people_api_url, headers=headers)
        profile = json.loads(r.text)

        try:
            user = User.objects.filter(email=profile['email'])[0]
        except IndexError:
            pass
        import jwt
        from rest_framework_jwt.utils import jwt_payload_handler, jwt_encode_handler
        if user:
            payload = jwt_payload_handler(user)
            token = jwt_encode_handler(payload)
            return Response({'token': token.decode('unicode_escape')},
                            status=status.HTTP_200_OK)
        u = User.objects.create(username=profile['name'], first_name=profile['given_name'],
                                last_name=profile['family_name'], email=profile['email'])
        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)
        return Response({'Bearer': token.decode('unicode_escape')},
                        status=status.HTTP_200_OK)

我的意思是,它可以工作,但如果我可以使用 python social auth 实现它,我并没有得到所有的好处。也许有一种方法可以通过 python social auth从这个视图调用一些函数,如do_auth进行身份验证?

令我惊讶的是,我在这个问题上的挣扎。

欢迎所有帮助。

tl,dr:如何使用Satellizerdjango-rest-frameworkpython social authJWT实现角度单页应用程序?

4

1 回答 1

4

不是一个完整的答案,但我没有足够的声誉发表评论。我一直在尝试做类似的事情。Satellizer 与 DRF 的 djoser 模块很好地配合使用,用于电子邮件/传递令牌身份验证。至于社交方面,我发现 OAuth2 相当复杂。您可能想查看以下项目(与我无关),它似乎正在尝试实现这一点(angularjs-satellizer/psa/jwt):

https://github.com/hsr-ba-fs15-dat/opendatahub

具体来说: https ://github.com/hsr-ba-fs15-dat/opendatahub/blob/master/src/main/python/authentication/views.py

于 2015-08-06T12:26:06.620 回答