Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
周伟奇
/
bmw-ocr
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
f2a7c4b4
authored
2022-06-23 14:54:35 +0800
by
周伟奇
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
token modify part2
1 parent
4c8dca19
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
11 deletions
src/apps/account/authentication.py
src/apps/account/views.py
src/common/redis_cache/handler.py
src/apps/account/authentication.py
View file @
f2a7c4b
...
...
@@ -6,6 +6,7 @@ from rest_framework import exceptions
from
rest_framework_jwt.settings
import
api_settings
import
jwt
from
django.utils.translation
import
ugettext
as
_
from
common.redis_cache
import
redis_handler
as
rh
jwt_decode_handler
=
api_settings
.
JWT_DECODE_HANDLER
...
...
@@ -41,19 +42,20 @@ class MyJSONWebTokenAuthentication(JSONWebTokenAuthentication):
if
jwt_value
is
None
:
return
None
print
(
'jwt_value: {0}'
.
format
(
jwt_value
))
# try
:
# payload = jwt_decode_handler(jwt_value
)
# except jwt.ExpiredSignatur
e:
#
msg = _('Signature has expired.')
#
raise exceptions.AuthenticationFailed(msg)
jwt_str
=
str
(
jwt_value
)[
-
10
:]
is_expired
=
rh
.
get_token
(
jwt_str
)
if
isinstance
(
is_expired
,
str
)
:
rh
.
set_token
(
jwt_str
)
els
e
:
msg
=
_
(
'Signature has expired.'
)
raise
exceptions
.
AuthenticationFailed
(
msg
)
try
:
payload
=
jwt_decode_handler
(
jwt_value
)
except
jwt
.
ExpiredSignature
:
msg
=
_
(
'Signature has expired.'
)
raise
exceptions
.
AuthenticationFailed
(
msg
)
# msg = _('Signature has expired.')
# raise exceptions.AuthenticationFailed(msg)
pass
except
jwt
.
DecodeError
:
msg
=
_
(
'Error decoding signature.'
)
raise
exceptions
.
AuthenticationFailed
(
msg
)
...
...
src/apps/account/views.py
View file @
f2a7c4b
...
...
@@ -9,8 +9,8 @@ from settings import conf
from
django.urls
import
reverse
from
django.http
import
HttpResponseRedirect
from
django.contrib.auth
import
login
as
auth_login
from
django.conf
import
settings
from
django.shortcuts
import
resolve_url
,
redirect
#
from django.conf import settings
#
from django.shortcuts import resolve_url, redirect
# Create your views here.
...
...
@@ -51,6 +51,7 @@ class LoginView(ObtainJSONWebToken, GenericView):
'user_name'
:
user
.
username
,
'token'
:
res
.
data
.
get
(
'token'
),
}
rh
.
set_token
(
res
.
data
.
get
(
'token'
)[
-
10
:])
return
response
.
ok
(
data
=
data
)
...
...
src/common/redis_cache/handler.py
View file @
f2a7c4b
...
...
@@ -85,3 +85,12 @@ class RedisHandler:
if
isinstance
(
expires
,
int
):
self
.
redis
.
expire
(
key
,
expires
)
def
get_token_key
(
self
,
token_str
):
return
'{0}:token:{1}'
.
format
(
self
.
prefix
,
token_str
)
def
set_token
(
self
,
token_str
,
expires
=
1800
):
return
self
.
redis
.
set
(
self
.
get_token_key
(
token_str
),
'token'
,
expires
)
def
get_token
(
self
,
token_str
):
return
self
.
redis
.
get
(
self
.
get_token_key
(
token_str
))
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment