【发布时间】:2021-02-06 07:28:52
【问题描述】:
我正在编写一个自定义身份验证中间件,用于检查标头中“授权”键的传入请求,其中包含一个令牌。
我正在使用此令牌与第三方 (Microsoft Graph) 检查用户的有效性。 MS Graph 将响应如下对象
# the response object
{
'@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#users/$entity',
'businessPhones': ['xxx'],
'displayName': 'xxx',
'givenName': 'xxx',
'id': 'xxx',
'jobTitle': None,
'mail': 'xxx',
'mobilePhone': None,
'officeLocation': None,
'preferredLanguage': 'xxx',
'surname': 'xxx',
'userPrincipalName': 'xxx'
}
编辑:在此处添加自定义中间件代码:
class AuthenticationMiddleware(MiddlewareMixin):
if not request.user.is_authenticated:
if "Authorization" in request.headers:
# Make a request to MS Graph with the given token
# to get user details and append to request
token = request.headers["Authorization"]
elif "accessToken" in request.GET:
token = request.GET["accessToken"]
else:
token = None
if token:
url = "https://graph.microsoft.com/v1.0/me/"
payload = {}
headers = {"Authorization": "Bearer {}".format(token)}
response = requests.request("GET", url, headers=headers, data=payload)
if response.ok:
request.custom_user = response.json()
else:
request.custom_user = AnonymousUser
else:
request.custom_user = AnonymousUser
现在我想将其设计为像 Django 的默认身份验证后端一样工作,并具有适当的组和权限。如何使用 LazyObject 来检查用户的组成员资格和权限?
更新
看起来还有一个像这样工作的自定义后端身份验证。
它和我对中间件做的事情一样吗?
from django.contrib.auth.backends import BaseBackend
class MyBackend(BaseBackend):
def authenticate(self, request, token=None):
# Check the token and return a user.
...
【问题讨论】:
-
您在中间件中缺少一些代码,例如所有这些 if 语句所属的方法定义。此外,新代码不应使用
MiddlewareMixin,而应符合 1.10+ 中间件 API。最终,兼容性 mixin 将会消失(我相信 4.0,但不确定)。
标签: django django-authentication