[英]django-oauth-toolkit to issue a JWT token
Tech Stack Django1.10.8 + Python3.6 + docker + React + Axios.js 技术堆栈Django1.10.8 + Python3.6 + docker + React + Axios.js
I have a situation where I need to make a server to server call and for this I am using Django-OAuth-toolkit. 我遇到一种情况,需要建立服务器到服务器的调用,为此,我正在使用Django-OAuth-toolkit。 How can I convert this token to issue JWT token instead?
如何转换此令牌以发行JWT令牌?
{ "access_token": "txxxxxxxxxxxxxxxxxxxxxFB45a", "expires_in": 36000, "token_type": "Bearer", "scope": "read write groups", "refresh_token": "16oKxxxxxxxxxxxxxxxxxxxxx" } {“ access_token”:“ txxxxxxxxxxxxxxxxxxxxxFB45a”,“ expires_in”:36000,“ token_type”:“ bearer”,“ scope”:“读写组”,“ refresh_token”:“ 16oKxxxxxxxxxxxxxxxxxxxxxxxxxxx”}
to 至
{ "access_token": "xxxxxxxx.xxxxxx.xxxxx", "expires_in": 36000, "token_type": "Bearer", "scope": "read write groups", "refresh_token": "xxxxxxxx.xxxxxx.xxxxx" } {“ access_token”:“ xxxxxxxx.xxxxxx.xxxxx”,“ expires_in”:36000,“ token_type”:“ bearer”,“ scope”:“读写组”,“ refresh_token”:“ xxxxxxxx.xxxxxx.xxxxx”}
I have gone through https://github.com/Humanitec/django-oauth-toolkit-jwt/ but I think the version I am using of django-oauth-toolkit are incompatible. 我已经浏览过https://github.com/Humanitec/django-oauth-toolkit-jwt/,但是我认为我使用的django-oauth-toolkit版本不兼容。
I solved it by subclassing the oauthlib.oauth2.Server
我通过子类化
oauthlib.oauth2.Server
解决了它
class OauthServer(oauth2.Server):
def __init__(self, request_validator, token_expires_in=None, token_generator=None, *args, **kwargs):
token_generator = custom_token_generator
super().__init__(request_validator, token_expires_in, token_generator, *args, **kwargs)
The custom_token_generator
function will generate the jwt token custom_token_generator
函数将生成jwt令牌
def custom_token_generator(request, refresh_token=False):
client_code = request.user and request.user.client.codigo
now = datetime.now()
payload = {
'iat': int(now.timestamp()),
'exp': int(expires.timestamp()),
}
if client_code:
payload['org'] = client_code
return jwt.encode(payload, settings.JWT['EC_PRIVATE_KEY'].encode(), algorithm='ES256').decode('ascii')
It's not the ideal JWT, but you can make what you need, the only problem is changing the AccessToken and RefreshToken token
field to a TextField since the JWT length will go way over the limit 它不是理想的JWT,但是您可以满足需要,唯一的问题是将AccessToken和RefreshToken
token
字段更改为TextField,因为JWT长度将超出限制
from oauth2_provider.models import AbstractAccessToken, AbstractRefreshToken
class AccessToken(AbstractAccessToken):
token = models.TextField()
class RefreshToken(AbstractRefreshToken):
token = models.TextField()
the django-oauth-toolkit docs will have more information on overwriting these fields on the django settings django-oauth-toolkit文档将提供有关覆盖django设置上的这些字段的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.