remove unused method in user login
parent
f2b0af2fb4
commit
bc039efdaf
@ -1,77 +0,0 @@
|
|||||||
import datetime
|
|
||||||
|
|
||||||
from flask import request
|
|
||||||
from flask_restful import Resource, reqparse # type: ignore
|
|
||||||
|
|
||||||
from constants.languages import supported_language
|
|
||||||
from controllers.service_api import api
|
|
||||||
from controllers.service_api.error import AlreadyActivateError
|
|
||||||
from extensions.ext_database import db
|
|
||||||
from libs.helper import StrLen, email, extract_remote_ip, timezone
|
|
||||||
from models.account import AccountStatus
|
|
||||||
from services.account_service import AccountService, RegisterService
|
|
||||||
|
|
||||||
|
|
||||||
class ActivateCheckApi(Resource):
|
|
||||||
def get(self):
|
|
||||||
parser = reqparse.RequestParser()
|
|
||||||
parser.add_argument("workspace_id", type=str, required=False, nullable=True, location="args")
|
|
||||||
parser.add_argument("email", type=email, required=False, nullable=True, location="args")
|
|
||||||
parser.add_argument("token", type=str, required=True, nullable=False, location="args")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
workspaceId = args["workspace_id"]
|
|
||||||
reg_email = args["email"]
|
|
||||||
token = args["token"]
|
|
||||||
|
|
||||||
invitation = RegisterService.get_invitation_if_token_valid(workspaceId, reg_email, token)
|
|
||||||
if invitation:
|
|
||||||
data = invitation.get("data", {})
|
|
||||||
tenant = invitation.get("tenant", None)
|
|
||||||
workspace_name = tenant.name if tenant else None
|
|
||||||
workspace_id = tenant.id if tenant else None
|
|
||||||
invitee_email = data.get("email") if data else None
|
|
||||||
return {
|
|
||||||
"is_valid": invitation is not None,
|
|
||||||
"data": {"workspace_name": workspace_name, "workspace_id": workspace_id, "email": invitee_email},
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
return {"is_valid": False}
|
|
||||||
|
|
||||||
|
|
||||||
class ActivateApi(Resource):
|
|
||||||
def post(self):
|
|
||||||
parser = reqparse.RequestParser()
|
|
||||||
parser.add_argument("workspace_id", type=str, required=False, nullable=True, location="json")
|
|
||||||
parser.add_argument("email", type=email, required=False, nullable=True, location="json")
|
|
||||||
parser.add_argument("token", type=str, required=True, nullable=False, location="json")
|
|
||||||
parser.add_argument("name", type=StrLen(30), required=True, nullable=False, location="json")
|
|
||||||
parser.add_argument(
|
|
||||||
"interface_language", type=supported_language, required=True, nullable=False, location="json"
|
|
||||||
)
|
|
||||||
parser.add_argument("timezone", type=timezone, required=True, nullable=False, location="json")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
invitation = RegisterService.get_invitation_if_token_valid(args["workspace_id"], args["email"], args["token"])
|
|
||||||
if invitation is None:
|
|
||||||
raise AlreadyActivateError()
|
|
||||||
|
|
||||||
RegisterService.revoke_token(args["workspace_id"], args["email"], args["token"])
|
|
||||||
|
|
||||||
account = invitation["account"]
|
|
||||||
account.name = args["name"]
|
|
||||||
|
|
||||||
account.interface_language = args["interface_language"]
|
|
||||||
account.timezone = args["timezone"]
|
|
||||||
account.interface_theme = "light"
|
|
||||||
account.status = AccountStatus.ACTIVE.value
|
|
||||||
account.initialized_at = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
token_pair = AccountService.login(account, ip_address=extract_remote_ip(request))
|
|
||||||
|
|
||||||
return {"result": "success", "data": token_pair.model_dump()}
|
|
||||||
|
|
||||||
|
|
||||||
api.add_resource(ActivateCheckApi, "/activate/check")
|
|
||||||
api.add_resource(ActivateApi, "/activate")
|
|
||||||
@ -1,132 +0,0 @@
|
|||||||
import base64
|
|
||||||
import secrets
|
|
||||||
|
|
||||||
from flask import request
|
|
||||||
from flask_restful import Resource, reqparse # type: ignore
|
|
||||||
|
|
||||||
from constants.languages import languages
|
|
||||||
from controllers.service_api import api
|
|
||||||
from controllers.service_api.auth.error import EmailCodeError, InvalidEmailError, InvalidTokenError, PasswordMismatchError
|
|
||||||
from controllers.service_api.error import AccountInFreezeError, AccountNotFound, EmailSendIpLimitError
|
|
||||||
from events.tenant_event import tenant_was_created
|
|
||||||
from extensions.ext_database import db
|
|
||||||
from libs.helper import email, extract_remote_ip
|
|
||||||
from libs.password import hash_password, valid_password
|
|
||||||
from models.account import Account
|
|
||||||
from services.account_service import AccountService, TenantService
|
|
||||||
from services.errors.account import AccountRegisterError
|
|
||||||
from services.errors.workspace import WorkSpaceNotAllowedCreateError
|
|
||||||
from services.feature_service import FeatureService
|
|
||||||
|
|
||||||
|
|
||||||
class ForgotPasswordSendEmailApi(Resource):
|
|
||||||
def post(self):
|
|
||||||
parser = reqparse.RequestParser()
|
|
||||||
parser.add_argument("email", type=email, required=True, location="json")
|
|
||||||
parser.add_argument("language", type=str, required=False, location="json")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
ip_address = extract_remote_ip(request)
|
|
||||||
if AccountService.is_email_send_ip_limit(ip_address):
|
|
||||||
raise EmailSendIpLimitError()
|
|
||||||
|
|
||||||
if args["language"] is not None and args["language"] == "zh-Hans":
|
|
||||||
language = "zh-Hans"
|
|
||||||
else:
|
|
||||||
language = "en-US"
|
|
||||||
|
|
||||||
account = Account.query.filter_by(email=args["email"]).first()
|
|
||||||
token = None
|
|
||||||
if account is None:
|
|
||||||
if FeatureService.get_system_features().is_allow_register:
|
|
||||||
token = AccountService.send_reset_password_email(email=args["email"], language=language)
|
|
||||||
return {"result": "fail", "data": token, "code": "account_not_found"}
|
|
||||||
else:
|
|
||||||
raise AccountNotFound()
|
|
||||||
else:
|
|
||||||
token = AccountService.send_reset_password_email(account=account, email=args["email"], language=language)
|
|
||||||
|
|
||||||
return {"result": "success", "data": token}
|
|
||||||
|
|
||||||
|
|
||||||
class ForgotPasswordCheckApi(Resource):
|
|
||||||
def post(self):
|
|
||||||
parser = reqparse.RequestParser()
|
|
||||||
parser.add_argument("email", type=str, required=True, location="json")
|
|
||||||
parser.add_argument("code", type=str, required=True, location="json")
|
|
||||||
parser.add_argument("token", type=str, required=True, nullable=False, location="json")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
user_email = args["email"]
|
|
||||||
|
|
||||||
token_data = AccountService.get_reset_password_data(args["token"])
|
|
||||||
if token_data is None:
|
|
||||||
raise InvalidTokenError()
|
|
||||||
|
|
||||||
if user_email != token_data.get("email"):
|
|
||||||
raise InvalidEmailError()
|
|
||||||
|
|
||||||
if args["code"] != token_data.get("code"):
|
|
||||||
raise EmailCodeError()
|
|
||||||
|
|
||||||
return {"is_valid": True, "email": token_data.get("email")}
|
|
||||||
|
|
||||||
|
|
||||||
class ForgotPasswordResetApi(Resource):
|
|
||||||
def post(self):
|
|
||||||
parser = reqparse.RequestParser()
|
|
||||||
parser.add_argument("token", type=str, required=True, nullable=False, location="json")
|
|
||||||
parser.add_argument("new_password", type=valid_password, required=True, nullable=False, location="json")
|
|
||||||
parser.add_argument("password_confirm", type=valid_password, required=True, nullable=False, location="json")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
new_password = args["new_password"]
|
|
||||||
password_confirm = args["password_confirm"]
|
|
||||||
|
|
||||||
if str(new_password).strip() != str(password_confirm).strip():
|
|
||||||
raise PasswordMismatchError()
|
|
||||||
|
|
||||||
token = args["token"]
|
|
||||||
reset_data = AccountService.get_reset_password_data(token)
|
|
||||||
|
|
||||||
if reset_data is None:
|
|
||||||
raise InvalidTokenError()
|
|
||||||
|
|
||||||
AccountService.revoke_reset_password_token(token)
|
|
||||||
|
|
||||||
salt = secrets.token_bytes(16)
|
|
||||||
base64_salt = base64.b64encode(salt).decode()
|
|
||||||
|
|
||||||
password_hashed = hash_password(new_password, salt)
|
|
||||||
base64_password_hashed = base64.b64encode(password_hashed).decode()
|
|
||||||
|
|
||||||
account = Account.query.filter_by(email=reset_data.get("email")).first()
|
|
||||||
if account:
|
|
||||||
account.password = base64_password_hashed
|
|
||||||
account.password_salt = base64_salt
|
|
||||||
db.session.commit()
|
|
||||||
tenant = TenantService.get_join_tenants(account)
|
|
||||||
if not tenant and not FeatureService.get_system_features().is_allow_create_workspace:
|
|
||||||
tenant = TenantService.create_tenant(f"{account.name}'s Workspace")
|
|
||||||
TenantService.create_tenant_member(tenant, account, role="owner")
|
|
||||||
account.current_tenant = tenant
|
|
||||||
tenant_was_created.send(tenant)
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
account = AccountService.create_account_and_tenant(
|
|
||||||
email=reset_data.get("email", ""),
|
|
||||||
name=reset_data.get("email", ""),
|
|
||||||
password=password_confirm,
|
|
||||||
interface_language=languages[0],
|
|
||||||
)
|
|
||||||
except WorkSpaceNotAllowedCreateError:
|
|
||||||
pass
|
|
||||||
except AccountRegisterError as are:
|
|
||||||
raise AccountInFreezeError()
|
|
||||||
|
|
||||||
return {"result": "success"}
|
|
||||||
|
|
||||||
|
|
||||||
api.add_resource(ForgotPasswordSendEmailApi, "/forgot-password")
|
|
||||||
api.add_resource(ForgotPasswordCheckApi, "/forgot-password/validity")
|
|
||||||
api.add_resource(ForgotPasswordResetApi, "/forgot-password/resets")
|
|
||||||
@ -1,122 +0,0 @@
|
|||||||
from flask import request
|
|
||||||
from flask_restful import Resource, reqparse # type: ignore
|
|
||||||
|
|
||||||
from configs import dify_config
|
|
||||||
from constants.languages import languages
|
|
||||||
from controllers.service_api import api
|
|
||||||
from controllers.service_api.error import (
|
|
||||||
AccountBannedError,
|
|
||||||
AccountInFreezeError,
|
|
||||||
EmailSendIpLimitError,
|
|
||||||
NotAllowedCreateWorkspace,
|
|
||||||
)
|
|
||||||
from events.tenant_event import tenant_was_created
|
|
||||||
from libs.helper import email, extract_remote_ip
|
|
||||||
from libs.password import valid_password
|
|
||||||
from services.account_service import AccountService, TenantService
|
|
||||||
from services.billing_service import BillingService
|
|
||||||
from services.errors.account import AccountRegisterError
|
|
||||||
from services.errors.workspace import WorkSpaceNotAllowedCreateError
|
|
||||||
from services.feature_service import FeatureService
|
|
||||||
|
|
||||||
|
|
||||||
class SignupApi(Resource):
|
|
||||||
def post(self):
|
|
||||||
"""User Signup API endpoint
|
|
||||||
This endpoint handles new user registration.
|
|
||||||
---
|
|
||||||
parameters:
|
|
||||||
- name: body
|
|
||||||
in: body
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/SignupRequest'
|
|
||||||
definitions:
|
|
||||||
SignupRequest:
|
|
||||||
type: object
|
|
||||||
required:
|
|
||||||
- email
|
|
||||||
- password
|
|
||||||
- name
|
|
||||||
properties:
|
|
||||||
email:
|
|
||||||
type: string
|
|
||||||
format: email
|
|
||||||
example: user@example.com
|
|
||||||
password:
|
|
||||||
type: string
|
|
||||||
format: password
|
|
||||||
example: StrongP@ssw0rd
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
example: John Doe
|
|
||||||
language:
|
|
||||||
type: string
|
|
||||||
default: en-US
|
|
||||||
example: en-US
|
|
||||||
TokenPair:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
access_token:
|
|
||||||
type: string
|
|
||||||
refresh_token:
|
|
||||||
type: string
|
|
||||||
responses:
|
|
||||||
200:
|
|
||||||
description: Successfully registered and logged in
|
|
||||||
schema:
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
result:
|
|
||||||
type: string
|
|
||||||
example: success
|
|
||||||
data:
|
|
||||||
$ref: '#/definitions/TokenPair'
|
|
||||||
400:
|
|
||||||
description: Registration failed due to validation errors
|
|
||||||
403:
|
|
||||||
description: Registration not allowed or account banned
|
|
||||||
"""
|
|
||||||
parser = reqparse.RequestParser()
|
|
||||||
parser.add_argument("email", type=email, required=True, location="json")
|
|
||||||
parser.add_argument("password", type=valid_password, required=True, location="json")
|
|
||||||
parser.add_argument("name", type=str, required=True, location="json")
|
|
||||||
parser.add_argument("language", type=str, required=False, default="en-US", location="json")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
if dify_config.BILLING_ENABLED and BillingService.is_email_in_freeze(args["email"]):
|
|
||||||
raise AccountInFreezeError()
|
|
||||||
|
|
||||||
if not FeatureService.get_system_features().is_allow_register:
|
|
||||||
raise NotAllowedCreateWorkspace()
|
|
||||||
|
|
||||||
ip_address = extract_remote_ip(request)
|
|
||||||
if AccountService.is_email_send_ip_limit(ip_address):
|
|
||||||
raise EmailSendIpLimitError()
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Create account and tenant
|
|
||||||
account = AccountService.create_account_and_tenant(
|
|
||||||
email=args["email"],
|
|
||||||
name=args["name"],
|
|
||||||
password=args["password"],
|
|
||||||
interface_language=args["language"] if args["language"] in languages else languages[0]
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create default tenant
|
|
||||||
tenant = TenantService.create_tenant(f"{account.name}'s Workspace")
|
|
||||||
TenantService.create_tenant_member(tenant, account, role="owner")
|
|
||||||
account.current_tenant = tenant
|
|
||||||
tenant_was_created.send(tenant)
|
|
||||||
|
|
||||||
# Login the user
|
|
||||||
token_pair = AccountService.login(account=account, ip_address=ip_address)
|
|
||||||
return {"result": "success", "data": token_pair.model_dump()}
|
|
||||||
|
|
||||||
except WorkSpaceNotAllowedCreateError:
|
|
||||||
raise NotAllowedCreateWorkspace()
|
|
||||||
except AccountRegisterError:
|
|
||||||
raise AccountBannedError()
|
|
||||||
|
|
||||||
|
|
||||||
api.add_resource(SignupApi, "/signup")
|
|
||||||
Loading…
Reference in New Issue