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
4c8dca19
authored
2022-06-23 11:36:47 +0800
by
周伟奇
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
token modify part1
1 parent
532cd578
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
1 deletions
src/apps/account/authentication.py
src/settings/__init__.py
src/apps/account/authentication.py
View file @
4c8dca1
from
django.contrib.auth
import
get_user_model
from
oauth2_provider.contrib.rest_framework
import
OAuth2Authentication
from
oauth2_provider.oauth2_backends
import
get_oauthlib_core
from
rest_framework_jwt.authentication
import
JSONWebTokenAuthentication
from
rest_framework
import
exceptions
from
rest_framework_jwt.settings
import
api_settings
import
jwt
from
django.utils.translation
import
ugettext
as
_
jwt_decode_handler
=
api_settings
.
JWT_DECODE_HANDLER
class
OAuth2AuthenticationWithUser
(
OAuth2Authentication
):
...
...
@@ -21,3 +28,38 @@ class OAuth2AuthenticationWithUser(OAuth2Authentication):
return
self
.
user
,
r
.
access_token
request
.
oauth2_error
=
getattr
(
r
,
"oauth2_error"
,
{})
return
None
class
MyJSONWebTokenAuthentication
(
JSONWebTokenAuthentication
):
def
authenticate
(
self
,
request
):
"""
Returns a two-tuple of `User` and token if a valid signature has been
supplied using JWT-based authentication. Otherwise returns `None`.
"""
jwt_value
=
self
.
get_jwt_value
(
request
)
if
jwt_value
is
None
:
return
None
print
(
'jwt_value: {0}'
.
format
(
jwt_value
))
# try:
# payload = jwt_decode_handler(jwt_value)
# except jwt.ExpiredSignature:
# 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
)
except
jwt
.
DecodeError
:
msg
=
_
(
'Error decoding signature.'
)
raise
exceptions
.
AuthenticationFailed
(
msg
)
except
jwt
.
InvalidTokenError
:
raise
exceptions
.
AuthenticationFailed
()
user
=
self
.
authenticate_credentials
(
payload
)
return
(
user
,
jwt_value
)
...
...
src/settings/__init__.py
View file @
4c8dca1
...
...
@@ -156,7 +156,8 @@ REST_FRAMEWORK = {
),
'DEFAULT_AUTHENTICATION_CLASSES'
:
(
'rest_framework.authentication.BasicAuthentication'
,
'rest_framework_jwt.authentication.JSONWebTokenAuthentication'
,
'apps.account.authentication.MyJSONWebTokenAuthentication'
,
# 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
# 'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
),
'EXCEPTION_HANDLER'
:
'common.exceptions.exception_handler'
...
...
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