Compare commits
4
Commits
83bd9f93a5
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ea8eff34d | ||
|
|
7e7afe3907 | ||
|
|
4276015cf3 | ||
|
|
59ac17e609 |
@@ -83,7 +83,8 @@ ai_configs = {
|
|||||||
},
|
},
|
||||||
'bailian-embedding': {
|
'bailian-embedding': {
|
||||||
'model': 'text-embedding-v4',
|
'model': 'text-embedding-v4',
|
||||||
'url': 'https://llm-z0ugjqj2yf8a7i89.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/embeddings',
|
# 'url': 'https://llm-z0ugjqj2yf8a7i89.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/embeddings',
|
||||||
|
"url": "https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings",
|
||||||
'key': env.llm_key_bailian,
|
'key': env.llm_key_bailian,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-30
@@ -1,48 +1,57 @@
|
|||||||
from datetime import timedelta, datetime, timezone
|
from datetime import timedelta, datetime, timezone
|
||||||
from typing import TypedDict, Literal, TypeAlias
|
from typing import TypedDict, Literal, TypeAlias
|
||||||
|
|
||||||
|
import bcrypt
|
||||||
import jwt
|
import jwt
|
||||||
from passlib.context import CryptContext
|
|
||||||
|
|
||||||
from app.config.env import env
|
from app.config.env import env
|
||||||
|
|
||||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
BCRYPT_MAX_BYTES = 72
|
||||||
|
|
||||||
# token的类型,access用于接口认证,refresh用于刷新access token,verify用于激活用户账号
|
|
||||||
AccessTokenType: TypeAlias = Literal["access", "refresh", "verify", "api"]
|
AccessTokenType: TypeAlias = Literal["access", "refresh", "verify", "api"]
|
||||||
|
|
||||||
|
|
||||||
class TokenInfo(TypedDict):
|
class TokenInfo(TypedDict):
|
||||||
# 用户名信息
|
username: str
|
||||||
username: str
|
exp: datetime
|
||||||
# token过期时间
|
type: AccessTokenType
|
||||||
exp: datetime
|
|
||||||
type: AccessTokenType
|
|
||||||
|
def _safe_password_bytes(password: str) -> bytes:
|
||||||
|
data = password.encode("utf-8")
|
||||||
|
if len(data) > BCRYPT_MAX_BYTES:
|
||||||
|
data = data[:BCRYPT_MAX_BYTES]
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
class CryptUtils:
|
class CryptUtils:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_password_hash(password: str):
|
def get_password_hash(password: str):
|
||||||
return pwd_context.hash(password)
|
return bcrypt.hashpw(_safe_password_bytes(password), bcrypt.gensalt()).decode(
|
||||||
|
"utf-8"
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def verify_password(plain_password: str, hashed_password: str):
|
def verify_password(plain_password: str, hashed_password: str):
|
||||||
return pwd_context.verify(plain_password, hashed_password)
|
try:
|
||||||
|
return bcrypt.checkpw(
|
||||||
|
_safe_password_bytes(plain_password), hashed_password.encode("utf-8")
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_token(
|
def create_token(username: str, type: AccessTokenType, expires_delta: timedelta):
|
||||||
username: str,
|
data: TokenInfo = {
|
||||||
type: AccessTokenType,
|
"username": username,
|
||||||
expires_delta: timedelta
|
"type": type,
|
||||||
):
|
"exp": datetime.now(timezone.utc) + expires_delta,
|
||||||
data: TokenInfo = {
|
}
|
||||||
"username": username,
|
return jwt.encode(data, env.jwt_secret_key, env.jwt_algorithm)
|
||||||
"type": type,
|
|
||||||
"exp": datetime.now(timezone.utc) + expires_delta
|
|
||||||
}
|
|
||||||
return jwt.encode(data, env.jwt_secret_key, env.jwt_algorithm)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_token_info(token: str) -> TokenInfo:
|
def get_token_info(token: str) -> TokenInfo:
|
||||||
data = jwt.decode(token, env.jwt_secret_key, algorithms=[env.jwt_algorithm])
|
data = jwt.decode(token, env.jwt_secret_key, algorithms=[env.jwt_algorithm])
|
||||||
return data
|
return data
|
||||||
|
|||||||
Generated
+3967
-2984
File diff suppressed because it is too large
Load Diff
+3
-7
@@ -20,7 +20,8 @@ psutil = "^7.0.0"
|
|||||||
asyncmy = "^0.2.10"
|
asyncmy = "^0.2.10"
|
||||||
sqlmodel = "^0.0.24"
|
sqlmodel = "^0.0.24"
|
||||||
greenlet = "^3.2.3"
|
greenlet = "^3.2.3"
|
||||||
passlib = {extras = ["bcrypt"], version = "^1.7.4"}
|
passlib = {extras = ["bcrypt"], version = "^1.7.4", optional = true}
|
||||||
|
bcrypt = "^4.0.0"
|
||||||
pyjwt = "^2.10.1"
|
pyjwt = "^2.10.1"
|
||||||
python-multipart = "^0.0.20"
|
python-multipart = "^0.0.20"
|
||||||
langgraph = "^0.6.3"
|
langgraph = "^0.6.3"
|
||||||
@@ -40,15 +41,10 @@ psycopg = {extras = ["binary"], version = "^3.3.3"}
|
|||||||
langchain-cli = ">=0.0.15"
|
langchain-cli = ">=0.0.15"
|
||||||
|
|
||||||
# 配置国内镜像源
|
# 配置国内镜像源
|
||||||
[[tool.poetry.source]]
|
|
||||||
name = "tsinghua"
|
|
||||||
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
|
|
||||||
priority = "primary" # 最高优先级
|
|
||||||
|
|
||||||
[[tool.poetry.source]]
|
[[tool.poetry.source]]
|
||||||
name = "aliyun"
|
name = "aliyun"
|
||||||
url = "https://mirrors.aliyun.com/pypi/simple/"
|
url = "https://mirrors.aliyun.com/pypi/simple/"
|
||||||
priority = "supplemental" # 次级优先级
|
priority = "primary" # 最高优先级
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["poetry-core"]
|
requires = ["poetry-core"]
|
||||||
|
|||||||
Reference in New Issue
Block a user