gpt4 book ai didi

python - 使用 python social auth 和 Satellizer 进行休息身份验证

转载 作者:行者123 更新时间:2023-11-28 22:42:22 24 4
gpt4 key购买 nike

我正在尝试使用 Satellizer 在我的单页 Angular 应用程序中启用身份验证作为前端客户端和python social auth在后端使用 django rest framework .主要问题是我想使用 JWT 而不是 session 身份验证。

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

在用于 google 配置的 satellizer 中,我放置了:

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

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

Missing needed parameter state

对于 google-oauth2

Missing needed parameter code

Facebook

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


我最接近的解决方案是编写我自己的 View ,在其中我可以正常接受参数 state

这是我的 View ,它处理响应并使用 django-rest-framework-jwt 创建一个新的 jwt token :

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 社交身份验证实现它会得到的所有好处。也许有一种方法可以通过 python 社交身份验证从这个 View 调用一些函数,比如 do_auth

令我吃惊的是,我为解决这个问题付出了多少努力。

欢迎所有帮助。

tl,dr:如何使用Satellizerdjango-rest-framework实现angular单页应用、python 社交身份验证JWT

最佳答案

不是一个完整的答案,但我没有足够的声誉来发表评论。我一直在尝试做类似的事情。 Satellizer 与 DRF 的 djoser 模块配合良好,用于电子邮件/通行 token 验证。至于社交方面,我发现 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

关于python - 使用 python social auth 和 Satellizer 进行休息身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31833084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com