Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ECE651_Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chris Li
ECE651_Backend
Commits
9889b7ac
Commit
9889b7ac
authored
2 years ago
by
Chris Li
Browse files
Options
Downloads
Patches
Plain Diff
Add token validation.
parent
95867e99
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/api/auth.py
+15
-1
15 additions, 1 deletion
core/api/auth.py
core/urls.py
+2
-1
2 additions, 1 deletion
core/urls.py
with
17 additions
and
2 deletions
core/api/auth.py
+
15
−
1
View file @
9889b7ac
from
django.shortcuts
import
render
from
django.shortcuts
import
render
from
rest_framework
import
generics
from
rest_framework
import
generics
,
HTTP_HEADER_ENCODING
from
rest_framework.decorators
import
api_view
,
authentication_classes
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
knox.auth
import
TokenAuthentication
from
knox.models
import
AuthToken
from
knox.models
import
AuthToken
from
django.contrib.auth.backends
import
AllowAllUsersModelBackend
from
django.contrib.auth.backends
import
AllowAllUsersModelBackend
...
@@ -39,6 +41,18 @@ class LoginAPI(generics.GenericAPIView):
...
@@ -39,6 +41,18 @@ class LoginAPI(generics.GenericAPIView):
})
})
@api_view
([
'
GET
'
])
@authentication_classes
([])
def
validate_token
(
request
):
try
:
authenticator
=
TokenAuthentication
()
user
,
auth_token
=
authenticator
.
authenticate
(
request
)
if
user
and
auth_token
:
return
Response
({
'
valid
'
:
'
true
'
})
except
:
return
Response
({
'
valid
'
:
'
false
'
})
def
verify_user_and_activate
(
request
,
token
):
def
verify_user_and_activate
(
request
,
token
):
try
:
try
:
auth
=
AuthToken
.
objects
.
filter
(
digest
=
token
).
first
()
auth
=
AuthToken
.
objects
.
filter
(
digest
=
token
).
first
()
...
...
This diff is collapsed.
Click to expand it.
core/urls.py
+
2
−
1
View file @
9889b7ac
...
@@ -2,7 +2,7 @@ from django.urls import path, include
...
@@ -2,7 +2,7 @@ from django.urls import path, include
from
knox
import
views
as
knox_views
from
knox
import
views
as
knox_views
from
rest_framework
import
routers
from
rest_framework
import
routers
from
core.api.auth
import
RegisterAPI
,
LoginAPI
,
verify_user_and_activate
from
core.api.auth
import
RegisterAPI
,
LoginAPI
,
validate_token
,
verify_user_and_activate
from
core.api.password
import
ChangePasswordView
from
core.api.password
import
ChangePasswordView
from
core.api.profile
import
ProfileViewSet
from
core.api.profile
import
ProfileViewSet
...
@@ -16,6 +16,7 @@ urlpatterns += [
...
@@ -16,6 +16,7 @@ urlpatterns += [
path
(
'
api/auth/activate/<token>
'
,
verify_user_and_activate
,
name
=
'
activate
'
),
path
(
'
api/auth/activate/<token>
'
,
verify_user_and_activate
,
name
=
'
activate
'
),
path
(
'
api/auth/login
'
,
LoginAPI
.
as_view
(),
name
=
'
login
'
),
path
(
'
api/auth/login
'
,
LoginAPI
.
as_view
(),
name
=
'
login
'
),
path
(
'
api/auth/logout
'
,
knox_views
.
LogoutView
.
as_view
(),
name
=
'
logout
'
),
path
(
'
api/auth/logout
'
,
knox_views
.
LogoutView
.
as_view
(),
name
=
'
logout
'
),
path
(
'
api/auth/validate-token
'
,
validate_token
,
name
=
'
validate-token
'
),
# passwd
# passwd
path
(
'
api/change-password
'
,
ChangePasswordView
.
as_view
(),
name
=
'
change-password
'
),
path
(
'
api/change-password
'
,
ChangePasswordView
.
as_view
(),
name
=
'
change-password
'
),
path
(
'
api/password_reset/
'
,
include
(
'
django_rest_passwordreset.urls
'
,
namespace
=
'
password_reset
'
)),
path
(
'
api/password_reset/
'
,
include
(
'
django_rest_passwordreset.urls
'
,
namespace
=
'
password_reset
'
)),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment