feat: show application ownership and scoped console management
This commit is contained in:
@@ -13,6 +13,8 @@ import (
|
||||
"time"
|
||||
|
||||
"git.yuntuo.hk/k2safe/orbit/internal/consoleauth"
|
||||
"git.yuntuo.hk/k2safe/orbit/internal/fault"
|
||||
core "git.yuntuo.hk/k2safe/orbit/model"
|
||||
"git.yuntuo.hk/k2safe/orbit/platform/auth"
|
||||
)
|
||||
|
||||
@@ -119,11 +121,32 @@ func RegisterConsole(server *http.Server, authStores ...consoleauth.Store) {
|
||||
|
||||
// ConsoleSessionMiddleware 将 HttpOnly 会话转换为内部 Bearer 请求头。
|
||||
// 浏览器脚本永远看不到业务令牌;现有所有 v1 handler 仍走同一个鉴权入口。
|
||||
func ConsoleSessionMiddleware(next http.Handler, consoleAuth consoleauth.Store) http.Handler {
|
||||
func ConsoleSessionMiddleware(next http.Handler, consoleAuth consoleauth.Store, scopes ...consoleauth.Scopes) http.Handler {
|
||||
if next == nil || consoleAuth == nil {
|
||||
return next
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if values := r.Header.Values("X-Orbit-Namespace"); len(values) > 0 {
|
||||
if len(values) != 1 || len(scopes) == 0 || scopes[0] == nil || len(r.Header.Values("Authorization")) > 0 || r.Header.Get("X-Orbit-Console") != "1" || !consoleScopePath(r) {
|
||||
writeError(w, "", fault.Forbidden)
|
||||
return
|
||||
}
|
||||
if err := http.NewCrossOriginProtection().Check(r); err != nil {
|
||||
writeError(w, "", fault.Forbidden)
|
||||
return
|
||||
}
|
||||
token, ok := consoleCookieToken(r)
|
||||
if !ok || consoleAuth.ValidateSession(r.Context(), token) != nil {
|
||||
writeError(w, "", fault.Unauthorized)
|
||||
return
|
||||
}
|
||||
scoped, err := scopes[0].ResolveScope(r.Context(), token, core.ID(values[0]))
|
||||
if err != nil {
|
||||
writeError(w, "", err)
|
||||
return
|
||||
}
|
||||
r = r.WithContext(context.WithValue(r.Context(), consoleScopeKey{}, scoped))
|
||||
}
|
||||
if len(r.Header.Values("Authorization")) == 0 {
|
||||
if token, ok := consoleCookieToken(r); ok && consoleAuth.ValidateSession(r.Context(), token) == nil {
|
||||
clone := r.Clone(r.Context())
|
||||
|
||||
Reference in New Issue
Block a user