mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-05-24 05:28:43 +08:00

Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: GareArc <chen4851@purdue.edu> Co-authored-by: Byron.wang <byron@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Garfield Dai <dai.hai@foxmail.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: Alexi.F <654973939@qq.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com> Co-authored-by: kautsar_masuara <61046989+izon-masuara@users.noreply.github.com> Co-authored-by: achmad-kautsar <achmad.kautsar@insignia.co.id> Co-authored-by: Xin Zhang <sjhpzx@gmail.com> Co-authored-by: kelvintsim <83445753+kelvintsim@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Zixuan Cheng <61724187+Theysua@users.noreply.github.com>
96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
import uuid
|
|
|
|
from flask import request
|
|
from flask_restful import Resource
|
|
from werkzeug.exceptions import NotFound, Unauthorized
|
|
|
|
from controllers.web import api
|
|
from controllers.web.error import WebAppAuthRequiredError
|
|
from extensions.ext_database import db
|
|
from libs.passport import PassportService
|
|
from models.model import App, EndUser, Site
|
|
from services.enterprise.enterprise_service import EnterpriseService
|
|
from services.feature_service import FeatureService
|
|
|
|
|
|
class PassportResource(Resource):
|
|
"""Base resource for passport."""
|
|
|
|
def get(self):
|
|
system_features = FeatureService.get_system_features()
|
|
app_code = request.headers.get("X-App-Code")
|
|
user_id = request.args.get("user_id")
|
|
|
|
if app_code is None:
|
|
raise Unauthorized("X-App-Code header is missing.")
|
|
|
|
if system_features.webapp_auth.enabled:
|
|
app_settings = EnterpriseService.WebAppAuth.get_app_access_mode_by_code(app_code=app_code)
|
|
if not app_settings or not app_settings.access_mode == "public":
|
|
raise WebAppAuthRequiredError()
|
|
|
|
# get site from db and check if it is normal
|
|
site = db.session.query(Site).filter(Site.code == app_code, Site.status == "normal").first()
|
|
if not site:
|
|
raise NotFound()
|
|
# get app from db and check if it is normal and enable_site
|
|
app_model = db.session.query(App).filter(App.id == site.app_id).first()
|
|
if not app_model or app_model.status != "normal" or not app_model.enable_site:
|
|
raise NotFound()
|
|
|
|
if user_id:
|
|
end_user = (
|
|
db.session.query(EndUser).filter(EndUser.app_id == app_model.id, EndUser.session_id == user_id).first()
|
|
)
|
|
|
|
if end_user:
|
|
pass
|
|
else:
|
|
end_user = EndUser(
|
|
tenant_id=app_model.tenant_id,
|
|
app_id=app_model.id,
|
|
type="browser",
|
|
is_anonymous=True,
|
|
session_id=user_id,
|
|
)
|
|
db.session.add(end_user)
|
|
db.session.commit()
|
|
else:
|
|
end_user = EndUser(
|
|
tenant_id=app_model.tenant_id,
|
|
app_id=app_model.id,
|
|
type="browser",
|
|
is_anonymous=True,
|
|
session_id=generate_session_id(),
|
|
)
|
|
db.session.add(end_user)
|
|
db.session.commit()
|
|
|
|
payload = {
|
|
"iss": site.app_id,
|
|
"sub": "Web API Passport",
|
|
"app_id": site.app_id,
|
|
"app_code": app_code,
|
|
"end_user_id": end_user.id,
|
|
}
|
|
|
|
tk = PassportService().issue(payload)
|
|
|
|
return {
|
|
"access_token": tk,
|
|
}
|
|
|
|
|
|
api.add_resource(PassportResource, "/passport")
|
|
|
|
|
|
def generate_session_id():
|
|
"""
|
|
Generate a unique session ID.
|
|
"""
|
|
while True:
|
|
session_id = str(uuid.uuid4())
|
|
existing_count = db.session.query(EndUser).filter(EndUser.session_id == session_id).count()
|
|
if existing_count == 0:
|
|
return session_id
|