- 新增图像生成接口,支持试用、积分和自定义API Key模式 - 实现生成图片结果异步上传至MinIO存储,带重试机制 - 优化积分预扣除和异常退还逻辑,保障用户积分准确 - 添加获取生成历史记录接口,支持时间范围和分页 - 提供本地字典配置接口,支持模型、比例、提示模板和尺寸 - 实现图片批量上传接口,支持S3兼容对象存储 feat(admin): 增加管理员角色管理与权限分配接口 - 实现角色列表查询、角色创建、更新及删除功能 - 增加权限列表查询接口 - 实现用户角色分配接口,便于统一管理用户权限 - 增加系统字典增删查改接口,支持分类过滤和排序 - 权限控制全面覆盖管理接口,保证安全访问 feat(auth): 完善用户登录注册及权限相关接口与页面 - 实现手机号验证码发送及校验功能,保障注册安全 - 支持手机号注册、登录及退出接口,集成日志记录 - 增加修改密码功能,验证原密码后更新 - 提供动态导航菜单接口,基于权限展示不同菜单 - 实现管理界面路由及日志、角色、字典管理页面访问权限控制 - 添加系统日志查询接口,支持关键词和等级筛选 feat(app): 初始化Flask应用并配置蓝图与数据库 - 创建应用程序工厂,加载配置,初始化数据库和Redis客户端 - 注册认证、API及管理员蓝图,整合路由 - 根路由渲染主页模板 - 应用上下文中自动创建数据库表,保证运行环境准备完毕 feat(database): 提供数据库创建与迁移支持脚本 - 新增数据库创建脚本,支持自动检测是否已存在 - 添加数据库表初始化脚本,支持创建和删除所有表 - 实现RBAC权限初始化,包含基础权限和角色创建 - 新增字段手动修复脚本,添加用户API Key和积分字段 - 强制迁移脚本支持清理连接和修复表结构,初始化默认数据及角色分配 feat(config): 新增系统配置参数 - 配置数据库、Redis、Session和MinIO相关参数 - 添加AI接口地址及试用Key配置 - 集成阿里云短信服务配置及开发模式相关参数 feat(extensions): 初始化数据库、Redis和MinIO客户端 - 创建全局SQLAlchemy数据库实例和Redis客户端 - 配置基于boto3的MinIO兼容S3客户端 chore(logs): 添加示例系统日志文件 - 记录用户请求、验证码发送成功与失败的日志信息
315 lines
9.9 KiB
Python
315 lines
9.9 KiB
Python
"""
|
|
requests.auth
|
|
~~~~~~~~~~~~~
|
|
|
|
This module contains the authentication handlers for Requests.
|
|
"""
|
|
|
|
import hashlib
|
|
import os
|
|
import re
|
|
import threading
|
|
import time
|
|
import warnings
|
|
from base64 import b64encode
|
|
|
|
from ._internal_utils import to_native_string
|
|
from .compat import basestring, str, urlparse
|
|
from .cookies import extract_cookies_to_jar
|
|
from .utils import parse_dict_header
|
|
|
|
CONTENT_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded"
|
|
CONTENT_TYPE_MULTI_PART = "multipart/form-data"
|
|
|
|
|
|
def _basic_auth_str(username, password):
|
|
"""Returns a Basic Auth string."""
|
|
|
|
# "I want us to put a big-ol' comment on top of it that
|
|
# says that this behaviour is dumb but we need to preserve
|
|
# it because people are relying on it."
|
|
# - Lukasa
|
|
#
|
|
# These are here solely to maintain backwards compatibility
|
|
# for things like ints. This will be removed in 3.0.0.
|
|
if not isinstance(username, basestring):
|
|
warnings.warn(
|
|
"Non-string usernames will no longer be supported in Requests "
|
|
"3.0.0. Please convert the object you've passed in ({!r}) to "
|
|
"a string or bytes object in the near future to avoid "
|
|
"problems.".format(username),
|
|
category=DeprecationWarning,
|
|
)
|
|
username = str(username)
|
|
|
|
if not isinstance(password, basestring):
|
|
warnings.warn(
|
|
"Non-string passwords will no longer be supported in Requests "
|
|
"3.0.0. Please convert the object you've passed in ({!r}) to "
|
|
"a string or bytes object in the near future to avoid "
|
|
"problems.".format(type(password)),
|
|
category=DeprecationWarning,
|
|
)
|
|
password = str(password)
|
|
# -- End Removal --
|
|
|
|
if isinstance(username, str):
|
|
username = username.encode("latin1")
|
|
|
|
if isinstance(password, str):
|
|
password = password.encode("latin1")
|
|
|
|
authstr = "Basic " + to_native_string(
|
|
b64encode(b":".join((username, password))).strip()
|
|
)
|
|
|
|
return authstr
|
|
|
|
|
|
class AuthBase:
|
|
"""Base class that all auth implementations derive from"""
|
|
|
|
def __call__(self, r):
|
|
raise NotImplementedError("Auth hooks must be callable.")
|
|
|
|
|
|
class HTTPBasicAuth(AuthBase):
|
|
"""Attaches HTTP Basic Authentication to the given Request object."""
|
|
|
|
def __init__(self, username, password):
|
|
self.username = username
|
|
self.password = password
|
|
|
|
def __eq__(self, other):
|
|
return all(
|
|
[
|
|
self.username == getattr(other, "username", None),
|
|
self.password == getattr(other, "password", None),
|
|
]
|
|
)
|
|
|
|
def __ne__(self, other):
|
|
return not self == other
|
|
|
|
def __call__(self, r):
|
|
r.headers["Authorization"] = _basic_auth_str(self.username, self.password)
|
|
return r
|
|
|
|
|
|
class HTTPProxyAuth(HTTPBasicAuth):
|
|
"""Attaches HTTP Proxy Authentication to a given Request object."""
|
|
|
|
def __call__(self, r):
|
|
r.headers["Proxy-Authorization"] = _basic_auth_str(self.username, self.password)
|
|
return r
|
|
|
|
|
|
class HTTPDigestAuth(AuthBase):
|
|
"""Attaches HTTP Digest Authentication to the given Request object."""
|
|
|
|
def __init__(self, username, password):
|
|
self.username = username
|
|
self.password = password
|
|
# Keep state in per-thread local storage
|
|
self._thread_local = threading.local()
|
|
|
|
def init_per_thread_state(self):
|
|
# Ensure state is initialized just once per-thread
|
|
if not hasattr(self._thread_local, "init"):
|
|
self._thread_local.init = True
|
|
self._thread_local.last_nonce = ""
|
|
self._thread_local.nonce_count = 0
|
|
self._thread_local.chal = {}
|
|
self._thread_local.pos = None
|
|
self._thread_local.num_401_calls = None
|
|
|
|
def build_digest_header(self, method, url):
|
|
"""
|
|
:rtype: str
|
|
"""
|
|
|
|
realm = self._thread_local.chal["realm"]
|
|
nonce = self._thread_local.chal["nonce"]
|
|
qop = self._thread_local.chal.get("qop")
|
|
algorithm = self._thread_local.chal.get("algorithm")
|
|
opaque = self._thread_local.chal.get("opaque")
|
|
hash_utf8 = None
|
|
|
|
if algorithm is None:
|
|
_algorithm = "MD5"
|
|
else:
|
|
_algorithm = algorithm.upper()
|
|
# lambdas assume digest modules are imported at the top level
|
|
if _algorithm == "MD5" or _algorithm == "MD5-SESS":
|
|
|
|
def md5_utf8(x):
|
|
if isinstance(x, str):
|
|
x = x.encode("utf-8")
|
|
return hashlib.md5(x).hexdigest()
|
|
|
|
hash_utf8 = md5_utf8
|
|
elif _algorithm == "SHA":
|
|
|
|
def sha_utf8(x):
|
|
if isinstance(x, str):
|
|
x = x.encode("utf-8")
|
|
return hashlib.sha1(x).hexdigest()
|
|
|
|
hash_utf8 = sha_utf8
|
|
elif _algorithm == "SHA-256":
|
|
|
|
def sha256_utf8(x):
|
|
if isinstance(x, str):
|
|
x = x.encode("utf-8")
|
|
return hashlib.sha256(x).hexdigest()
|
|
|
|
hash_utf8 = sha256_utf8
|
|
elif _algorithm == "SHA-512":
|
|
|
|
def sha512_utf8(x):
|
|
if isinstance(x, str):
|
|
x = x.encode("utf-8")
|
|
return hashlib.sha512(x).hexdigest()
|
|
|
|
hash_utf8 = sha512_utf8
|
|
|
|
KD = lambda s, d: hash_utf8(f"{s}:{d}") # noqa:E731
|
|
|
|
if hash_utf8 is None:
|
|
return None
|
|
|
|
# XXX not implemented yet
|
|
entdig = None
|
|
p_parsed = urlparse(url)
|
|
#: path is request-uri defined in RFC 2616 which should not be empty
|
|
path = p_parsed.path or "/"
|
|
if p_parsed.query:
|
|
path += f"?{p_parsed.query}"
|
|
|
|
A1 = f"{self.username}:{realm}:{self.password}"
|
|
A2 = f"{method}:{path}"
|
|
|
|
HA1 = hash_utf8(A1)
|
|
HA2 = hash_utf8(A2)
|
|
|
|
if nonce == self._thread_local.last_nonce:
|
|
self._thread_local.nonce_count += 1
|
|
else:
|
|
self._thread_local.nonce_count = 1
|
|
ncvalue = f"{self._thread_local.nonce_count:08x}"
|
|
s = str(self._thread_local.nonce_count).encode("utf-8")
|
|
s += nonce.encode("utf-8")
|
|
s += time.ctime().encode("utf-8")
|
|
s += os.urandom(8)
|
|
|
|
cnonce = hashlib.sha1(s).hexdigest()[:16]
|
|
if _algorithm == "MD5-SESS":
|
|
HA1 = hash_utf8(f"{HA1}:{nonce}:{cnonce}")
|
|
|
|
if not qop:
|
|
respdig = KD(HA1, f"{nonce}:{HA2}")
|
|
elif qop == "auth" or "auth" in qop.split(","):
|
|
noncebit = f"{nonce}:{ncvalue}:{cnonce}:auth:{HA2}"
|
|
respdig = KD(HA1, noncebit)
|
|
else:
|
|
# XXX handle auth-int.
|
|
return None
|
|
|
|
self._thread_local.last_nonce = nonce
|
|
|
|
# XXX should the partial digests be encoded too?
|
|
base = (
|
|
f'username="{self.username}", realm="{realm}", nonce="{nonce}", '
|
|
f'uri="{path}", response="{respdig}"'
|
|
)
|
|
if opaque:
|
|
base += f', opaque="{opaque}"'
|
|
if algorithm:
|
|
base += f', algorithm="{algorithm}"'
|
|
if entdig:
|
|
base += f', digest="{entdig}"'
|
|
if qop:
|
|
base += f', qop="auth", nc={ncvalue}, cnonce="{cnonce}"'
|
|
|
|
return f"Digest {base}"
|
|
|
|
def handle_redirect(self, r, **kwargs):
|
|
"""Reset num_401_calls counter on redirects."""
|
|
if r.is_redirect:
|
|
self._thread_local.num_401_calls = 1
|
|
|
|
def handle_401(self, r, **kwargs):
|
|
"""
|
|
Takes the given response and tries digest-auth, if needed.
|
|
|
|
:rtype: requests.Response
|
|
"""
|
|
|
|
# If response is not 4xx, do not auth
|
|
# See https://github.com/psf/requests/issues/3772
|
|
if not 400 <= r.status_code < 500:
|
|
self._thread_local.num_401_calls = 1
|
|
return r
|
|
|
|
if self._thread_local.pos is not None:
|
|
# Rewind the file position indicator of the body to where
|
|
# it was to resend the request.
|
|
r.request.body.seek(self._thread_local.pos)
|
|
s_auth = r.headers.get("www-authenticate", "")
|
|
|
|
if "digest" in s_auth.lower() and self._thread_local.num_401_calls < 2:
|
|
self._thread_local.num_401_calls += 1
|
|
pat = re.compile(r"digest ", flags=re.IGNORECASE)
|
|
self._thread_local.chal = parse_dict_header(pat.sub("", s_auth, count=1))
|
|
|
|
# Consume content and release the original connection
|
|
# to allow our new request to reuse the same one.
|
|
r.content
|
|
r.close()
|
|
prep = r.request.copy()
|
|
extract_cookies_to_jar(prep._cookies, r.request, r.raw)
|
|
prep.prepare_cookies(prep._cookies)
|
|
|
|
prep.headers["Authorization"] = self.build_digest_header(
|
|
prep.method, prep.url
|
|
)
|
|
_r = r.connection.send(prep, **kwargs)
|
|
_r.history.append(r)
|
|
_r.request = prep
|
|
|
|
return _r
|
|
|
|
self._thread_local.num_401_calls = 1
|
|
return r
|
|
|
|
def __call__(self, r):
|
|
# Initialize per-thread state, if needed
|
|
self.init_per_thread_state()
|
|
# If we have a saved nonce, skip the 401
|
|
if self._thread_local.last_nonce:
|
|
r.headers["Authorization"] = self.build_digest_header(r.method, r.url)
|
|
try:
|
|
self._thread_local.pos = r.body.tell()
|
|
except AttributeError:
|
|
# In the case of HTTPDigestAuth being reused and the body of
|
|
# the previous request was a file-like object, pos has the
|
|
# file position of the previous body. Ensure it's set to
|
|
# None.
|
|
self._thread_local.pos = None
|
|
r.register_hook("response", self.handle_401)
|
|
r.register_hook("response", self.handle_redirect)
|
|
self._thread_local.num_401_calls = 1
|
|
|
|
return r
|
|
|
|
def __eq__(self, other):
|
|
return all(
|
|
[
|
|
self.username == getattr(other, "username", None),
|
|
self.password == getattr(other, "password", None),
|
|
]
|
|
)
|
|
|
|
def __ne__(self, other):
|
|
return not self == other
|