更新若干
This commit is contained in:
1 parent
54c90072fb
commit
5682069553
59 files changed
+3813
-1617
No files matched your search
@@ -0,0 +1,85 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"server/models"
|
||||
)
|
||||
|
||||
// CodeLogin 通过已校验的验证码完成登录,登录态结构与 PasswordLogin 保持一致。
|
||||
// identity 由调用方按手机号/邮箱查得并已完成验证码校验。
|
||||
func CodeLogin(identity *models.AuthIdentity, clientID, ip, userAgent, amr, loginType string) (*LoginResult, error) {
|
||||
if identity == nil {
|
||||
return nil, ErrPasswordWrong
|
||||
}
|
||||
if err := checkIdentityStatus(identity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tenants, err := ListTenantOptions(identity.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(tenants) == 0 {
|
||||
_ = writeLoginLog(0, identity.ID, accountOf(identity), "", clientID, loginType, amr, 0, ErrNoTenantBound.Error(), ip, userAgent)
|
||||
return nil, ErrNoTenantBound
|
||||
}
|
||||
|
||||
profile, err := BuildProfile(identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &LoginResult{Tenants: tenants, Identity: profile}
|
||||
|
||||
targetTid := uint64(PendingTenantID)
|
||||
if len(tenants) == 1 {
|
||||
targetTid = tenants[0].Tid
|
||||
}
|
||||
sess, err := CreateSession(SessionInfo{
|
||||
IdentityID: identity.ID,
|
||||
Tid: targetTid,
|
||||
ClientID: clientID,
|
||||
IP: ip,
|
||||
UserAgent: userAgent,
|
||||
LoginType: loginType,
|
||||
Amr: amr,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accessTTL := 0
|
||||
if targetTid == PendingTenantID {
|
||||
accessTTL = 600
|
||||
result.NeedChooseTenant = true
|
||||
}
|
||||
|
||||
tokens, err := IssueTokens(TokenIssue{
|
||||
IdentityID: identity.ID,
|
||||
Tid: targetTid,
|
||||
ClientID: clientID,
|
||||
Sid: sess.Sid,
|
||||
Username: profile.Nickname,
|
||||
Amr: amr,
|
||||
AccessTTL: accessTTL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Tokens = tokens
|
||||
|
||||
_ = writeLoginLog(targetTid, identity.ID, accountOf(identity), profile.Nickname, clientID, loginType, amr, 1, "登录成功", ip, userAgent)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func accountOf(identity *models.AuthIdentity) string {
|
||||
if identity != nil && identity.Account != nil {
|
||||
return *identity.Account
|
||||
}
|
||||
if identity != nil && identity.Mobile != nil {
|
||||
return *identity.Mobile
|
||||
}
|
||||
if identity != nil && identity.Email != nil {
|
||||
return *identity.Email
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -34,23 +34,31 @@ func FindIdentityByAccount(account string) (*models.AuthIdentity, error) {
|
||||
if account == "" {
|
||||
return nil, orm.ErrNoRows
|
||||
}
|
||||
base := models.Orm.QueryTable(new(models.AuthIdentity)).Filter("delete_time__isnull", true)
|
||||
|
||||
for _, field := range []string{"account", "mobile", "email"} {
|
||||
m := &models.AuthIdentity{}
|
||||
if err := base.Filter(field, account).One(m); err == nil {
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 优先走企业绑定关系:企业用户名通过 AuthTenantUser 明确关联到唯一 identity。
|
||||
// 若先按 identity 表的 account 直接匹配,可能命中历史遗留的同名账号(账号相同但密码/手机号不同),
|
||||
// 导致用错误的密码哈希校验而登录失败。
|
||||
var bind models.AuthTenantUser
|
||||
if err := models.Orm.QueryTable(new(models.AuthTenantUser)).
|
||||
Filter("account", account).
|
||||
Filter("delete_time__isnull", true).
|
||||
OrderBy("-is_default", "id").
|
||||
One(&bind); err == nil {
|
||||
One(&bind); err == nil && bind.IdentityID > 0 {
|
||||
m := &models.AuthIdentity{}
|
||||
if err := models.Orm.QueryTable(new(models.AuthIdentity)).Filter("id", bind.IdentityID).One(m); err == nil {
|
||||
if err := models.Orm.QueryTable(new(models.AuthIdentity)).
|
||||
Filter("id", bind.IdentityID).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(m); err == nil {
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 回退:身份只认手机号/邮箱作为凭证,不用 account 字段直接匹配身份。
|
||||
// 企业账号必须经上面的 AuthTenantUser 绑定关系定位身份,避免账号撞名命中错误身份。
|
||||
base := models.Orm.QueryTable(new(models.AuthIdentity)).Filter("delete_time__isnull", true)
|
||||
for _, field := range []string{"mobile", "email"} {
|
||||
m := &models.AuthIdentity{}
|
||||
if err := base.Filter(field, account).One(m); err == nil {
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user