Access was admin or nothing, so letting a friend print receipts meant making them a full admin. Users now carry a list of permissions (User.Permissions, a JSON column), each a key from the registry in services/permissions.go; admins pass every check. The only key so far is `receipt`. GET /auth/validate/:permission answers like /auth/validate-admin (200/403/401, with the same refresh-token fallback, now shared) plus 404 for an unknown key, and reads the user row rather than the token, so a grant or revoke applies on the next request. On 200 it returns the username in X-Auth-User. nginx gets a permission gate beside the admin gate: a location sets $auth_permission and auth_requests /internal/auth/validate. /receipt/ moves onto it, forwarding X-Remote-User to the receipt server. 403 now goes to /no-access rather than back to the login page. The variable must not be given a server-level default: the subrequest re-runs the server rewrite phase and would blank it. The admin panel's Manage Users tab ticks permissions per user (setUserPermissions, permissionTypes). The login page is no longer admin-only in wording or destination: a non-admin lands on /account, which lists the services they can open; /no-access is the same view with a notice. /auth/validate-admin and every other admin-gated service are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
80 lines
2.5 KiB
Go
80 lines
2.5 KiB
Go
package services
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func fmtParams(method, path string, status int, ip string, d time.Duration) gin.LogFormatterParams {
|
|
return gin.LogFormatterParams{
|
|
Method: method,
|
|
Path: path,
|
|
StatusCode: status,
|
|
ClientIP: ip,
|
|
Latency: d,
|
|
}
|
|
}
|
|
|
|
func TestTrafficFormatterDropsQuietHeartbeats(t *testing.T) {
|
|
out := TrafficLogFormatter(fmtParams("GET", "/auth/validate-admin", 200, "172.28.0.6", 2*time.Millisecond))
|
|
if out != "" {
|
|
t.Fatalf("plain validate-admin 200 should be dropped, got %q", out)
|
|
}
|
|
}
|
|
|
|
func TestTrafficFormatterDropsPermissionGate(t *testing.T) {
|
|
if out := TrafficLogFormatter(fmtParams("GET", "/auth/validate/receipt", 200, "172.28.0.6", time.Millisecond)); out != "" {
|
|
t.Fatalf("permission-gate 200 should be dropped, got %q", out)
|
|
}
|
|
if out := TrafficLogFormatter(fmtParams("GET", "/auth/validate/receipt", 403, "172.28.0.6", time.Millisecond)); out == "" {
|
|
t.Fatal("a refused permission check must stay visible")
|
|
}
|
|
}
|
|
|
|
func TestTrafficFormatterKeepsHeartbeatFailures(t *testing.T) {
|
|
out := TrafficLogFormatter(fmtParams("GET", "/auth/validate-admin", 401, "172.28.0.6", time.Millisecond))
|
|
if out == "" {
|
|
t.Fatal("a failing heartbeat must stay visible")
|
|
}
|
|
if !strings.Contains(out, "401") || !strings.Contains(out, "172.28.0.6") {
|
|
t.Fatalf("failure line missing fields: %q", out)
|
|
}
|
|
}
|
|
|
|
func TestTrafficFormatterVisitorGetsIP(t *testing.T) {
|
|
out := TrafficLogFormatter(fmtParams("GET", "/", 200, "82.69.124.224", 15*time.Millisecond))
|
|
if !strings.Contains(out, "82.69.124.224") {
|
|
t.Fatalf("visitor line should carry the IP: %q", out)
|
|
}
|
|
}
|
|
|
|
func TestTrafficFormatterInfra200Lean(t *testing.T) {
|
|
out := TrafficLogFormatter(fmtParams("POST", "/graphql", 200, "172.28.0.6", 5*time.Millisecond))
|
|
if strings.Contains(out, "172.28.0.6") {
|
|
t.Fatalf("infrastructure success should not repeat its IP: %q", out)
|
|
}
|
|
}
|
|
|
|
func TestTrafficFormatterServerErrorMarked(t *testing.T) {
|
|
out := TrafficLogFormatter(fmtParams("GET", "/spotify/listening", 500, "82.69.124.224", time.Second))
|
|
if !strings.Contains(out, "!!! SERVER ERROR") {
|
|
t.Fatalf("server error should be marked: %q", out)
|
|
}
|
|
// Human latency units, not nanoseconds.
|
|
if strings.Contains(out, "ns") {
|
|
t.Fatalf("latency should be human units: %q", out)
|
|
}
|
|
}
|
|
|
|
func TestTrafficFormatterInfraIPClassification(t *testing.T) {
|
|
if !logIsInterestingClient("82.69.124.224") {
|
|
t.Fatal("public IP must read as a visitor")
|
|
}
|
|
if logIsInterestingClient("172.28.0.6") {
|
|
t.Fatal("docker-network IP must not read as a visitor")
|
|
}
|
|
}
|