Exemplo n.º 1
0
    def post(self):
        log_event("POST: /login")
        body = request.get_json(force=True)
        err_response_message = 'Incorrect login details'
        for user in UserModel.scan(UserModel.email == (body['email'])):

            email = user.email
            password_hash = user.password
            password = body['password']

            if pbkdf2_sha256.verify(password, password_hash):
                access_token = create_access_token(identity=email)
                refresh_token = create_refresh_token(identity=email)

                access_token_jti = get_jti(access_token)
                refresh_token_jti = get_jti(refresh_token)

                user.update(actions=[
                    UserModel.accessToken.set(access_token_jti),
                    UserModel.refreshToken.set(refresh_token_jti),
                    UserModel.updated_at.set(
                        datetime.datetime.utcnow().isoformat()),
                ])

                return success_response({
                    'uuid': user.uuid,
                    'accessToken': access_token,
                    'refreshToken': refresh_token
                })
            else:
                return err_response(err_response_message, 404)
        return err_response(err_response_message, 404)
Exemplo n.º 2
0
 def get(self, user_id):
     log_event(f"GET: /users/{user_id}")
     try:
         user = UserModel.get(user_id, attributes_to_get=['uuid', 'accessToken', 'refreshToken', 'email', 'firstName', 'lastName'])
         user = model_helpers.model_to_dict(user)
         return success_response(user)
     except UserModel.DoesNotExist:
         return err_response('User does not exist', 404)
     except Exception:
         return err_response('Error getting user', 500)
Exemplo n.º 3
0
 def get(self, user_id):
     log_event(f"GET: /users/{user_id}")
     try:
         user = {'user_id': user_id}
         return success_response(user)
     except Exception:
         return err_response('Error getting user', 500)
Exemplo n.º 4
0
 def patch(self, user_id):
     log_event(f"PATCH: /users/{user_id}")
     body = request.get_json(force=True)
     user = UserModel.get(user_id)
     for item in body:
         try:
             user.update_item(attribute=item, value=body[item], action='PUT')
             user.update_item(attribute='updated_at', value=datetime.datetime.utcnow().isoformat(), action='PUT')
         except Exception as e:
             return err_response(e, 400)
     return success_response({'message': 'Successfully updated user'})
Exemplo n.º 5
0
    def post(self):
        log_event("POST: /signup")
        body = request.get_json(force=True)
        try:
            for user in UserModel.scan(UserModel.email == (body['email'])):
                return err_response('User already exists', 404)

            user_id = str(uuid.uuid4())
            try:
                user = UserModel(user_id)
                user.email = body['email']
                user.firstName = body['firstName']
                user.lastName = body['lastName']
                user.password = pbkdf2_sha256.encrypt(body['password'], salt_size=16)
                now = datetime.datetime.utcnow().isoformat()
                user.created_at = now
                user.updated_at = now

                access_token = create_access_token(identity=body['email'])
                refresh_token = create_refresh_token(identity=body['email'])
                access_token_jti = get_jti(access_token)
                refresh_token_jti = get_jti(refresh_token)

                user.accessToken = access_token_jti
                user.refreshToken = refresh_token_jti

                user.save()
                return success_response({
                    'message': 'Created user successfully',
                    'uuid': user_id,
                    'accessToken': access_token,
                    'refreshToken': refresh_token
                })
            except Exception as e:
                return err_response(e, 400)
        except Exception as e:
                return err_response(e, 400)