You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.1 KiB
Python
111 lines
3.1 KiB
Python
import datetime as dt
|
|
|
|
from apistar import http, Route, Include
|
|
|
|
from apistar_jwt.token import JWT, JWTUser
|
|
# from apistar_mail import Message, Mail
|
|
# from sqlalchemy.exc import IntegrityError, InvalidRequestError
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
from cookie_api.models import User
|
|
from cookie_api.schema import UserExportSchema, UserCreateSchema
|
|
from cookie_api.util import ExtJSONResponse
|
|
|
|
|
|
def login(json_data: http.RequestData, session: Session, jwt: JWT):
|
|
user_id = json_data.get('email')
|
|
password = json_data.get('password')
|
|
|
|
try:
|
|
user = session.query(User).filter_by(email=user_id).one()
|
|
|
|
except NoResultFound:
|
|
error = {
|
|
'status': 'fail',
|
|
'message': 'User does not exist'
|
|
}
|
|
return ExtJSONResponse(error, status=400, headers={'WWW-Authenticate': 'Bearer'})
|
|
|
|
if not user.check_password(password):
|
|
error = {'error': 'Password auth failed'},
|
|
return ExtJSONResponse(error, status=401, headers={'WWW-Authenticate': 'Bearer'})
|
|
|
|
payload = {
|
|
'exp': dt.datetime.utcnow() + dt.timedelta(days=0, minutes=60), # Expiration date of the token
|
|
'iat': dt.datetime.utcnow(), # the time the token was generated
|
|
'sub': user.id # the subject of the token
|
|
}
|
|
|
|
token = jwt.encode(payload)
|
|
|
|
data = {
|
|
'status': 'success',
|
|
'message': 'Successfully logged in.',
|
|
'auth_token': token
|
|
}
|
|
|
|
return ExtJSONResponse(data, 200)
|
|
|
|
|
|
# TODO Add user logout
|
|
def logout():
|
|
pass
|
|
|
|
|
|
# TODO Add email confirmation to registration
|
|
# def register(user_data: UserCreateSchema, session: Session, mail: Mail):
|
|
def register(user_data: UserCreateSchema, session: Session) -> UserExportSchema:
|
|
email_check = session.query(User).filter_by(email=user_data['email']).one_or_none()
|
|
|
|
if email_check is not None:
|
|
error = {
|
|
'status': 'error',
|
|
'message': 'user email address is already in use'
|
|
}
|
|
return ExtJSONResponse(error, 400)
|
|
|
|
user = User(email=user_data['email'], password=user_data['password'])
|
|
|
|
session.add(user)
|
|
session.commit()
|
|
|
|
# msg = Message("Thank you for registering please confirm your email", recipients=[user_rep['email']])
|
|
# mail.send(msg)
|
|
|
|
# headers = {}
|
|
message = {
|
|
'status': 'success',
|
|
'message': 'Please check your inbox and confirm your email',
|
|
'data': UserExportSchema(user)
|
|
}
|
|
return ExtJSONResponse(message, 201)
|
|
|
|
|
|
def user_profile(user: JWTUser, session: Session) -> UserExportSchema:
|
|
try:
|
|
user = session.query(User).filter_by(id=user.id).one()
|
|
except NoResultFound as e:
|
|
error = {'message': str(e)}
|
|
return ExtJSONResponse(error, 400)
|
|
return ExtJSONResponse(UserExportSchema(user))
|
|
|
|
|
|
# TODO Add email confirmation
|
|
def confirm(json_data: http.RequestData, session: Session):
|
|
pass
|
|
|
|
|
|
# TODO Add email password reset
|
|
def reset():
|
|
pass
|
|
|
|
|
|
routes = [
|
|
Route('/login', 'POST', login),
|
|
Route('/register', 'POST', register),
|
|
Route('/status', 'GET', user_profile)
|
|
]
|
|
|
|
auth_routes = [Include('/auth', name='auth', routes=routes)]
|