1 parent
110a5f86cc
commit
20a6eea3b3
15 files changed
+360
-21
No files matched your search
@@ -369,13 +369,13 @@ func ListProtocolCredentials(ctx *context.Context, w http.ResponseWriter, req *h
|
||||
items, err := service.ListProtocolCredentials(ctx.UserID)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("load protocol credentials failed")
|
||||
writeSecurityError(w, response.ParamsError, "读取第三方协议设置失败")
|
||||
writeSecurityError(w, response.ParamsError, "读取邮件客户端协议设置失败")
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(items).FPrint(w)
|
||||
}
|
||||
|
||||
// SetProtocolCredentialEnabled 设置当前用户的第三方协议开关。
|
||||
// SetProtocolCredentialEnabled 设置当前用户的邮件客户端协议开关,不改变服务监听状态。
|
||||
func SetProtocolCredentialEnabled(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
service, ok := requireProtocolCredentialContext(ctx, w)
|
||||
if !ok {
|
||||
|
||||
@@ -2,7 +2,7 @@ package models
|
||||
|
||||
import "time"
|
||||
|
||||
// ProtocolCredential 保存单个用户的第三方邮件协议开关和授权码哈希。
|
||||
// ProtocolCredential 保存单个用户的邮件客户端协议开关和授权码哈希,不控制全局监听服务。
|
||||
type ProtocolCredential struct {
|
||||
ID int `xorm:"id bigint unsigned not null pk autoincr" json:"-"`
|
||||
UserID int `xorm:"user_id bigint not null" json:"-"`
|
||||
|
||||
@@ -204,8 +204,21 @@ func (s *Service) VerifyProtocolCode(userID int, protocol string, plain string)
|
||||
return false
|
||||
}
|
||||
now := s.now()
|
||||
_, _ = s.engine.ID(item.ID).Cols("last_used_at", "updated_at").Update(&models.ProtocolCredential{LastUsedAt: &now})
|
||||
return true
|
||||
// 哈希计算期间可能发生关闭、撤销或换码,返回成功前必须再次原子确认凭据状态。
|
||||
affected, err := s.engine.ID(item.ID).
|
||||
Where("enabled = ? AND revoked_at IS NULL AND code_version = ? AND code_hash = ?", true, item.CodeVersion, item.CodeHash).
|
||||
Cols("last_used_at", "updated_at").Update(&models.ProtocolCredential{LastUsedAt: &now})
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if affected == 1 {
|
||||
return true
|
||||
}
|
||||
// MySQL 在时间字段未变化时可能报告零影响行,必须再次查询有效状态而不是误拒绝或直接放行。
|
||||
valid, err := s.engine.ID(item.ID).
|
||||
Where("enabled = ? AND revoked_at IS NULL AND code_version = ? AND code_hash = ?", true, item.CodeVersion, item.CodeHash).
|
||||
Exist(&models.ProtocolCredential{})
|
||||
return err == nil && valid
|
||||
}
|
||||
|
||||
func newProtocolCode() (string, error) {
|
||||
|
||||
Reference in new issue
Block a user