更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -8,20 +8,17 @@ import (
|
||||
"server/models"
|
||||
)
|
||||
|
||||
// 默认会话配置(租户未配置 yz_auth_tenant_auth_config 时使用)
|
||||
const (
|
||||
DefaultSessionTTL = 7200 // 会话有效期(秒)
|
||||
DefaultMaxSession = 1 // 默认 1 号 1 机
|
||||
DefaultSessionTTL = 7200
|
||||
DefaultMaxSession = 1
|
||||
)
|
||||
|
||||
// TenantSessionPolicy 租户会话策略
|
||||
type TenantSessionPolicy struct {
|
||||
SessionTTL int
|
||||
MaxSession int
|
||||
KickStrategy int8
|
||||
}
|
||||
|
||||
// GetTenantSessionPolicy 读取租户登录策略,未配置时返回默认值
|
||||
func GetTenantSessionPolicy(tid uint64) TenantSessionPolicy {
|
||||
policy := TenantSessionPolicy{
|
||||
SessionTTL: DefaultSessionTTL,
|
||||
@@ -45,7 +42,6 @@ func GetTenantSessionPolicy(tid uint64) TenantSessionPolicy {
|
||||
return policy
|
||||
}
|
||||
|
||||
// SessionInfo 创建会话的入参
|
||||
type SessionInfo struct {
|
||||
IdentityID uint64
|
||||
Tid uint64
|
||||
@@ -58,15 +54,9 @@ type SessionInfo struct {
|
||||
Amr string
|
||||
}
|
||||
|
||||
// CreateSession 创建会话并执行并发控制。
|
||||
//
|
||||
// 并发策略(租户可配):
|
||||
// - KickStrategyKickOld(默认):超出上限时踢掉最旧的会话(1号1机)
|
||||
// - KickStrategyReject:超出上限时拒绝新登录
|
||||
func CreateSession(info SessionInfo) (*models.AuthSession, error) {
|
||||
policy := GetTenantSessionPolicy(info.Tid)
|
||||
|
||||
// 已占用的活跃会话
|
||||
var actives []models.AuthSession
|
||||
if _, err := models.Orm.QueryTable(new(models.AuthSession)).
|
||||
Filter("identity_id", info.IdentityID).
|
||||
@@ -75,7 +65,6 @@ func CreateSession(info SessionInfo) (*models.AuthSession, error) {
|
||||
All(&actives); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 过期会话先作废,不计入占用
|
||||
now := time.Now()
|
||||
valid := make([]models.AuthSession, 0, len(actives))
|
||||
for _, s := range actives {
|
||||
@@ -90,7 +79,6 @@ func CreateSession(info SessionInfo) (*models.AuthSession, error) {
|
||||
if policy.KickStrategy == models.KickStrategyReject {
|
||||
return nil, ErrSessionLimitExceeded
|
||||
}
|
||||
// 踢掉最旧的,直到腾出名额
|
||||
kick := len(valid) - policy.MaxSession + 1
|
||||
for i := 0; i < kick && i < len(valid); i++ {
|
||||
_ = RevokeSession(valid[i].Sid, models.RevokeReasonKicked)
|
||||
@@ -133,14 +121,6 @@ func CreateSession(info SessionInfo) (*models.AuthSession, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// ReuseSession 授权码换令牌时复用登录阶段已经建立的会话。
|
||||
//
|
||||
// authorize 阶段已经用 yz_sid Cookie 校验过登录态,换码只是给这个已登录会话
|
||||
// 签发令牌,不应再 CreateSession:默认「1 号 1 机」策略会把刚登录的会话踢成
|
||||
// revoked,而浏览器 Cookie 里仍是旧 sid,之后任何一次回到认证中心都会被判定
|
||||
// 未登录并重新显示登录页(表现为「登录成功后又跳回登录界面」)。
|
||||
//
|
||||
// 找不到有效会话(过期、被下线等)时才回退到新建会话。
|
||||
func ReuseSession(info SessionInfo) (*models.AuthSession, error) {
|
||||
var list []models.AuthSession
|
||||
_, err := models.Orm.QueryTable(new(models.AuthSession)).
|
||||
@@ -156,8 +136,6 @@ func ReuseSession(info SessionInfo) (*models.AuthSession, error) {
|
||||
if !list[i].ExpiresAt.After(now) {
|
||||
continue
|
||||
}
|
||||
// 优先复用同一应用的登录会话;同一浏览器会话被多个应用共享时,
|
||||
// client_id 可能为空(直接访问登录页),此时复用最近的有效会话。
|
||||
if list[i].ClientID == info.ClientID {
|
||||
_ = TouchSession(list[i].Sid)
|
||||
return &list[i], nil
|
||||
@@ -174,7 +152,6 @@ func ReuseSession(info SessionInfo) (*models.AuthSession, error) {
|
||||
return CreateSession(info)
|
||||
}
|
||||
|
||||
// GetSession 查询有效会话(已吊销或已过期返回错误)
|
||||
func GetSession(sid string) (*models.AuthSession, error) {
|
||||
var s models.AuthSession
|
||||
if err := models.Orm.QueryTable(new(models.AuthSession)).Filter("sid", sid).One(&s); err != nil {
|
||||
@@ -189,7 +166,6 @@ func GetSession(sid string) (*models.AuthSession, error) {
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// TouchSession 更新会话最近访问时间(建议每 5~10 分钟一次,避免高频写库)
|
||||
func TouchSession(sid string) error {
|
||||
last := time.Now()
|
||||
_, err := models.Orm.QueryTable(new(models.AuthSession)).
|
||||
@@ -198,7 +174,6 @@ func TouchSession(sid string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// SwitchSessionTenant 切换当前会话所属企业(免密切换)
|
||||
func SwitchSessionTenant(sid string, tid uint64) error {
|
||||
_, err := models.Orm.QueryTable(new(models.AuthSession)).
|
||||
Filter("sid", sid).
|
||||
@@ -206,7 +181,6 @@ func SwitchSessionTenant(sid string, tid uint64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// RevokeSession 吊销单个会话
|
||||
func RevokeSession(sid, reason string) error {
|
||||
now := time.Now()
|
||||
_, err := models.Orm.QueryTable(new(models.AuthSession)).
|
||||
@@ -219,7 +193,6 @@ func RevokeSession(sid, reason string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// RevokeAllSessions 吊销该身份的全部会话(改密、管理员下线等场景)
|
||||
func RevokeAllSessions(identityID uint64, reason string) error {
|
||||
now := time.Now()
|
||||
_, err := models.Orm.QueryTable(new(models.AuthSession)).
|
||||
@@ -233,7 +206,6 @@ func RevokeAllSessions(identityID uint64, reason string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// ListActiveSessions 在线设备列表
|
||||
func ListActiveSessions(identityID uint64) ([]models.AuthSession, error) {
|
||||
var list []models.AuthSession
|
||||
_, err := models.Orm.QueryTable(new(models.AuthSession)).
|
||||
@@ -244,7 +216,6 @@ func ListActiveSessions(identityID uint64) ([]models.AuthSession, error) {
|
||||
return list, err
|
||||
}
|
||||
|
||||
// randomToken 生成 URL 安全的随机串(用于 sid / refresh token 明文)
|
||||
func randomToken(n int) (string, error) {
|
||||
buf := make([]byte, n)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
|
||||
Reference in new issue
Block a user