def test_encode_auth_token(self): user = User(email='*****@*****.**', password='******', registered_on=datetime.datetime.utcnow()) app.config.db.session.add(user) app.config.db.session.commit() auth_token = User.encode_auth_token(user.pk) self.assertTrue(isinstance(auth_token, bytes))
def login_user(data): try: # fetch the user data user = User.objects(email=data.get('email')).first() if user: if user.check_password(data.get('password')): auth_token = User.encode_auth_token(str(user.id)) if auth_token: return response_message(SUCCESS, token=auth_token.decode()), 200 return response_message(UNKNOWN_ERROR), 401 return response_message(PASSWORD_INCORRECT), 401 return response_message(USER_NOT_EXIST), 404 except Exception as e: current_app.logger.exception(e) return response_message(EAGAIN), 500
def generate_token(user): try: # generate the auth token auth_token = User.encode_auth_token(str(user.pk)) return response_message(SUCCESS, token=auth_token.decode()) except Exception as e: logger.exception(e) return response_message(UNKNOWN_ERROR)
def generate_token(user): try: # generate the auth token auth_token = User.encode_auth_token(str(user.id)) return response_message(SUCCESS, token=auth_token.decode()), 201 except Exception as e: current_app.logger.exception(e) return response_message(UNKNOWN_ERROR), 401
def generate_token(user): try: # generate the auth token auth_token = User.encode_auth_token(str(user.id)) return error_message(SUCCESS, token=auth_token.decode()), 201 except Exception as e: print(e) return error_message(UNKNOWN_ERROR), 401
def get_logged_in_user(token): if token: payload = User.decode_auth_token(token) if not isinstance(payload, str): user = User.objects(pk=payload['sub']).first() if user: return response_message(SUCCESS, user_id=str(user.id), email=user.email, username=user.name, roles=user.roles, registered_on=user.registered_on, avatar=user.avatar, introduction=user.introduction, region=user.region), 200 return response_message(USER_NOT_EXIST), 404 return response_message(TOKEN_ILLEGAL, payload), 401 return response_message(TOKEN_REQUIRED), 401
def logout_user(data): auth_token = data if data else '' if auth_token: payload = User.decode_auth_token(auth_token) if not isinstance(payload, str): # mark the token as blacklisted return save_token(token=auth_token) return response_message(TOKEN_ILLEGAL, payload), 401 return response_message(TOKEN_REQUIRED), 401
def get_logged_in_user(new_request): # get the auth token auth_token = new_request.headers.get('X-Token') if auth_token: payload = User.decode_auth_token(auth_token) if not isinstance(payload, str): user = User.objects(pk=payload['sub']).first() if user: return error_message(SUCCESS, user_id=str(user.id), email=user.email, username=user.name, roles=user.roles, registered_on=str(user.registered_on), avatar=user.avatar, introduction=user.introduction, region=user.region), 200 return error_message(USER_NOT_EXIST), 404 return error_message(TOKEN_ILLEGAL, payload), 401 return error_message(TOKEN_REQUIRED), 401
async def save_new_user(data, admin=None): user = await User.find_one({'email': data['email']}) if not user: new_user = User( # public_id=str(uuid.uuid4()), email=data['email'], name=data.get('username', ''), registered_on=datetime.datetime.utcnow(), avatar=data.get('avatar', ''), introduction=data.get('introduction', '')) cnt = await User.count_documents() if cnt == 0: new_user.roles = ['admin'] else: new_user.roles = ['viewer'] new_user.password = data['password'] try: await new_user.commit() except Exception as e: logger.exception(e) return response_message(EINVAL, 'Field validating for User failed') user_root = USERS_ROOT / data['email'] try: await aiofiles.os.mkdir(user_root) except FileExistsError as e: return response_message(EEXIST) try: await aiofiles.os.mkdir(user_root / 'test_results') except FileExistsError as e: return response_message(EEXIST) if new_user.avatar == '': img = await render_identicon(hash(data['email']), 27) await async_wraps(img.save)(user_root / ('%s.png' % new_user.pk)) new_user.avatar = '%s.png' % new_user.pk if new_user.name == '': new_user.name = new_user.email.split('@')[0] if not admin: organization = Organization(name='Personal') organization.owner = new_user organization.path = new_user.email organization.members = [new_user] organization.personal = True await organization.commit() new_user.organizations = [organization] await new_user.commit() return generate_token(new_user) else: return response_message(USER_ALREADY_EXIST)
async def login_user(data): try: # fetch the user data user = await User.find_one({'email': data.get('email')}) if user: if user.check_password(data.get('password')): auth_token = User.encode_auth_token(str(user.pk)) if auth_token: return response_message(SUCCESS, token=auth_token.decode()) return response_message(UNKNOWN_ERROR) return response_message(PASSWORD_INCORRECT) return response_message(USER_NOT_EXIST) except Exception as e: logger.exception(e) return response_message(EAGAIN)
def get_a_user(user_id): return User.objects(pk=user_id).first()
def get_all_users(): return [user for user in User.objects()]
def save_new_user(data, admin=None): user = User.objects(email=data['email']).first() if not user: new_user = User( # public_id=str(uuid.uuid4()), email=data['email'], name=data.get('username', ''), registered_on=datetime.datetime.utcnow(), roles=data.get('roles', ['admin']), avatar=data.get('avatar', ''), introduction=data.get('introduction', '')) new_user.password = data['password'] try: new_user.save() except Exception as e: current_app.logger.exception(e) return response_message(EINVAL, 'Field validating for User failed'), 401 user_root = USERS_ROOT / data['email'] try: os.mkdir(user_root) except FileExistsError as e: return response_message(EEXIST), 401 try: os.mkdir(user_root / 'test_results') except FileExistsError as e: return response_message(EEXIST), 401 if new_user.avatar == '': img = render_identicon(hash(data['email']), 27) img.save(user_root / ('%s.png' % new_user.id)) new_user.avatar = '%s.png' % new_user.id if new_user.name == '': new_user.name = new_user.email.split('@')[0] if not admin: organization = Organization(name='Personal') organization.owner = new_user organization.path = new_user.email organization.members = [new_user] organization.personal = True organization.save() new_user.organizations = [organization] new_user.save() return generate_token(new_user) else: return response_message(USER_ALREADY_EXIST), 409
def get_all_users(): return User.objects()