基于json web令牌的django认证
webstack-django-jwt-auth的Python项目详细描述
Django JWT认证
概述
这个包提供JSON Web Token Authentication 使用PyJWT支持django。
项目是创建的(https://github.com/jpadilla/django-jwt-auth)的分支 作者:JoséPadilla(Pyjwt的维护者)。何塞似乎没有时间 在Django Jwt Auth工作。
原始代码的新功能:
- 刷新令牌
- 提供2个中间产品
- Django 2.0
- 更好的覆盖范围和包装
安装
使用pip
安装…
$ pip install webstack-django-jwt-auth
用法
在您的urls.py
中,添加以下url路由以允许通过
post包含用户的用户名和密码。
fromjwt_authimportviewsasjwt_auth_viewsfromyour_app.viewsimportRestrictedViewurlpatterns=[# ...path("token-auth/",jwt_auth_views.jwt_token),path("token-refresh/",jwt_auth_views.refresh_jwt_token),path("protected-url/",RestrictedView.as_view()),]
在你的应用程序中,创建一个django受限视图:
importjsonfromdjango.httpimportJsonResponsefromdjango.views.genericimportViewfromjwt_auth.mixinsimportJSONWebTokenAuthMixinclassRestrictedView(JSONWebTokenAuthMixin,View):defget(self,request):data={"foo":"bar","username":request.user.username,}returnJsonResponse(data)
通过在 终端,如果您创建了一个用户名admin和密码的用户 abc123。
$ curl -X POST -H "Content-Type: application/json" -d '{"username":"admin","password":"abc123"}' http://localhost:8000/api-token-auth/
现在,为了访问受保护的api url,必须包含Authorization: Bearer <your_token>
头。
$ curl -H "Authorization: Bearer <your_token>" http://localhost:8000/protected-url/
如果您更喜欢这个视图,还提供了一个中间件 整合。只需在中间件中添加以下内容:
MIDDLEWARE=(# ...'jwt_auth.middleware.JWTAuthenticationMiddleware',)
附加设置
有一些额外的设置,您可以重写类似于 它本身带有django rest框架。以下是所有可用的默认值。
JWT_ALGORITHM='HS256'JWT_ALLOW_REFRESH=FalseJWT_AUDIENCE=NoneJWT_AUTH_HEADER_PREFIX='Bearer'JWT_DECODE_HANDLER='jwt_auth.utils.jwt_decode_handler',JWT_ENCODE_HANDLER='jwt_auth.utils.jwt_encode_handler'JWT_EXPIRATION_DELTA=datetime.timedelta(seconds=300)JWT_LEEWAY=0JWT_LOGIN_URL=settings.LOGIN_URLJWT_PAYLOAD_GET_USER_ID_HANDLER='jwt_auth.utils.jwt_get_user_id_from_payload_handler'JWT_PAYLOAD_HANDLER='jwt_auth.utils.jwt_payload_handler'JWT_REFRESH_EXPIRATION_DELTA=datetime.timedelta(days=7)JWT_SECRET_KEY:SECRET_KEYJWT_VERIFY=TrueJWT_VERIFY_EXPIRATION=True
这个包使用json web令牌python实现, PyJWT并允许修改 可用选项。
jwt_算法
可能值:
- HS256 - HMAC using SHA-256 hash algorithm (default)
- HS384 - HMAC using SHA-384 hash algorithm
- HS512 - HMAC using SHA-512 hash algorithm
- RS256 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-256 hash algorithm
- RS384 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-384 hash algorithm
- RS512 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-512 hash algorithm
注意:
For the RSASSA-PKCS1-v1_5 algorithms, the "secret" argument in jwt.encode is supposed to be a private RSA key as imported with Crypto.PublicKey.RSA.importKey. Likewise, the "secret" argument in jwt.decode is supposed to be the public RSA key imported with the same method.
默认为"HS256"
。
允许刷新
启用令牌刷新功能。从jwt_auth.views.jwt_token
颁发的令牌
将有一个orig_iat
字段。
默认值是False
Jwt_观众
通常,被访问资源的基址,例如https://example.com
。
jwt_auth_header_前缀
您可以修改需要发送的授权头值前缀 连同令牌。
默认值是Bearer
。
到期日
这是python的datetime.timedelta
的一个实例。这将添加到
datetime.utcnow()
设置过期时间。
默认值是datetime.timedelta(seconds=300)
(5分钟)。
Jwt_leeway
This allows you to validate an expiration time which is in the past but no very far. For example, if you have a JWT payload with an expiration time set to 30 seconds after creation but you know that sometimes you will process it after 30 seconds, you can set a leeway of 10 seconds in order to have some margin.
默认值是0
秒。
jwt_payload_get_user_id_handler
如果存储user_id
的方式与默认负载处理程序不同,
实现此函数以从负载中获取user_id
。
有效载荷处理器
指定自定义函数以生成令牌负载
jwt_refresh_expiration_delta
令牌刷新限制,是一个datetime.timedelta
实例。这是多少钱
可从中刷新未来令牌的原始令牌之后的时间。
默认值是datetime.timedelta(days=7)
(7天)。
Jwt_secret_密钥
这是用于加密jwt的密钥。确保这是安全的 共享的或公开的。
默认值是项目的settings.SECRET_KEY
。
JWT U验证
如果这个秘密是错误的,它将引发一个jwt.decode错误来告诉您。你
仍然可以通过将JWT_VERIFY
设置为False
获得有效负载。
默认值是True
。
jwt_verify_expiration
您可以通过设置关闭过期时间验证
JWT_VERIFY_EXPIRATION
到False
。
默认值是True
。