Skip to content

认证系统

SirrChat 提供多种灵活的认证方式,支持从传统密码到现代区块链签名的各种认证机制。

认证方式

区块链认证

使用区块链钱包进行无密码认证。

工作原理

  1. 客户端使用私钥签名一个随机消息
  2. 服务器验证签名并恢复钱包地址
  3. 验证地址与用户账户匹配

配置示例

toml
[auth.blockchain]
enabled = true
networks = ["ethereum", "bsc", "polygon"]

[auth.blockchain.rpc]
ethereum = "https://mainnet.infura.io/v3/YOUR-PROJECT-ID"
bsc = "https://bsc-dataseed.binance.org"
polygon = "https://polygon-rpc.com"

支持的网络

  • Ethereum (ETH)
  • BNB Smart Chain (BSC)
  • Polygon (MATIC)
  • 所有 EVM 兼容链

客户端使用

javascript
// 生成签名
const message = `Login to SirrChat: ${timestamp}`;
const signature = await web3.eth.personal.sign(message, address);

// SMTP AUTH
AUTH BLOCKCHAIN
<address>
<signature>
<message>

LDAP 认证

集成企业 LDAP 目录服务。

配置

toml
[auth.ldap]
enabled = true
server = "ldap://ldap.example.com:389"
bind_dn = "cn=admin,dc=example,dc=com"
bind_password = "password"
user_base = "ou=users,dc=example,dc=com"
user_filter = "(uid={username})"

Active Directory

toml
[auth.ldap]
server = "ldap://dc.example.com:389"
bind_dn = "cn=Administrator,cn=Users,dc=example,dc=com"
bind_password = "password"
user_base = "cn=Users,dc=example,dc=com"
user_filter = "(sAMAccountName={username})"

PAM 认证

使用 Linux 系统账户认证。

配置

toml
[auth.pam]
enabled = true
service = "sirrchat"

PAM 配置文件

创建 /etc/pam.d/sirrchat:

auth       required     pam_unix.so
account    required     pam_unix.so

数据库认证

传统的用户名密码认证。

配置

toml
[auth.database]
enabled = true
password_hash = "bcrypt"  # bcrypt, argon2, scrypt

创建用户

bash
sirrchatd user create \
  --username [email protected] \
  --password secretpassword

多因素认证 (MFA)

TOTP

基于时间的一次性密码。

toml
[auth.mfa]
enabled = true
issuer = "SirrChat"

启用 MFA

bash
sirrchatd mfa enable --user [email protected]

硬件密钥

支持 FIDO2/WebAuthn 硬件密钥。

toml
[auth.mfa.webauthn]
enabled = true
rp_name = "SirrChat Mail Server"

认证协议

SASL 机制

支持的 SASL 认证机制:

  • PLAIN: 明文密码(需 TLS)
  • LOGIN: 登录认证
  • CRAM-MD5: 挑战-响应认证
  • SCRAM-SHA-256: 安全认证
  • BLOCKCHAIN: 自定义区块链认证

配置示例

toml
[auth.sasl]
mechanisms = ["PLAIN", "LOGIN", "BLOCKCHAIN"]
require_tls = true

访问控制

IP 白名单

toml
[auth.access_control]
allowed_ips = ["192.168.1.0/24", "10.0.0.0/8"]

IP 黑名单

toml
[auth.access_control]
blocked_ips = ["203.0.113.0/24"]

地理位置限制

toml
[auth.geo]
enabled = true
allowed_countries = ["US", "GB", "CA"]

会话管理

会话配置

toml
[auth.session]
# 会话超时(秒)
timeout = 3600

# 最大并发会话
max_sessions = 10

# 会话令牌长度
token_length = 32

会话存储

  • 内存: 快速但不持久
  • Redis: 分布式会话管理
  • 数据库: 持久化存储
toml
[auth.session.storage]
type = "redis"
redis_url = "redis://localhost:6379/0"

密码策略

密码要求

toml
[auth.password_policy]
min_length = 12
require_uppercase = true
require_lowercase = true
require_digits = true
require_special = true

密码历史

toml
[auth.password_policy]
remember_count = 5  # 记住最近5个密码
expiry_days = 90    # 90天后过期

安全特性

暴力破解防护

toml
[auth.security]
max_attempts = 5
lockout_duration = 300  # 5分钟

异常检测

toml
[auth.anomaly_detection]
enabled = true
alert_on_new_ip = true
alert_on_new_device = true

审计日志

记录的事件

  • 登录尝试(成功/失败)
  • 密码更改
  • MFA 状态变更
  • 会话创建/销毁

日志格式

json
{
  "timestamp": "2025-01-15T10:30:00Z",
  "event": "login_success",
  "user": "[email protected]",
  "ip": "192.168.1.100",
  "method": "blockchain"
}

API 认证

API 密钥

bash
sirrchatd api-key create --user [email protected]

OAuth 2.0

toml
[auth.oauth]
enabled = true
provider = "custom"
client_id = "sirrchat"
client_secret = "secret"

最佳实践

  1. 始终使用 TLS: 加密传输认证凭据
  2. 启用 MFA: 提高账户安全性
  3. 定期轮换密钥: 更新 API 密钥和密码
  4. 监控异常: 设置告警通知
  5. 最小权限原则: 仅授予必要权限

相关文档:

Released under the GPL 3.0 License.