async def logout(req: Request, resp: Response): if req.user.id == 0: return BasicResponse() user: User = req.user await credential_ctl.revoke_access_credential(user.access_token) await auth_web_ctl.revoke_credential(resp) return BasicResponse()
async def send_reset_password_email(tasks: BackgroundTasks, form: EmailValidateForm): email = form.email member = await member_ctl.get_member(email=email) if not member: logger.warning(f'Member not found: {email}') return BasicResponse() token = await credential_ctl.set_email_validate_token( 'reset_password', email) tasks.add_task(worker.email.send_reset_email, email, token) return BasicResponse()
async def reset_password(form: ResetPasswordForm): email = await credential_ctl.get_email_validate_token( 'reset_password', form.validate_token) if not email: raise auth_error.InvalidValidateTokenError() member = await member_ctl.get_member(email=email) await member_ctl.change_member_password(member.id, form.password) return BasicResponse()
async def login(req: Request, resp: Response, login_form: LoginForm): email = login_form.email password = login_form.password member = await member_ctl.check_member_email_password(email, password) if not member: raise auth_error.IncorrectEMailPasswordError() await auth_web_ctl.create_credential(req, resp, member.id) return BasicResponse()
async def email_validate(tasks: BackgroundTasks, form: EmailValidateForm): email = form.email member = await member_ctl.get_member(email=email) if member: raise auth_error.EMailUsedError() token = await credential_ctl.set_email_validate_token('register', email) tasks.add_task(worker.email.send_register_email, email, token) return BasicResponse()
async def register(tasks: BackgroundTasks, req: Request, resp: Response, form: RegisterForm): email = await credential_ctl.get_email_validate_token( 'register', form.validate_token) if not email: raise auth_error.InvalidValidateTokenError() member = await member_ctl.get_member(email=email) if member: raise auth_error.EMailUsedError() member = await member_ctl.create_member(name=form.name, email=email, password=form.password) await auth_web_ctl.create_credential(req, resp, member.id) tasks.add_task(worker.external.bind_member_character, member.id) return BasicResponse()
async def send_external(request: Request, tasks: BackgroundTasks, form: SendExternalForm): if form.external_type != 'QQ': raise auth_error.InvalidExternalTypeError() member = await member_ctl.get_member_by_external(ExternalType.QQ, form.external_id) if member: raise auth_error.ExternalIDUsedError() member_externals = await member_ctl.get_member_externals(request.user.id) if ExternalType.QQ in member_externals: raise auth_error.AlreadyBindExternalError() external_id = form.external_id if not external_id.isdigit(): raise auth_error.InvalidExternalIDError() token = ''.join([str(secrets.choice(range(10))) for _ in range(6)]) email = f'{external_id}@qq.com' await credential_ctl.set_email_validate_token( 'bind_external', email, token=f'{request.user.id}:{token}') tasks.add_task(worker.email.send_bind_external_email, email, token) return BasicResponse()
async def validate_external(tasks: BackgroundTasks, request: Request, form: ValidateExternalForm): if form.external_type != 'QQ': raise auth_error.InvalidExternalTypeError() member = await member_ctl.get_member_by_external(ExternalType.QQ, form.external_id) if member: raise auth_error.ExternalIDUsedError() member_externals = await member_ctl.get_member_externals(request.user.id) if ExternalType.QQ in member_externals: raise auth_error.AlreadyBindExternalError() external_id = form.external_id email = f'{external_id}@qq.com' token = f'{request.user.id}:{form.validate_token}' if await credential_ctl.get_email_validate_token('bind_external', token) != email: raise auth_error.InvalidValidateTokenError(message='验证码输入错误') await member_ctl.bind_member_external_id(request.user.id, ExternalType.QQ, external_id) tasks.add_task(worker.external.bind_member_character, request.user.id) return BasicResponse()
async def update_me_settings(r: Request, settings: MeSettings): member_id = r.user.id setting_dict = settings.dict(exclude_unset=True) await member_ctl.update_member(member_id, **setting_dict) return BasicResponse()