feat(web): RBAC, tenant scope, and WS room hooks
Add configurable role⊥permission (five tables, gate permissions, register, /_rbac desk), multi-tenant path/header resolution with JSON tenant_scope, and route_ws on_message/presence/dynamic room_key — with author surfaces, demos, and smokes. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
27991a7822
commit
5cf84fe9b7
27 files changed
+2372
-80
No files matched your search
@@ -1,15 +1,19 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/auth"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/password"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ratelimit"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/rbac"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
)
|
||||
|
||||
@@ -28,23 +32,28 @@ const (
|
||||
)
|
||||
|
||||
type gate struct {
|
||||
path string
|
||||
roles []string
|
||||
matchMode gateMatch
|
||||
onDeny onDeny
|
||||
exclude []string
|
||||
path string
|
||||
roles []string
|
||||
permissions []string
|
||||
matchMode gateMatch
|
||||
onDeny onDeny
|
||||
exclude []string
|
||||
}
|
||||
|
||||
type authConfig struct {
|
||||
users any
|
||||
sessionTTL uint64
|
||||
cookieSecure bool
|
||||
admin bool
|
||||
adminPrefix string
|
||||
loginPath string
|
||||
loginRedirect string
|
||||
logoutRedirect string
|
||||
gates []gate
|
||||
users any
|
||||
sessionTTL uint64
|
||||
cookieSecure bool
|
||||
admin bool
|
||||
adminPrefix string
|
||||
loginPath string
|
||||
loginRedirect string
|
||||
logoutRedirect string
|
||||
gates []gate
|
||||
rbac bool
|
||||
register bool
|
||||
registerPath string
|
||||
defaultRole string
|
||||
}
|
||||
|
||||
func authConfigOf(appBag map[string]any) authConfig {
|
||||
@@ -68,6 +77,7 @@ func authConfigOf(appBag map[string]any) authConfig {
|
||||
if v, ok := appBag["cookie_secure"].(bool); ok {
|
||||
cfg.cookieSecure = v
|
||||
}
|
||||
cfg.registerPath = strOpt(appBag, "register_path", "")
|
||||
if authBag, ok := appBag["auth"].(map[string]any); ok {
|
||||
cfg.users = authBag["users"]
|
||||
if n, ok := asUint64(authBag["session_ttl"]); ok && n > 0 {
|
||||
@@ -75,6 +85,18 @@ func authConfigOf(appBag map[string]any) authConfig {
|
||||
} else if n, ok := asUint64(authBag["ttl"]); ok && n > 0 {
|
||||
cfg.sessionTTL = n
|
||||
}
|
||||
cfg.rbac = boolish(authBag["rbac"])
|
||||
cfg.register = boolish(authBag["register"])
|
||||
if p := strOpt(authBag, "register_path", ""); p != "" {
|
||||
cfg.registerPath = p
|
||||
}
|
||||
cfg.defaultRole = strOpt(authBag, "default_role", "")
|
||||
if cfg.defaultRole == "" {
|
||||
cfg.defaultRole = "member"
|
||||
}
|
||||
}
|
||||
if cfg.registerPath == "" {
|
||||
cfg.registerPath = "/register"
|
||||
}
|
||||
cfg.gates = parseGates(appBag["gates"])
|
||||
return cfg
|
||||
@@ -106,7 +128,11 @@ func parseGates(v any) []gate {
|
||||
if roles == nil {
|
||||
roles = parseGateRoles(m["角色"])
|
||||
}
|
||||
if len(roles) == 0 {
|
||||
perms := parseGateRoles(m["permissions"])
|
||||
if perms == nil {
|
||||
perms = parseGateRoles(m["权限"])
|
||||
}
|
||||
if len(roles) == 0 && len(perms) == 0 {
|
||||
roles = []string{"admin"}
|
||||
}
|
||||
matchS := strings.ToLower(strings.TrimSpace(strOpt(m, "match", "")))
|
||||
@@ -136,11 +162,12 @@ func parseGates(v any) []gate {
|
||||
on = onDenyForbid
|
||||
}
|
||||
out = append(out, gate{
|
||||
path: path,
|
||||
roles: roles,
|
||||
matchMode: matchMode,
|
||||
onDeny: on,
|
||||
exclude: parseExclude(m["exclude"], m["排除"]),
|
||||
path: path,
|
||||
roles: roles,
|
||||
permissions: perms,
|
||||
matchMode: matchMode,
|
||||
onDeny: on,
|
||||
exclude: parseExclude(m["exclude"], m["排除"]),
|
||||
})
|
||||
}
|
||||
return out
|
||||
@@ -245,7 +272,17 @@ func withRBAC(next http.Handler, gates []gate, loginPath string) http.Handler {
|
||||
break
|
||||
}
|
||||
role := session.RoleFromCookie(cookie)
|
||||
if !session.RoleAllowed(role, g.roles) {
|
||||
allowed := false
|
||||
if len(g.permissions) > 0 {
|
||||
held := session.PermissionsFromCookie(cookie)
|
||||
if len(held) == 0 && role != "visitor" {
|
||||
held = expandLegacyRole(role)
|
||||
}
|
||||
allowed = session.PermissionAllowed(held, g.permissions)
|
||||
} else {
|
||||
allowed = session.RoleAllowed(role, g.roles)
|
||||
}
|
||||
if !allowed {
|
||||
if role == "visitor" && g.onDeny == onDenyRedirect {
|
||||
nextQ := urlEncodePath(path)
|
||||
dest := loginPath
|
||||
@@ -268,6 +305,22 @@ func withRBAC(next http.Handler, gates []gate, loginPath string) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func expandLegacyRole(role string) []string {
|
||||
switch strings.ToLower(strings.TrimSpace(role)) {
|
||||
case "superadmin", "admin":
|
||||
return []string{
|
||||
"comments:create", "comments:delete", "comments:delete_own",
|
||||
"desk:access", "roles:manage", "posts:edit", "users:manage",
|
||||
}
|
||||
case "member", "user":
|
||||
return []string{"comments:create", "comments:delete_own"}
|
||||
case "editor", "author":
|
||||
return []string{"comments:create", "comments:delete_own", "posts:edit", "desk:access"}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func urlEncodePath(path string) string {
|
||||
var out strings.Builder
|
||||
for _, b := range []byte(path) {
|
||||
@@ -303,20 +356,34 @@ func withNavAuth(page map[string]any, cookieHeader string) {
|
||||
}
|
||||
|
||||
func (st *state) mountAuthRoutes(mux *http.ServeMux) {
|
||||
if st.auth.users == nil {
|
||||
if st.auth.users == nil && !(st.auth.rbac && st.auth.register) {
|
||||
return
|
||||
}
|
||||
lp := st.auth.loginPath
|
||||
mux.HandleFunc("GET "+lp, st.handleLoginGet)
|
||||
mux.HandleFunc("POST "+lp, st.handleLoginPost)
|
||||
altLogin := strings.TrimRight(st.auth.adminPrefix, "/") + "/login"
|
||||
if altLogin != lp {
|
||||
mux.HandleFunc("GET "+altLogin, st.handleLoginGet)
|
||||
mux.HandleFunc("POST "+altLogin, st.handleLoginPost)
|
||||
if st.auth.users != nil || st.auth.rbac {
|
||||
lp := st.auth.loginPath
|
||||
mux.HandleFunc("GET "+lp, st.handleLoginGet)
|
||||
mux.HandleFunc("POST "+lp, st.handleLoginPost)
|
||||
altLogin := strings.TrimRight(st.auth.adminPrefix, "/") + "/login"
|
||||
if altLogin != lp {
|
||||
mux.HandleFunc("GET "+altLogin, st.handleLoginGet)
|
||||
mux.HandleFunc("POST "+altLogin, st.handleLoginPost)
|
||||
}
|
||||
logoutPath := strings.TrimRight(st.auth.adminPrefix, "/") + "/logout"
|
||||
mux.HandleFunc("GET "+logoutPath, st.handleLogout)
|
||||
}
|
||||
if st.auth.register && st.auth.rbac {
|
||||
rp := st.auth.registerPath
|
||||
mux.HandleFunc("GET "+rp, st.handleRegisterGet)
|
||||
mux.HandleFunc("POST "+rp, st.handleRegisterPost)
|
||||
}
|
||||
if st.auth.rbac && st.dbURL != "" {
|
||||
mux.HandleFunc("GET /_rbac/roles", st.handleRbacListRoles)
|
||||
mux.HandleFunc("GET /_rbac/permissions", st.handleRbacListPermissions)
|
||||
mux.HandleFunc("POST /_rbac/roles", st.handleRbacCreateRole)
|
||||
mux.HandleFunc("POST /_rbac/roles/{id}/permissions", st.handleRbacSetRolePerms)
|
||||
mux.HandleFunc("POST /_rbac/assign", st.handleRbacAssign)
|
||||
mux.HandleFunc("GET /_rbac/desk", st.handleRbacDesk)
|
||||
}
|
||||
// Logout is part of session auth, not only built-in admin=True chrome.
|
||||
logoutPath := strings.TrimRight(st.auth.adminPrefix, "/") + "/logout"
|
||||
mux.HandleFunc("GET "+logoutPath, st.handleLogout)
|
||||
if st.auth.admin {
|
||||
adminHome := strings.TrimRight(st.auth.adminPrefix, "/")
|
||||
if adminHome == "" {
|
||||
@@ -327,7 +394,7 @@ func (st *state) mountAuthRoutes(mux *http.ServeMux) {
|
||||
}
|
||||
|
||||
func (st *state) handleLoginGet(w http.ResponseWriter, r *http.Request) {
|
||||
if st.auth.users == nil {
|
||||
if st.auth.users == nil && !st.auth.rbac {
|
||||
http.Redirect(w, r, st.auth.loginRedirect, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -344,7 +411,7 @@ func (st *state) handleLoginGet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
if st.auth.users == nil {
|
||||
if st.auth.users == nil && !st.auth.rbac {
|
||||
http.Redirect(w, r, st.auth.loginRedirect, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -374,7 +441,7 @@ func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.WriteString(w, loginPageHTML(st.auth.loginPath, &msg, csrf, r.FormValue("next")))
|
||||
return
|
||||
}
|
||||
res := auth.Login(username, password, st.auth.users, st.auth.sessionTTL)
|
||||
res := st.tryLogin(username, password)
|
||||
if ok, _ := res["ok"].(bool); !ok {
|
||||
ratelimit.RecordFailure(ip, username)
|
||||
errMsg := "Invalid username or password."
|
||||
@@ -385,11 +452,54 @@ func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
ratelimit.ClearSuccess(ip, username)
|
||||
sessID, _ := res["session_id"].(string)
|
||||
role, _ := res["role"].(string)
|
||||
st.attachLoginPermissions(sessID, username, role)
|
||||
dest := loginDest(r, st.auth.loginRedirect)
|
||||
w.Header().Add("Set-Cookie", session.IssueCookie(sessID))
|
||||
http.Redirect(w, r, dest, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// tryLogin: GFM users first, then web_users when RBAC is on.
|
||||
func (st *state) tryLogin(username, password string) map[string]any {
|
||||
if st.auth.users != nil {
|
||||
res := auth.Login(username, password, st.auth.users, st.auth.sessionTTL)
|
||||
if ok, _ := res["ok"].(bool); ok {
|
||||
u, _ := res["username"].(string)
|
||||
role, _ := res["role"].(string)
|
||||
st.seedGFMUserIntoRBAC(u, password, role)
|
||||
return res
|
||||
}
|
||||
}
|
||||
if st.auth.rbac && st.dbURL != "" {
|
||||
role, ok, err := rbac.Authenticate(st.dbURL, username, password, passwordVerify)
|
||||
if err == nil && ok {
|
||||
id := session.NewID(st.auth.sessionTTL)
|
||||
session.SetValue(id, "username", username)
|
||||
session.SetValue(id, "role", role)
|
||||
return map[string]any{
|
||||
"ok": true, "session_id": id, "username": username, "role": role,
|
||||
}
|
||||
}
|
||||
}
|
||||
return map[string]any{"ok": false}
|
||||
}
|
||||
|
||||
func (st *state) attachLoginPermissions(sessID, username, role string) {
|
||||
var perms []string
|
||||
if st.dbURL != "" && st.auth.rbac {
|
||||
if dbPerms, err := rbac.PermissionsForUser(st.dbURL, username); err == nil && len(dbPerms) > 0 {
|
||||
perms = dbPerms
|
||||
}
|
||||
}
|
||||
if len(perms) == 0 {
|
||||
perms = rbac.ExpandRole(role)
|
||||
}
|
||||
if len(perms) == 0 {
|
||||
perms = expandLegacyRole(role)
|
||||
}
|
||||
auth.AttachPermissions(sessID, perms)
|
||||
}
|
||||
|
||||
func (st *state) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
if sid, ok := session.IDFromCookie(r.Header.Get("Cookie")); ok {
|
||||
auth.Logout(sid)
|
||||
@@ -469,6 +579,307 @@ a.back:hover { color:#f5e6c8; }
|
||||
</body></html>`, errHTML, esc(loginAction), csrfField, nextField)
|
||||
}
|
||||
|
||||
func passwordVerify(pass, encoded string) bool {
|
||||
return password.Verify(pass, encoded)
|
||||
}
|
||||
|
||||
func (st *state) seedGFMUserIntoRBAC(username, plaintext, role string) {
|
||||
if !st.auth.rbac || st.dbURL == "" || username == "" {
|
||||
return
|
||||
}
|
||||
exists, err := rbac.UserExists(st.dbURL, username)
|
||||
if err != nil || exists {
|
||||
return
|
||||
}
|
||||
hash, err := password.Hash(plaintext)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
target := "member"
|
||||
switch strings.ToLower(role) {
|
||||
case "admin", "superadmin":
|
||||
target = "superadmin"
|
||||
case "editor", "author":
|
||||
// custom roles may not exist yet; fall back to member + ExpandRole at session
|
||||
target = "member"
|
||||
}
|
||||
_ = rbac.EnsureUserWithRole(st.dbURL, username, hash, target)
|
||||
}
|
||||
|
||||
func (st *state) handleRegisterGet(w http.ResponseWriter, r *http.Request) {
|
||||
_, csrf, setCookie := resolveSession(r)
|
||||
html := registerPageHTML(st.auth.registerPath, nil, csrf)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) handleRegisterPost(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
sid, csrf, setCookie := resolveSession(r)
|
||||
csrfToken := r.FormValue("_csrf")
|
||||
if csrfToken != "" {
|
||||
if !session.ValidateCSRF(sid, csrfToken) {
|
||||
msg := "Invalid CSRF token."
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
return
|
||||
}
|
||||
}
|
||||
username := strings.TrimSpace(r.FormValue("username"))
|
||||
pass := r.FormValue("password")
|
||||
if len(username) < 2 || len(pass) < 4 {
|
||||
msg := "Username (≥2) and password (≥4) required."
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
return
|
||||
}
|
||||
hash, err := password.Hash(pass)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := rbac.RegisterUser(st.dbURL, username, hash, st.auth.defaultRole); err != nil {
|
||||
msg := err.Error()
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
return
|
||||
}
|
||||
res := st.tryLogin(username, pass)
|
||||
if ok, _ := res["ok"].(bool); ok {
|
||||
sessID, _ := res["session_id"].(string)
|
||||
role, _ := res["role"].(string)
|
||||
st.attachLoginPermissions(sessID, username, role)
|
||||
w.Header().Add("Set-Cookie", session.IssueCookie(sessID))
|
||||
dest := st.auth.loginRedirect
|
||||
held := session.PermissionsFromSession(sessID)
|
||||
if !session.PermissionAllowed(held, []string{"desk:access"}) {
|
||||
dest = "/"
|
||||
}
|
||||
http.Redirect(w, r, dest, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, st.auth.loginPath, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func registerPageHTML(action string, errMsg *string, csrf string) string {
|
||||
errHTML := ""
|
||||
if errMsg != nil && *errMsg != "" {
|
||||
errHTML = fmt.Sprintf("<p class=\"flash err\">%s</p>", esc(*errMsg))
|
||||
}
|
||||
csrfField := ""
|
||||
if csrf != "" {
|
||||
csrfField = fmt.Sprintf("<input type=\"hidden\" name=\"_csrf\" value=\"%s\"/>", esc(csrf))
|
||||
}
|
||||
return fmt.Sprintf(`<!DOCTYPE html>
|
||||
<html lang="en"><head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>Register</title>
|
||||
<style>
|
||||
:root { --ink:#0a0809; --paper:#f5e6c8; --gold:#d4af37; --err:#b91c1c; }
|
||||
* { box-sizing:border-box; }
|
||||
body { margin:0; min-height:100vh; display:grid; place-items:center; color:var(--paper); font-family:"Noto Sans SC","IBM Plex Sans",system-ui,sans-serif;
|
||||
background: radial-gradient(ellipse 90%% 55%% at 50%% -15%%, rgba(107,30,58,.5), transparent 58%%), linear-gradient(180deg,#0a0809,#120c10 45%%,#0a0809); }
|
||||
.login { background:rgba(0,0,0,.45); border:1px solid rgba(212,175,55,.28); border-radius:12px; padding:2rem 2.25rem; width:min(92vw,22rem); }
|
||||
.login h1 { margin:0 0 .25rem; font-size:1.55rem; color:var(--gold); }
|
||||
.login .sub { color:rgba(245,230,200,.72); margin:0 0 1.25rem; font-size:.9rem; }
|
||||
.login form { display:grid; gap:.9rem; }
|
||||
.login label { display:grid; gap:.25rem; font-size:.85rem; }
|
||||
.login input { padding:.6rem .75rem; border:1px solid rgba(212,175,55,.35); border-radius:8px; font:inherit; color:var(--paper); background:rgba(10,8,9,.7); }
|
||||
.login button { background:linear-gradient(135deg,#f5e6c8,var(--gold)); color:var(--ink); border:0; padding:.65rem 1rem; border-radius:999px; cursor:pointer; font:inherit; font-weight:600; }
|
||||
.flash.err { background:rgba(185,28,28,.15); color:#fecaca; border:1px solid rgba(185,28,28,.45); padding:.6rem .8rem; border-radius:8px; margin:0 0 .9rem; font-size:.9rem; }
|
||||
a.back { color:var(--gold); text-decoration:none; font-size:.85rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="login">
|
||||
<h1>Create account</h1>
|
||||
<p class="sub">New members get the default role (comments).</p>
|
||||
%s
|
||||
<form method="post" action="%s">
|
||||
%s
|
||||
<label>Username<input name="username" autocomplete="username" required autofocus/></label>
|
||||
<label>Password<input name="password" type="password" autocomplete="new-password" required/></label>
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
<p style="margin:1.1rem 0 0"><a class="back" href="/login">← Sign in</a></p>
|
||||
</div>
|
||||
</body></html>`, errHTML, esc(action), csrfField)
|
||||
}
|
||||
|
||||
func (st *state) requireRolesManage(w http.ResponseWriter, r *http.Request) bool {
|
||||
held := session.PermissionsFromCookie(r.Header.Get("Cookie"))
|
||||
if !session.PermissionAllowed(held, []string{"roles:manage"}) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (st *state) writeJSON(w http.ResponseWriter, status int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func (st *state) handleRbacListRoles(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
roles, err := rbac.ListRoles(st.dbURL)
|
||||
if err != nil {
|
||||
st.writeJSON(w, http.StatusInternalServerError, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true, "roles": roles})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacListPermissions(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
perms, err := rbac.ListPermissions(st.dbURL)
|
||||
if err != nil {
|
||||
st.writeJSON(w, http.StatusInternalServerError, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true, "permissions": perms})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacCreateRole(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || strings.TrimSpace(body.Name) == "" {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": "name required"})
|
||||
return
|
||||
}
|
||||
id, err := rbac.CreateRole(st.dbURL, strings.TrimSpace(body.Name))
|
||||
if err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true, "id": id, "name": body.Name})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacSetRolePerms(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
idStr := r.PathValue("id")
|
||||
roleID, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": "bad id"})
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Permissions []string `json:"permissions"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": "bad json"})
|
||||
return
|
||||
}
|
||||
grantable := session.PermissionsFromCookie(r.Header.Get("Cookie"))
|
||||
if err := rbac.SetRolePermissions(st.dbURL, roleID, body.Permissions, grantable); err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacAssign(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Username string `json:"username"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": "bad json"})
|
||||
return
|
||||
}
|
||||
if err := rbac.AssignRole(st.dbURL, body.Username, body.Role); err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacDesk(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
roles, _ := rbac.ListRoles(st.dbURL)
|
||||
perms, _ := rbac.ListPermissions(st.dbURL)
|
||||
var roleOpts, permChecks strings.Builder
|
||||
for _, role := range roles {
|
||||
name, _ := role["name"].(string)
|
||||
id := role["id"]
|
||||
sys, _ := role["is_system"].(bool)
|
||||
tag := ""
|
||||
if sys {
|
||||
tag = " (system)"
|
||||
}
|
||||
fmt.Fprintf(&roleOpts, `<option value="%v">%s%s</option>`, id, esc(name), tag)
|
||||
}
|
||||
for _, p := range perms {
|
||||
code, _ := p["code"].(string)
|
||||
desc, _ := p["description"].(string)
|
||||
fmt.Fprintf(&permChecks, `<label style="display:block;margin:.25rem 0"><input type="checkbox" name="perm" value="%s"/> %s — %s</label>`,
|
||||
esc(code), esc(code), esc(desc))
|
||||
}
|
||||
html := fmt.Sprintf(`<!DOCTYPE html>
|
||||
<html lang="zh"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>Roles · RBAC</title>
|
||||
<style>
|
||||
body{font-family:"Noto Sans SC",system-ui,sans-serif;max-width:42rem;margin:2rem auto;padding:0 1rem;line-height:1.5}
|
||||
h1{font-size:1.4rem} section{margin:1.5rem 0;padding:1rem;border:1px solid #ddd;border-radius:8px}
|
||||
button{padding:.5rem 1rem;cursor:pointer} input,select{padding:.4rem;margin:.25rem 0;width:100%%;max-width:20rem}
|
||||
#msg{min-height:1.2rem;color:#b91c1c}
|
||||
</style></head><body>
|
||||
<h1>角色与权限</h1>
|
||||
<p>需 <code>roles:manage</code>。系统角色权限不可在此改写;可新建角色并勾选权限,或给用户赋角色。</p>
|
||||
<p id="msg"></p>
|
||||
<section>
|
||||
<h2>新建角色</h2>
|
||||
<input id="new-role" placeholder="角色名"/><button type="button" id="btn-create">创建</button>
|
||||
</section>
|
||||
<section>
|
||||
<h2>设置角色权限</h2>
|
||||
<select id="role-sel">%s</select>
|
||||
<div>%s</div>
|
||||
<button type="button" id="btn-set">保存权限</button>
|
||||
</section>
|
||||
<section>
|
||||
<h2>赋角色给用户</h2>
|
||||
<input id="assign-user" placeholder="用户名"/>
|
||||
<input id="assign-role" placeholder="角色名(如 member)"/>
|
||||
<button type="button" id="btn-assign">赋权</button>
|
||||
</section>
|
||||
<p><a href="/desk">← 后台</a></p>
|
||||
<script>
|
||||
const msg=document.getElementById('msg');
|
||||
async function j(url,opt){const r=await fetch(url,Object.assign({credentials:'same-origin',headers:{'Content-Type':'application/json'}},opt||{}));const d=await r.json();if(!d.ok)throw new Error(d.error||r.status);return d;}
|
||||
document.getElementById('btn-create').onclick=async()=>{try{const name=document.getElementById('new-role').value.trim();await j('/_rbac/roles',{method:'POST',body:JSON.stringify({name})});msg.textContent='已创建 '+name;location.reload();}catch(e){msg.textContent=e.message;}};
|
||||
document.getElementById('btn-set').onclick=async()=>{try{const id=document.getElementById('role-sel').value;const permissions=[...document.querySelectorAll('input[name=perm]:checked')].map(x=>x.value);await j('/_rbac/roles/'+id+'/permissions',{method:'POST',body:JSON.stringify({permissions})});msg.textContent='权限已保存';}catch(e){msg.textContent=e.message;}};
|
||||
document.getElementById('btn-assign').onclick=async()=>{try{const username=document.getElementById('assign-user').value.trim();const role=document.getElementById('assign-role').value.trim();await j('/_rbac/assign',{method:'POST',body:JSON.stringify({username,role})});msg.textContent='已赋 '+role+' → '+username;}catch(e){msg.textContent=e.message;}};
|
||||
</script>
|
||||
</body></html>`, roleOpts.String(), permChecks.String())
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func clientIP(r *http.Request) string {
|
||||
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
||||
if i := strings.Index(xff, ","); i >= 0 {
|
||||
|
||||
@@ -22,9 +22,11 @@ import (
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/form"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ratelimit"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/rbac"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/render"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/sitemap"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/tenant"
|
||||
)
|
||||
|
||||
// NewHandler builds the HTTP handler for an app bag (no listen).
|
||||
@@ -35,6 +37,11 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
}
|
||||
authCfg := authConfigOf(appBag)
|
||||
dbURL := dbURLOf(appBag)
|
||||
if authCfg.rbac && dbURL != "" {
|
||||
if err := rbac.EnsureSchema(dbURL); err != nil {
|
||||
return nil, fmt.Errorf("rbac ensure: %w", err)
|
||||
}
|
||||
}
|
||||
session.Configure(session.Config{
|
||||
DBURL: dbURL,
|
||||
TTLSec: authCfg.sessionTTL,
|
||||
@@ -99,6 +106,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
proxyRoutes: routeMapOf(appBag, "proxy_routes"),
|
||||
invokeRoutes: routeMapOf(appBag, "invoke_routes"),
|
||||
auth: authCfg,
|
||||
tenant: tenant.FromBag(appBag),
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
@@ -132,7 +140,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
route := jr
|
||||
pattern := route.Method + " " + route.Path
|
||||
mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
st.handleJSON(w, route)
|
||||
st.handleJSON(w, r, route)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -217,6 +225,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
|
||||
handler := withMiddleware(mux, mw)
|
||||
handler = withRBAC(handler, authCfg.gates, authCfg.loginPath)
|
||||
handler = withTenant(handler, st.tenant)
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
@@ -280,6 +289,7 @@ type state struct {
|
||||
proxyRoutes map[string]any
|
||||
invokeRoutes map[string]any
|
||||
auth authConfig
|
||||
tenant tenant.Config
|
||||
}
|
||||
|
||||
func (st *state) handleHome(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -432,6 +442,11 @@ func (st *state) handleFormPost(w http.ResponseWriter, r *http.Request) {
|
||||
data[k] = vs[0]
|
||||
}
|
||||
}
|
||||
if st.tenant.Enabled() && st.tenant.DefaultScope {
|
||||
if tid := tenant.FromContext(r.Context()); tid != "" {
|
||||
data = tenant.StampRow(data, st.tenant.Column, tid)
|
||||
}
|
||||
}
|
||||
res, err := form.Submit(frm, data, st.dbURL)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -447,12 +462,21 @@ func (st *state) handleFormPost(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) handleJSON(w http.ResponseWriter, route middleware.JSONRouteMount) {
|
||||
func (st *state) handleJSON(w http.ResponseWriter, r *http.Request, route middleware.JSONRouteMount) {
|
||||
if st.dbURL == "" {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": "no database"})
|
||||
return
|
||||
}
|
||||
opts := db.SelectOpts{Where: route.Where, Order: route.Order}
|
||||
where := route.Where
|
||||
if (route.TenantScope || st.tenant.DefaultScope) && st.tenant.Enabled() {
|
||||
tid := tenant.FromContext(r.Context())
|
||||
if tid == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "tenant required"})
|
||||
return
|
||||
}
|
||||
where = tenant.MergeWhere(where, st.tenant.Column, tid)
|
||||
}
|
||||
opts := db.SelectOpts{Where: where, Order: route.Order}
|
||||
out, err := db.Select(st.dbURL, route.Table, route.Limit, opts)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||
@@ -461,6 +485,17 @@ func (st *state) handleJSON(w http.ResponseWriter, route middleware.JSONRouteMou
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func withTenant(next http.Handler, cfg tenant.Config) http.Handler {
|
||||
if !cfg.Enabled() {
|
||||
return next
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := tenant.Resolve(r, cfg)
|
||||
ctx := tenant.WithContext(r.Context(), id)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func withMiddleware(next http.Handler, mw middleware.Config) http.Handler {
|
||||
h := next
|
||||
if mw.BodyLimit != nil && *mw.BodyLimit > 0 {
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/plugin"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ws"
|
||||
)
|
||||
|
||||
@@ -13,14 +18,44 @@ var wsUpgrader = websocket.Upgrader{
|
||||
|
||||
func (st *state) mountWSRoutes(mux *http.ServeMux) {
|
||||
for _, path := range sortedKeys(st.wsRoutes) {
|
||||
spec := ws.ParseRouteSpec(st.wsRoutes[path]) // mode + optional room_key
|
||||
spec := ws.ParseRouteSpec(st.wsRoutes[path]) // mode + optional room_key / on_message
|
||||
routePath := path
|
||||
mux.HandleFunc("GET "+routePath, func(w http.ResponseWriter, r *http.Request) {
|
||||
pattern := routePath
|
||||
if strings.Contains(routePath, "{") {
|
||||
pattern = goMuxPattern(routePath)
|
||||
}
|
||||
mux.HandleFunc("GET "+pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
st.handleWS(w, r, routePath, spec)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func pathValuesOf(r *http.Request, routePath string) map[string]string {
|
||||
out := map[string]string{}
|
||||
// Collect {name} tokens from the route template.
|
||||
rest := routePath
|
||||
for {
|
||||
i := strings.Index(rest, "{")
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
rest = rest[i+1:]
|
||||
j := strings.Index(rest, "}")
|
||||
if j < 0 {
|
||||
break
|
||||
}
|
||||
name := rest[:j]
|
||||
name = strings.TrimPrefix(name, "*")
|
||||
if name != "" {
|
||||
if v := r.PathValue(name); v != "" {
|
||||
out[name] = v
|
||||
}
|
||||
}
|
||||
rest = rest[j+1:]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, spec ws.RouteSpec) {
|
||||
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
@@ -28,6 +63,7 @@ func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, s
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
vals := pathValuesOf(r, path)
|
||||
switch spec.Mode {
|
||||
case ws.ModeEcho:
|
||||
for {
|
||||
@@ -53,11 +89,26 @@ func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, s
|
||||
case ws.ModeBroadcast, ws.ModeRoom:
|
||||
room := path
|
||||
if spec.RoomKey != "" {
|
||||
room = spec.RoomKey
|
||||
room = ws.ExpandTemplate(spec.RoomKey, vals)
|
||||
} else if len(vals) > 0 {
|
||||
room = ws.ExpandTemplate(path, vals)
|
||||
}
|
||||
user := session.UsernameFromCookie(r.Header.Get("Cookie"))
|
||||
if user == "" {
|
||||
user = "anon"
|
||||
}
|
||||
ch := ws.JoinRoom(room)
|
||||
defer ws.LeaveRoom(room, ch)
|
||||
|
||||
presenceRoom := room + ".presence"
|
||||
var presenceCh chan string
|
||||
if spec.Presence {
|
||||
presenceCh = ws.JoinRoom(presenceRoom)
|
||||
defer ws.LeaveRoom(presenceRoom, presenceCh)
|
||||
ws.PublishRoom(presenceRoom, presenceJSON("join", user, room))
|
||||
defer ws.PublishRoom(presenceRoom, presenceJSON("leave", user, room))
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
@@ -67,7 +118,25 @@ func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, s
|
||||
return
|
||||
}
|
||||
if mt == websocket.TextMessage {
|
||||
ws.PublishRoom(room, string(data))
|
||||
text := string(data)
|
||||
if spec.OnMessage != "" {
|
||||
out, err := plugin.CallLibPath(spec.OnMessage, map[string]any{
|
||||
"message": text,
|
||||
"room": room,
|
||||
"path": path,
|
||||
"user": user,
|
||||
})
|
||||
if err != nil {
|
||||
_ = conn.WriteMessage(websocket.TextMessage, []byte(`{"error":`+jsonQuote(err.Error())+`}`))
|
||||
continue
|
||||
}
|
||||
skip, rewritten := interpretOnMessage(out, text)
|
||||
if skip {
|
||||
continue
|
||||
}
|
||||
text = rewritten
|
||||
}
|
||||
ws.PublishRoom(room, text)
|
||||
} else if mt == websocket.CloseMessage {
|
||||
return
|
||||
}
|
||||
@@ -85,9 +154,66 @@ func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, s
|
||||
if err := conn.WriteMessage(websocket.TextMessage, []byte(text)); err != nil {
|
||||
return
|
||||
}
|
||||
case text, ok := <-presenceOrNil(presenceCh):
|
||||
if presenceCh == nil {
|
||||
continue
|
||||
}
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := conn.WriteMessage(websocket.TextMessage, []byte(text)); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func presenceOrNil(ch chan string) <-chan string {
|
||||
if ch == nil {
|
||||
return nil
|
||||
}
|
||||
return ch
|
||||
}
|
||||
|
||||
func presenceJSON(event, user, room string) string {
|
||||
b, _ := json.Marshal(map[string]any{
|
||||
"type": "presence",
|
||||
"event": event,
|
||||
"user": user,
|
||||
"room": room,
|
||||
})
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func jsonQuote(s string) string {
|
||||
b, _ := json.Marshal(s)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func interpretOnMessage(out any, original string) (skip bool, text string) {
|
||||
text = original
|
||||
m, ok := out.(map[string]any)
|
||||
if !ok {
|
||||
if s, ok := out.(string); ok && s != "" {
|
||||
return false, s
|
||||
}
|
||||
return false, original
|
||||
}
|
||||
if v, ok := m["skip"]; ok && boolish(v) {
|
||||
return true, ""
|
||||
}
|
||||
if v, ok := m["drop"]; ok && boolish(v) {
|
||||
return true, ""
|
||||
}
|
||||
if v, ok := m["message"]; ok {
|
||||
text = fmt.Sprint(v)
|
||||
} else if v, ok := m["消息"]; ok {
|
||||
text = fmt.Sprint(v)
|
||||
} else if v, ok := m["text"]; ok {
|
||||
text = fmt.Sprint(v)
|
||||
}
|
||||
return false, text
|
||||
}
|
||||
Reference in new issue
Block a user