当前位置:网站首页>Restframework-simplejwt rewrite authentication mechanism
Restframework-simplejwt rewrite authentication mechanism
2022-07-01 06:14:00 【Ink seeking Roy】
from rest_framework_simplejwt.authentication import JWTAuthentication
class MyJWTAuthentication(JWTAuthentication):
''' modify JWT Certification , Return to custom User Table object '''
def authenticate(self, request):
header = self.get_header(request)
if header is None:
return None
# obtain access_token
raw_token = self.get_raw_token(header)
user = None
token = None
if raw_token is None:
return None
# Define the current access_token White list
wraw_token = str(raw_token, 'utf-8') + config.JWT_WHITE_LIST_TAG
# When there is a When in the white list
if cache.has_key(wraw_token):
# In the cache obtain stay middleware Set up User object
user = cache.get(wraw_token)["user_obj"]
token = raw_token
else:
try:
# Yes, I received access_token authentication
token = self.get_validated_token(raw_token)
# According to the certified access_token Get user object
user = self.get_user(token)
except Exception as e:
# Ordinary access_token Exceptions are not handled How to throw a response is to throw a response
# primary access_token abnormal , When refreshing the interface for , obtain request.data Of refresh_token
if request.data.get("refresh_token"):
refresh_token = request.data.get("refresh_token")
# Get current refresh_token Of Valid time
refresh_time_remaining = output_time_remaining(refresh_token)
# refresh_token Within the validity period
if refresh_time_remaining > 0:
# And not on the blacklist
if not cache.has_key(refresh_token):
decoded_data = jwt_decode(str(refresh_token), config.JWT_SECRET_KEY, verify=False,
algorithms="HS256")
# obtain User object
user = Person_info.objects.get(**{
'person_id': decoded_data["user_id"]})
return user, token
def get_user(self, validated_token):
try:
# Whether it has been entered into the blacklist
if cache.has_key(str(validated_token) + config.JWT_BLACK_LIST_TAG):
raise AuthenticationFailed(_('Token contained no recognizable user identification'))
user_id = validated_token['user_id']
except KeyError:
raise InvalidToken(_('Token contained no recognizable user identification'))
try:
user = Person_info.objects.get(**{
'person_id': user_id})
except Person_info.DoesNotExist:
raise AuthenticationFailed(_('User not found'), code='user_not_found')
return user
边栏推荐
猜你喜欢

SystemVerilog学习-08-随机约束和线程控制

TIDB数据库特性总结

skywalking集成nacos动态配置

高阶-二叉平衡树

Primary application case of Excel DuPont analyzer

Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village

让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村

数据库问题,如何优化Oracle SQL查询语句更快,效率更高

【ITSM】什么是ITSM,IT部门为什么需要ITSM

OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow
随机推荐
SQL必会题之留存率
【ITSM】什么是ITSM,IT部门为什么需要ITSM
Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
DEV XPO对比之XAF BO
2022 年面向初学者的 10 大免费 3D 建模软件
Servlet
Tidb database characteristics summary
SystemVerilog学习-08-随机约束和线程控制
excel可视化
蚂蚁新村田头村变甜头村 让厦门灌口镇田头村变甜头村的特色农产品之一是
Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village
地宫取宝(记忆化深搜)
Timer based on LabVIEW
Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源
freeswitch拨打分机号
Record currency in MySQL
3D打印机穿线:5种简单的解决方案
Oracle create user + Role
C# ManualResetEvent 类的理解