1 parent
d93337852d
commit
dbcfc5c461
6 files changed
+36
-16
No files matched your search
@@ -7,6 +7,11 @@ import (
|
||||
|
||||
// authorizeCapability 统一把 HTTP 请求上下文映射到能力授权器。
|
||||
func authorizeCapability(ctx *context.Context, capability access.Capability, targetRole access.Role, ownResource bool) error {
|
||||
return authorizeCapabilityWithNewRole(ctx, capability, targetRole, "", ownResource)
|
||||
}
|
||||
|
||||
// authorizeCapabilityWithNewRole 在角色变更时同时把目标新角色传给统一授权器。
|
||||
func authorizeCapabilityWithNewRole(ctx *context.Context, capability access.Capability, targetRole, newRole access.Role, ownResource bool) error {
|
||||
if ctx == nil {
|
||||
return &access.Denial{Code: access.CodeAccessDenied}
|
||||
}
|
||||
@@ -18,6 +23,7 @@ func authorizeCapability(ctx *context.Context, capability access.Capability, tar
|
||||
RecentAuth: ctx.RecentAuth,
|
||||
RecoveryCodeConfigured: ctx.RecoveryCodeConfigured,
|
||||
TargetRecoveryConfigured: ctx.TargetRecoveryConfigured,
|
||||
NewRole: newRole,
|
||||
PluginInstanceAuthorized: ctx.PluginInstanceAuthorized,
|
||||
AuditScopeAuthorized: ctx.AuditScopeAuthorized,
|
||||
DeploymentChannel: ctx.DeploymentChannel,
|
||||
|
||||
@@ -583,7 +583,7 @@ func recoveryBrowserContext(req *http.Request, w http.ResponseWriter, create boo
|
||||
value := hex.EncodeToString(raw[:])
|
||||
secure := req.TLS != nil || !config.DevelopmentMode
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: recoveryContextCookieName, Value: value, Path: "/api/security/recovery",
|
||||
Name: recoveryContextCookieName, Value: value, Path: "/api/",
|
||||
HttpOnly: true, Secure: secure, SameSite: http.SameSiteStrictMode, MaxAge: 15 * 60,
|
||||
})
|
||||
return value, true
|
||||
@@ -595,7 +595,7 @@ func clearRecoveryBrowserContext(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
secure := req != nil && (req.TLS != nil || !config.DevelopmentMode)
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: recoveryContextCookieName, Value: "", Path: "/api/security/recovery",
|
||||
Name: recoveryContextCookieName, Value: "", Path: "/api/",
|
||||
HttpOnly: true, Secure: secure, SameSite: http.SameSiteStrictMode, MaxAge: -1,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func TestRecoveryBrowserContextCookieProperties(t *testing.T) {
|
||||
t.Fatal("recovery context was not created")
|
||||
}
|
||||
cookie := recorder.Result().Cookies()[0]
|
||||
if cookie.Name != recoveryContextCookieName || !cookie.HttpOnly || cookie.SameSite != http.SameSiteStrictMode || cookie.Path != "/api/security/recovery" || cookie.Secure {
|
||||
if cookie.Name != recoveryContextCookieName || !cookie.HttpOnly || cookie.SameSite != http.SameSiteStrictMode || cookie.Path != "/api/" || cookie.Secure {
|
||||
t.Fatalf("unexpected recovery context cookie: %+v", cookie)
|
||||
}
|
||||
request.AddCookie(cookie)
|
||||
|
||||
@@ -147,15 +147,15 @@ func Info(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
domains := array.Difference(mailDomains.Accepted, []string{mailDomains.Primary})
|
||||
domains = append([]string{mailDomains.Primary}, domains...)
|
||||
result := map[string]any{
|
||||
"account": ctx.UserAccount,
|
||||
"name": ctx.UserName,
|
||||
"role": ctx.EffectiveRole(),
|
||||
"capabilities": access.Capabilities(ctx.EffectiveRole()),
|
||||
"is_admin": ctx.IsAdmin,
|
||||
"must_change_password": ctx.MustChangePassword,
|
||||
"account": ctx.UserAccount,
|
||||
"name": ctx.UserName,
|
||||
"role": ctx.EffectiveRole(),
|
||||
"capabilities": access.Capabilities(ctx.EffectiveRole()),
|
||||
"is_admin": ctx.IsAdmin,
|
||||
"must_change_password": ctx.MustChangePassword,
|
||||
"showSecuritySetupPrompt": ctx.SecuritySetupAcknowledged == 0,
|
||||
"webAuthnEnabled": webAuthnRuntime() != nil,
|
||||
"domains": domains,
|
||||
"webAuthnEnabled": webAuthnRuntime() != nil,
|
||||
"domains": domains,
|
||||
}
|
||||
if ctx.EffectiveRole() != access.RoleSuperAdmin {
|
||||
result["recoveryCodesConfigured"] = ctx.RecoveryCodeConfigured
|
||||
@@ -232,7 +232,7 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
ctx.TargetRecoveryConfigured = configured
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityRoleManage, targetRole, false); err != nil {
|
||||
if err := authorizeCapabilityWithNewRole(ctx, access.CapabilityRoleManage, targetRole, newRole, false); err != nil {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "角色变更需要近期认证和恢复凭据", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user