Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CS123-Canteeneo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Willard Torres
CS123-Canteeneo
Commits
158feeaa
Commit
158feeaa
authored
Oct 25, 2016
by
Willard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move token methods outside of model
parent
cfd246f1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
23 deletions
+19
-23
api.py
canteeneo/api.py
+18
-5
models.py
canteeneo/models.py
+1
-18
No files found.
canteeneo/api.py
View file @
158feeaa
...
...
@@ -2,6 +2,7 @@ from canteeneo import app, db, auth
from
flask
import
jsonify
,
request
,
g
from
models
import
Dish
,
Stall
,
Location
,
User
from
datetime
import
datetime
from
itsdangerous
import
TimedJSONWebSignatureSerializer
as
Serializer
,
BadSignature
,
SignatureExpired
@
app
.
route
(
'/api/all'
)
def
all
():
...
...
@@ -63,19 +64,31 @@ def new_user():
@
auth
.
verify_password
def
verify_password
(
username
,
password
):
user
=
User
.
verify_token
(
username
)
print
(
user
)
user
=
verify_token
(
username
)
if
user
is
None
:
user
=
User
.
query
.
filter_by
(
username
=
username
)
.
first
()
print
(
user
)
if
user
is
None
or
not
user
.
check_password
(
password
):
return
False
print
(
user
)
g
.
user
=
user
return
True
def
generate_token
(
user
):
s
=
Serializer
(
app
.
config
[
'SECRET_KEY'
],
expires_in
=
600
)
return
s
.
dumps
({
'id'
:
user
.
id
})
def
verify_token
(
token
):
s
=
Serializer
(
app
.
config
[
'SECRET_KEY'
])
try
:
data
=
s
.
loads
(
token
)
except
SignatureExpired
:
return
None
except
BadSignature
:
return
None
user
=
User
.
query
.
get
(
data
[
'id'
])
return
user
@
app
.
route
(
'/api/token'
)
@
auth
.
login_required
def
get_auth_token
():
token
=
g
.
user
.
generate_token
(
)
token
=
g
enerate_token
(
g
.
user
)
return
jsonify
({
'token'
:
token
.
decode
(
'ascii'
),
'duration'
:
600
})
canteeneo/models.py
View file @
158feeaa
from
canteeneo
import
app
,
db
from
canteeneo
import
db
from
werkzeug.security
import
generate_password_hash
,
check_password_hash
from
itsdangerous
import
TimedJSONWebSignatureSerializer
as
Serializer
,
BadSignature
,
SignatureExpired
dish_favorites
=
db
.
Table
(
'dish_favorites'
,
db
.
Column
(
'dish_id'
,
db
.
Integer
,
db
.
ForeignKey
(
'dish.id'
)),
...
...
@@ -106,22 +105,6 @@ class User(db.Model):
def
check_password
(
self
,
password
):
return
check_password_hash
(
self
.
password
,
password
)
def
generate_token
(
self
):
s
=
Serializer
(
app
.
config
[
'SECRET_KEY'
],
expires_in
=
600
)
return
s
.
dumps
({
'id'
:
self
.
id
})
@
staticmethod
def
verify_token
(
token
):
s
=
Serializer
(
app
.
config
[
'SECRET_KEY'
])
try
:
data
=
s
.
loads
(
token
)
except
SignatureExpired
:
return
None
except
BadSignature
:
return
None
user
=
User
.
query
.
get
(
data
[
'id'
])
return
user
class
DishReview
(
db
.
Model
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
title
=
db
.
Column
(
db
.
String
(
80
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment