def handleFacebookUser(): fb_response = request.json.get(Labels.FbResponse) guest_jwt = request.json.get(Labels.Jwt) guest_user = JwtUtil.getUserInfoFromJwt(guest_jwt) fb_id = fb_response.get(Labels.Id) if fb_id == None: return JsonUtil.failure() fb_user = User.query.filter_by(fb_id=fb_response.get(Labels.Id)).first() # if the fb_user already has an account if fb_user: fb_user.transferGuestCart(guest_user) user_jwt = JwtUtil.create_jwt(fb_user.toJwtDict()) user_info = fb_user.toPublicDictFast() output = {Labels.User: user_info, Labels.Jwt: user_jwt} return JsonUtil.successWithOutput(output) register_user_response = User.registerFacebookUser(fb_response, guest_user) if register_user_response.get(Labels.Success): register_user_response[Labels.Jwt] = JwtUtil.create_jwt( register_user_response[Labels.Jwt]) return JsonUtil.successWithOutput(register_user_response) else: return JsonUtil.failureWithOutput(register_user_response) return JsonUtil.failure()
def registerUserAccount(): name = request.json.get(Labels.Name) email_input = request.json.get(Labels.Email) password = request.json.get(Labels.Password) password_confirm = request.json.get(Labels.PasswordConfirm) guest_jwt = request.json.get(Labels.GuestJwt) guest_user = JwtUtil.getUserInfoFromJwt(guest_jwt) register_user_response = User.registerUser(name, email_input, password, password_confirm, guest_user) if register_user_response.get(Labels.Success): register_user_response[Labels.Jwt] = JwtUtil.create_jwt( register_user_response[Labels.Jwt]) return JsonUtil.successWithOutput(register_user_response) else: return JsonUtil.failureWithOutput(register_user_response)
def checkAdminLogin(): ip = request.remote_addr username = request.json.get(Labels.Username) password = request.json.get(Labels.Password) if LoginAttempt.blockIpAddress(ip): LoginAttempt.addLoginAttempt(username, ip, success=False, is_admin=True) return JsonUtil.failure(ErrorMessages.IpBlocked) if AdminUser.checkLogin(username, password): admin_user = AdminUser.query.filter_by(username=username).first() admin_jwt = JwtUtil.create_jwt(admin_user.toPublicDict()) LoginAttempt.addLoginAttempt(username, ip, success=True, is_admin=True) return JsonUtil.successWithOutput({ Labels.User: admin_user.toPublicDict(), "jwt": admin_jwt }) else: LoginAttempt.addLoginAttempt(username, ip, success=False, is_admin=True) return JsonUtil.failure(ErrorMessages.InvalidCredentials)
def getProductRequests(): jwt = request.json.get(Labels.Jwt) decoded_jwt = JwtUtil.decodeAdminJwt(jwt) if not decoded_jwt: AdminAction.addAdminAction(decoded_jwt, request.path, request.remote_addr, success = False) return JsonUtil.jwt_failure() all_requests = Request.query.all() AdminAction.addAdminAction(decoded_jwt, request.path, request.remote_addr, success = True) return jsonify([req.toPublicDict() for req in all_requests])
def checkPassword(this_user): input_password = request.json.get(Labels.Password) if this_user.checkLogin(input_password): output = { Labels.User: this_user.toPublicDict(), Labels.Jwt: JwtUtil.create_jwt(this_user.toJwtDict()) } return JsonUtil.successWithOutput(output) else: return JsonUtil.failure(ErrorMessages.InvalidCredentials)
def refreshCheckoutInfo(this_user): address = request.json.get(Labels.Address) time_0 = time.time() public_user_dict = this_user.toPublicDictCheckout(address) return JsonUtil.successWithOutput({ Labels.Jwt: JwtUtil.create_jwt(this_user.toJwtDict()), Labels.User: public_user_dict })
def wrapper(): jwt = request.json.get(Labels.Jwt) admin_user = JwtUtil.decodeAdminJwt(jwt) if not admin_user: AdminAction.addAdminAction(admin_user, request.path, request.remote_addr, success=False) return JsonUtil.failure(ErrorMessages.InvalidCredentials) return func(admin_user)
def getUserInfo(this_user): adjusted_items = this_user.adjustCart() public_user_dict = this_user.toPublicDict() return JsonUtil.successWithOutput({ Labels.Jwt: JwtUtil.create_jwt(this_user.toJwtDict()), Labels.User: public_user_dict, Labels.AdjustedItems: adjusted_items })
def confirmEmail(): email_confirmation_id = request.json.get(Labels.EmailConfirmationId) this_user = User.query.filter_by( email_confirmation_id=email_confirmation_id).first() if this_user == None: return JsonUtil.failure() else: this_user.confirmEmail() return JsonUtil.successWithOutput({ Labels.User: this_user.toPublicDict(), Labels.Jwt: JwtUtil.create_jwt(this_user.toJwtDict()) })
def checkLogin(): email_input = request.json.get(Labels.Email) input_password = request.json.get(Labels.Password) if email_input == "": return JsonUtil.failure(ErrorMessages.BlankEmail) if isinstance(email_input, str): email = email_input.lower() else: return JsonUtil.failure(ErrorMessages.InvalidCredentials) ip = request.remote_addr if LoginAttempt.blockIpAddress(ip): return JsonUtil.failure(ErrorMessages.IpBlocked) this_user = User.query.filter_by(email=email).first() if this_user == None: LoginAttempt.addLoginAttempt(email, ip, success=False, is_admin=False) return JsonUtil.failure(ErrorMessages.InvalidCredentials) elif this_user.isFacebookUser(): LoginAttempt.addLoginAttempt(email, ip, success=False, is_admin=False) return JsonUtil.failure(ErrorMessages.InvalidCredentials) if this_user.checkLogin(input_password): guest_jwt = request.json.get(Labels.GuestJwt) if guest_jwt: guest_user = JwtUtil.getUserInfoFromJwt(guest_jwt) this_user.transferGuestCart(guest_user) user_jwt = JwtUtil.create_jwt(this_user.toJwtDict()) user_info = this_user.toPublicDictFast() output = {Labels.User: user_info, Labels.Jwt: user_jwt} LoginAttempt.addLoginAttempt(email, ip, success=True, is_admin=False) return JsonUtil.successWithOutput(output) else: LoginAttempt.addLoginAttempt(email, ip, success=False, is_admin=False) return JsonUtil.failure(ErrorMessages.InvalidCredentials)
def changePassword(this_user): old_password = request.json.get(Labels.OldPassword) new_password = request.json.get(Labels.Password) new_password_confirm = request.json.get(Labels.PasswordConfirm) if new_password == new_password_confirm: valid_password = this_user.changePassword(old_password, new_password) if valid_password: output = { Labels.User: this_user.toPublicDict(), Labels.Jwt: JwtUtil.create_jwt(this_user.toJwtDict()) } return JsonUtil.successWithOutput(output) else: return JsonUtil.failure(ErrorMessages.InvalidCredentials) else: return JsonUtil.failure(ErrorMessages.InvalidCredentials)
def softDeleteProductRequestByRequestId(): jwt = request.json.get(Labels.Jwt) decoded_jwt = JwtUtil.decodeAdminJwt(jwt) if not decoded_jwt: AdminAction.addAdminAction(decoded_jwt, request.path, request.remote_addr, success = False) return JsonUtil.jwt_failure() request_id = request.json.get(Labels.RequestId) if request_id == None: AdminAction.addAdminAction(decoded_jwt, request.path, request.remote_addr, success = False) return JsonUtil.failure("Bad input") this_request = Request.query.filter_by(request_id = request_id).first() if this_request == None: AdminAction.addAdminAction(decoded_jwt, request.path, request.remote_addr, success = False) return JsonUtil.failure("This request id doesn't exist") this_request.soft_deleted = True db.session.commit() AdminAction.addAdminAction(decoded_jwt, request.path, request.remote_addr, success = True) return JsonUtil.success()
def createGuestUser(): create_guest_user = User.createGuestUser() create_guest_user[Labels.Jwt] = JwtUtil.create_jwt( create_guest_user[Labels.User]) return JsonUtil.successWithOutput(create_guest_user)
def wrapper(): jwt = request.json.get(Labels.Jwt) this_user = JwtUtil.getUserInfoFromJwt(jwt) if this_user == None: return JsonUtil.failure(ErrorMessages.InvalidCredentials) return func(this_user)