async def send_confirmation_link(mail: str): """ Sends a link to the user's mail id for account verification :param mail: the mail id of the user :return: mail id, mail subject and mail body """ email_enabled = Utility.email_conf["email"]["enable"] if email_enabled: if isinstance(mail_check(mail), ValidationFailure): raise AppException("Please enter valid email id") Utility.is_exist(UserEmailConfirmation, exp_message="Email already confirmed!", email__iexact=mail.strip()) if not Utility.is_exist(User, email__iexact=mail.strip(), status=True, raise_error=False): raise AppException( "Error! There is no user with the following mail id") user = AccountProcessor.get_user(mail) token = Utility.generate_token(mail) link = Utility.email_conf["app"]["url"] + '/verify/' + token return mail, user['first_name'], link else: raise AppException("Error! Email verification is not enabled")
async def verify_and_process(request, sso_type: str): """ Fetches user details and returns a login token. If user does not have an account, it will be created. :param request: starlette request object :param sso_type: one of supported types - google/facebook/linkedin. """ sso_client = LoginSSOFactory.get_client(sso_type) user_details = await sso_client.verify(request) try: AccountProcessor.get_user(user_details['email']) existing_user = True except DoesNotExist: existing_user = False user_details['password'] = SecretStr(Utility.generate_password()) user_details['account'] = user_details['email'] if existing_user: AccountProcessor.get_user_details(user_details['email']) else: await AccountProcessor.account_setup(user_details) tmp_token = Utility.generate_token(user_details['email']) await AccountProcessor.confirm_email(tmp_token) access_token = Authentication.create_access_token(data={"sub": user_details["email"]}) return existing_user, user_details, access_token
async def account_setup(account_setup: Dict): """ create new account :param account_setup: dict of account details :param user: user id :return: dict user details, user email id, confirmation mail subject, mail body """ from kairon.shared.data.processor import MongoProcessor account = None bot = None mail_to = None email_enabled = Utility.email_conf["email"]["enable"] link = None user = account_setup.get("email") try: account = AccountProcessor.add_account( account_setup.get("account"), user) bot = AccountProcessor.add_bot('Hi-Hello', account["_id"], user, True) user_details = AccountProcessor.add_user( email=account_setup.get("email"), first_name=account_setup.get("first_name"), last_name=account_setup.get("last_name"), password=account_setup.get("password").get_secret_value(), account=account["_id"].__str__(), user=user) AccountProcessor.__allow_access_to_bot( bot["_id"].__str__(), account_setup.get("email"), account_setup.get("email"), account['_id'], ACCESS_ROLES.OWNER.value, ACTIVITY_STATUS.ACTIVE.value) await MongoProcessor().save_from_path( "template/use-cases/Hi-Hello", bot["_id"].__str__(), user="******") if email_enabled: token = Utility.generate_token(account_setup.get("email")) link = Utility.email_conf["app"]["url"] + '/verify/' + token mail_to = account_setup.get("email") except Exception as e: if account and "_id" in account: Account.objects().get(id=account["_id"]).delete() if bot and "_id" in bot: Bot.objects().get(id=bot["_id"]).delete() raise e return user_details, mail_to, link
def allow_bot_and_generate_invite_url( bot: Text, email: Text, user: Text, bot_account: int, role: ACCESS_ROLES = ACCESS_ROLES.TESTER.value): token = Utility.generate_token(email) link = f'{Utility.email_conf["app"]["url"]}/{bot}/invite/accept/{token}' if role == ACCESS_ROLES.OWNER.value: raise AppException('There can be only 1 owner per bot') if Utility.email_conf["email"]["enable"]: activity_status = ACTIVITY_STATUS.INVITE_NOT_ACCEPTED.value else: activity_status = ACTIVITY_STATUS.ACTIVE.value bot_details = AccountProcessor.__allow_access_to_bot( bot, email, user, bot_account, role, activity_status) return bot_details['name'], link
async def send_reset_link(mail: str): """ Sends a password reset link to the mail id :param mail: email id of the user :return: mail id, mail subject, mail body """ email_enabled = Utility.email_conf["email"]["enable"] if email_enabled: mail = mail.strip() if isinstance(mail_check(mail), ValidationFailure): raise AppException("Please enter valid email id") if not Utility.is_exist( User, email__iexact=mail, status=True, raise_error=False): raise AppException( "Error! There is no user with the following mail id") if not Utility.is_exist(UserEmailConfirmation, email__iexact=mail, raise_error=False): raise AppException( "Error! The following user's mail is not verified") UserActivityLogger.is_password_reset_within_cooldown_period(mail) UserActivityLogger.is_password_reset_request_limit_exceeded(mail) token_expiry = Utility.environment['user'][ 'reset_password_cooldown_period'] or 120 token = Utility.generate_token(mail, token_expiry * 60) user = AccountProcessor.get_user(mail) link = Utility.email_conf["app"]["url"] + '/reset_password/' + token UserActivityLogger.add_log( account=user['account'], email=mail, a_type=UserActivityType.reset_password_request.value) return mail, user['first_name'], link else: raise AppException("Error! Email verification is not enabled")