简体   繁体   English

django-oauth-toolkit发行JWT令牌

[英]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.

相关问题 允许django-oauth-toolkit发出jwt而不是随机字符串 - allowing django-oauth-toolkit to issue jwt instead of random strings Django + django-oauth-toolkit 上的迁移错误 - Migration error on Django + django-oauth-toolkit django-oauth-toolkit:自定义身份验证响应 - django-oauth-toolkit : Customize authenticate response 无法设置 django-oauth-toolkit 身份验证 - Could not setup django-oauth-toolkit authentication 使用django-oauth-toolkit进行用户身份验证 - User authentication using django-oauth-toolkit 如何使用刷新令牌在 django-oauth-toolkit 上获取新的访问令牌? - How to use refresh token to obtain new access token on django-oauth-toolkit? 如何从django-oauth-toolkit令牌获取当前登录用户? - How can I get the current logged in user from a django-oauth-toolkit token? 尝试使用 django-oauth-toolkit 获取访问令牌,使用 fetch 在使用 jquery 时不起作用 - Trying to get access token with django-oauth-toolkit using fetch not working while working using jquery 带有 oAuth2 的 Django DRF 使用 DOT (django-oauth-toolkit) - Django DRF with oAuth2 using DOT (django-oauth-toolkit) django-oauth-toolkit注销问题,每次都不提示用户输入用户名/密码 - django-oauth-toolkit logout issue with not prompting the user for username/password every time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM