-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.go
More file actions
65 lines (53 loc) · 1.77 KB
/
Copy pathtesting.go
File metadata and controls
65 lines (53 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package envelope
import (
"testing"
"azugo.io/azugo"
"azugo.io/azugo/token"
"azugo.io/azugo/user"
"github.com/go-quicktest/qt"
)
// TestApp builds an App for tests: no document wiring, the in-memory store, and a
// stub auth middleware driven by the X-Test-Scopes request header (production
// always uses the go-authbyte DPoP middleware).
func TestApp(tb testing.TB) *App {
tb.Helper()
tb.Setenv("METRICS_ENABLED", "false")
tb.Setenv("SERVICE_NAME", "envelope")
tb.Setenv("ENVIRONMENT", "development")
tb.Setenv("AUTH_ISSUER_URL", "http://localhost:8080")
tb.Setenv("SERVICE_AUDIENCE", "svc:envelope")
app, err := New(nil, "0.0.0-test")
qt.Assert(tb, qt.IsNil(err))
app.SetAuthMiddleware(TestAuthMiddleware())
return app
}
// TestAuthMiddleware authenticates requests from the X-Test-Scopes header
// (comma-separated scopes) and uses the optional X-Test-Sub header as the caller
// identity (default "svc:test-client"). The optional X-Test-Serial header sets the
// caller's eIDAS serial_number claim (the key that matches a caller to a signer slot).
// Requests without scopes are rejected 401 — mirroring the production contract.
func TestAuthMiddleware() azugo.RequestHandlerFunc {
return func(next azugo.RequestHandler) azugo.RequestHandler {
return func(ctx *azugo.Context) {
scopes := ctx.Header.Get("X-Test-Scopes")
if scopes == "" {
ctx.StatusCode(401)
ctx.Text("unauthorized")
return
}
sub := ctx.Header.Get("X-Test-Sub")
if sub == "" {
sub = "svc:test-client"
}
claims := map[string]token.ClaimStrings{
"sub": {sub},
"scope": {scopes},
}
if serial := ctx.Header.Get("X-Test-Serial"); serial != "" {
claims["serial_number"] = token.ClaimStrings{serial}
}
ctx.SetUser(user.New(claims))
next(ctx)
}
}
}