@sebspark/test-iap 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +36 -55
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- @sebspark/test-iap
1
+ # @sebspark/test-iap
2
2
 
3
3
  A tiny Identity-Aware Proxy for tests. It sits in front of your HTTP/WS target during E2E and rewrites Authorization to a short‑lived RS256 JWT — either minted locally or fetched from a remote token service.
4
4
 
@@ -6,36 +6,35 @@ Test-only plumbing. Intentionally noisy on errors; not for production.
6
6
 
7
7
  ⸻
8
8
 
9
- Why
9
+ ### Why
10
+
10
11
  • Run E2E without wiring a real IAM stack
11
12
  • Deterministic auth: inject claims, assert behavior
12
13
  • Works for HTTP and WebSocket targets
13
14
 
14
- Features
15
+ ### Features
16
+
15
17
  • Proxies HTTP + WS
16
18
  • Local mode: read Authorization: Bearer <base64url(JSON)>, mint JWT
17
19
  • Downstream mode: call a remote GET /api/token?… and forward its JWT
18
20
  • Claim mapping: user → preferred_username (if both exist, preferred_username wins)
19
21
  • RS256 with kid = JWK thumbprint, iat/nbf, exp=5m
20
22
 
21
- Requirements
22
- • Node 22 (for your tests)
23
- • Docker (used by Testcontainers)
24
- • A test runner (Vitest/Jest/etc.)
25
-
26
23
  ⸻
27
24
 
28
- Install
25
+ ## Install
29
26
 
27
+ ```sh
30
28
  yarn add -D @sebspark/test-iap
31
-
29
+ ```
32
30
 
33
31
  ⸻
34
32
 
35
- Quick start (Testcontainers)
33
+ ## Quick start (Testcontainers)
36
34
 
37
35
  Spin up the IAP in front of a service on your Docker network.
38
36
 
37
+ ```ts
39
38
  import { Network } from 'testcontainers'
40
39
  import { TestIapContainer } from '@sebspark/test-iap'
41
40
 
@@ -49,9 +48,11 @@ const iap = await new TestIapContainer()
49
48
  .start()
50
49
 
51
50
  const baseUrl = `http://${iap.getEndpoint()}`
51
+ ```
52
52
 
53
53
  Send requests through the IAP:
54
54
 
55
+ ```ts
55
56
  // Local mode expects Authorization to be base64url(JSON)
56
57
  const claims = { sub: 'u1', user: 'alice' }
57
58
  const token = Buffer.from(JSON.stringify(claims)).toString('base64url')
@@ -59,15 +60,17 @@ const token = Buffer.from(JSON.stringify(claims)).toString('base64url')
59
60
  const res = await fetch(`${baseUrl}/orders?q=1`, {
60
61
  headers: { authorization: `Bearer ${token}` }
61
62
  })
63
+ ```
62
64
 
63
- Your upstream will receive Authorization: Bearer <RS256-JWT>; payload contains your claims + defaults.
65
+ Your upstream will receive `Authorization: Bearer <RS256-JWT>` payload contains your claims + defaults.
64
66
 
65
67
  ⸻
66
68
 
67
- Downstream mode
69
+ ## Downstream mode
68
70
 
69
71
  Use a remote token service that returns a JWT string (response body):
70
72
 
73
+ ```ts
71
74
  const iap = await new TestIapContainer()
72
75
  .withNetwork(network)
73
76
  .withPort(3200)
@@ -75,73 +78,51 @@ const iap = await new TestIapContainer()
75
78
  .withMode('downstream')
76
79
  .withDownstream('https://issuer.example/api/token') // receives ?user=... etc.
77
80
  .start()
81
+ ```
82
+
83
+ `.withMode()` is optional - inferred as 'downstream' if `.withDownstream()` is set, else 'local'
78
84
 
79
- Incoming Authorization: Bearer <base64url(JSON)> → claims become query params to the downstream URL; its response text is used as the new JWT.
85
+ Incoming `Authorization: Bearer <base64url(JSON)>` → claims become query params to the downstream URL; its response text is used as the new JWT.
80
86
 
81
87
  ⸻
82
88
 
83
- API
89
+ ## API
84
90
 
91
+ Run the proxy in-process (useful for unit tests without Docker):
92
+
93
+ ```ts
85
94
  import {
86
- TestIapContainer,
87
- StartedTestIapContainer,
88
95
  createProxyServer,
89
96
  type ProxyConfig,
90
- type Mode
91
97
  } from '@sebspark/test-iap'
92
98
 
93
- TestIapContainer (preferred)
94
- • .withBaseImage(image: string) — default node:22-alpine
95
- • .withNetwork(network)
96
- • .withPort(port: number) — default 3000
97
- • .withTarget(url: string) — required, http(s):// or ws(s)://
98
- • .withMode(mode: 'local' | 'downstream') — optional; inferred as 'downstream' if .withDownstream() is set, else 'local'
99
- • .withDownstream(url: string) — absolute http(s) URL returning a JWT body
100
- • .start(): Promise<StartedTestIapContainer>
101
-
102
- StartedTestIapContainer:
103
- • .getEndpoint(): string → host:port (use with http:// in tests)
104
-
105
- createProxyServer(config: ProxyConfig)
106
-
107
- Run the proxy in-process (useful for unit tests without Docker):
108
-
109
- import { createProxyServer } from '@sebspark/test-iap'
110
-
111
- const server = createProxyServer({
99
+ const config: ProxyConfig = {
112
100
  target: 'http://localhost:3000',
113
- mode: 'local',
114
- downstream: '' // or 'https://issuer.example/api/token'
115
- })
116
- server.listen(3100)
117
-
118
- ProxyConfig:
119
-
120
- type Mode = 'local' | 'downstream'
121
-
122
- interface ProxyConfig {
123
- target: string // http(s) or ws(s) absolute URL
124
- mode?: Mode // default: local (unless downstream is set)
125
- downstream?: string // http(s) endpoint returning a JWT
101
+ downstream: 'https://issuer.example/api/token'
126
102
  }
103
+ const server = createProxyServer(config)
127
104
 
128
- The packaged entrypoint (dist/start.mjs) also accepts env vars: TARGET, MODE, DOWNSTREAM, PORT.
105
+ const port = 3100
106
+ server.listen(3100)
107
+ ```
129
108
 
130
109
  ⸻
131
110
 
132
- Auth behavior
111
+ ## Auth behavior
112
+
133
113
  • Incoming: Authorization: Bearer <access_token>
134
114
  • Local: <access_token> must be base64url-encoded JSON
135
115
  • Downstream: claims become URL query params
136
116
  • Mapping:
137
- • If preferred_username exists, use it
138
- • Else if user exists → emit preferred_username = user and drop user
117
+ • If preferred_username exists, use it
118
+ • Else if user exists → emit preferred_username = user and drop user
139
119
  • Outgoing: Authorization: Bearer <jwt>
140
120
  • RS256, kid = JWK thumbprint; iat, nbf, exp=5m
141
121
 
142
122
  ⸻
143
123
 
144
- Troubleshooting
124
+ ## Troubleshooting
125
+
145
126
  • 500 JSON error: the proxy returns { name, message, stack } to make debugging tests easy (by design).
146
127
  • “remote jwt fetch failed: …”: downstream returned non‑2xx.
147
128
  • ESM/CJS: the package publishes both; just import it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/test-iap",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",