Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-1-CloudComputing
/
C_Team_KhuDrive
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
Authored by
하재연
2020-06-11 00:54:24 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d75fe6d11198915f2e46fbf87da1010c813f6949
d75fe6d1
1 parent
3065702e
Add new file
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
0 deletions
backend/api/utils.py
backend/api/utils.py
0 → 100644
View file @
d75fe6d
import
jwt
import
json
from
rest_framework
import
status
from
django.http
import
JsonResponse
from
django.core.exceptions
import
ObjectDoesNotExist
from
django.conf
import
settings
from
api.models
import
User
def
login_decorator
(
func
):
def
wrapper
(
self
,
request
,
*
args
,
**
kwargs
):
if
'Authorization'
not
in
request
.
headers
:
return
JsonResponse
({
'Error'
:
'INVALID_LOGIN'
},
status
=
status
.
HTTP_401_UNAUTHORIZED
)
request_token
=
request
.
headers
[
'Authorization'
]
encode_token
=
request_token
.
encode
(
'utf-8'
)
try
:
payload
=
jwt
.
decode
(
encode_token
,
settings
.
SECRET_KEY
,
algorithm
=
'HS256'
)
user
=
User
.
objects
.
get
(
int_id
=
payload
[
'int_id'
])
request
.
user
=
user
except
jwt
.
exceptions
.
DecodeError
:
return
JsonResponse
({
'Error'
:
'INVALID_TOKEN'
},
status
=
status
.
HTTP_400
)
except
User
.
DoesNotExist
:
return
JsonResponse
({
'Error'
:
'UNKNOWN_USER'
},
status
=
status
.
HTTP_400
)
return
func
(
self
,
request
,
*
args
,
**
kwargs
)
return
wrapper
\ No newline at end of file
Please
register
or
login
to post a comment