Merge pull request #6 from sorrelbri/sj-auth
test authorization with user endpoint
This commit is contained in:
commit
95f2c70c8f
4 changed files with 19 additions and 7 deletions
|
@ -11,3 +11,7 @@ def api_home():
|
|||
@api.route('/users')
|
||||
def api_users():
|
||||
return jsonify(UserEndpoint.users())
|
||||
|
||||
@api.route('/user')
|
||||
def api_user():
|
||||
return jsonify(UserEndpoint.user())
|
|
@ -1,7 +1,15 @@
|
|||
from models.User import User, user_schema, users_schema
|
||||
from flask import request, jsonify, Response, json
|
||||
|
||||
class UserEndpoint(object):
|
||||
def users():
|
||||
user = User.query.all()
|
||||
response = users_schema.dumps(user)
|
||||
users = User.query.all()
|
||||
response = users_schema.dumps(users)
|
||||
return response
|
||||
def user():
|
||||
|
||||
auth_header = request.headers.get('Authorization') or None
|
||||
auth_token = auth_header.split(" ")[1]
|
||||
user = User.decode_auth_token(auth_token) or None
|
||||
response = json.dumps(user)
|
||||
return response
|
|
@ -23,7 +23,7 @@ def auth_signup():
|
|||
response = {
|
||||
'status': 'success',
|
||||
'message': 'Succesfully registered.',
|
||||
'auth_token': auth_token.decode()
|
||||
'token': auth_token.decode()
|
||||
}
|
||||
return jsonify(response), 201
|
||||
except Exception as e:
|
||||
|
@ -60,7 +60,7 @@ def auth_login():
|
|||
response = {
|
||||
'status': 'success',
|
||||
'message': 'Successfully logged in.',
|
||||
'auth_token': auth_token.decode()
|
||||
'token': auth_token.decode()
|
||||
}
|
||||
return jsonify(response), 200
|
||||
except Exception as e:
|
||||
|
|
|
@ -80,9 +80,9 @@ class User(db.Model):
|
|||
"""
|
||||
try:
|
||||
payload = {
|
||||
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=5),
|
||||
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=4),
|
||||
'iat': datetime.datetime.utcnow(),
|
||||
'sub': user_id
|
||||
'user': user_schema.dumps(self)
|
||||
}
|
||||
return jwt.encode(
|
||||
payload,
|
||||
|
@ -101,7 +101,7 @@ class User(db.Model):
|
|||
"""
|
||||
try:
|
||||
payload = jwt.decode(auth_token, os.environ.get('SECRET_KEY'))
|
||||
return payload['sub']
|
||||
return payload['user']
|
||||
except jwt.ExpiredSignatureError:
|
||||
return 'Signature expired. Please log in again.'
|
||||
except jwt.InvalidTokenError:
|
||||
|
|
Loading…
Reference in a new issue