passflow 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/dist/cli.mjs +1755 -0
- package/package.json +42 -3
- package/skills/REVISION +1 -0
- package/skills/index.json +405 -0
- package/skills/passflow-nextjs/SKILL.md +205 -0
- package/skills/passflow-nextjs/assets/.env.example +5 -0
- package/skills/passflow-nextjs/assets/app/account/page.tsx +29 -0
- package/skills/passflow-nextjs/assets/app/actions.example.ts +21 -0
- package/skills/passflow-nextjs/assets/app/api/me/route.ts +22 -0
- package/skills/passflow-nextjs/assets/components/auth-links.tsx +11 -0
- package/skills/passflow-nextjs/assets/lib/call-api.example.ts +15 -0
- package/skills/passflow-nextjs/assets/lib/passflow.ts +4 -0
- package/skills/passflow-nextjs/assets/proxy.ts +10 -0
- package/skills/passflow-nextjs/references/guardrails.md +87 -0
- package/skills/passflow-nextjs/scripts/index.json +405 -0
- package/skills/passflow-nextjs/scripts/lib.sh +1069 -0
- package/skills/passflow-nextjs/scripts/verify.sh +467 -0
- package/skills/passflow-node-api/SKILL.md +188 -0
- package/skills/passflow-node-api/assets/examples/explain-rejection.ts +22 -0
- package/skills/passflow-node-api/assets/examples/express-routes.ts +22 -0
- package/skills/passflow-node-api/assets/examples/fastify-routes.ts +28 -0
- package/skills/passflow-node-api/assets/examples/hono-routes.ts +19 -0
- package/skills/passflow-node-api/assets/src/express-cors.ts +11 -0
- package/skills/passflow-node-api/assets/src/express.ts +13 -0
- package/skills/passflow-node-api/assets/src/fastify-cors.ts +11 -0
- package/skills/passflow-node-api/assets/src/fastify.ts +14 -0
- package/skills/passflow-node-api/assets/src/fetch-handler.ts +13 -0
- package/skills/passflow-node-api/assets/src/hono-cors.ts +11 -0
- package/skills/passflow-node-api/assets/src/hono.ts +16 -0
- package/skills/passflow-node-api/assets/src/passflow.ts +10 -0
- package/skills/passflow-node-api/references/guardrails.md +66 -0
- package/skills/passflow-node-api/scripts/index.json +405 -0
- package/skills/passflow-node-api/scripts/lib.sh +1069 -0
- package/skills/passflow-node-api/scripts/verify.sh +283 -0
- package/skills/passflow-react-vite/SKILL.md +224 -0
- package/skills/passflow-react-vite/assets/server/express-cors.ts +9 -0
- package/skills/passflow-react-vite/assets/server/express.ts +14 -0
- package/skills/passflow-react-vite/assets/server/fastify-cors.ts +11 -0
- package/skills/passflow-react-vite/assets/server/fastify.ts +15 -0
- package/skills/passflow-react-vite/assets/server/hono-cors.ts +8 -0
- package/skills/passflow-react-vite/assets/server/hono.ts +17 -0
- package/skills/passflow-react-vite/assets/server/passflow.ts +19 -0
- package/skills/passflow-react-vite/assets/server/tsconfig.json +14 -0
- package/skills/passflow-react-vite/assets/src/passflow.tsx +28 -0
- package/skills/passflow-react-vite/assets/src/require-sign-in.tsx +35 -0
- package/skills/passflow-react-vite/assets/src/use-api.ts +23 -0
- package/skills/passflow-react-vite/references/guardrails.md +82 -0
- package/skills/passflow-react-vite/scripts/index.json +405 -0
- package/skills/passflow-react-vite/scripts/lib.sh +1069 -0
- package/skills/passflow-react-vite/scripts/verify.sh +678 -0
- package/skills/passflow-tanstack-start/SKILL.md +182 -0
- package/skills/passflow-tanstack-start/assets/.env.example +5 -0
- package/skills/passflow-tanstack-start/assets/src/components/auth-links.tsx +10 -0
- package/skills/passflow-tanstack-start/assets/src/lib/call-api.example.ts +14 -0
- package/skills/passflow-tanstack-start/assets/src/lib/passflow.ts +14 -0
- package/skills/passflow-tanstack-start/assets/src/lib/server-functions.example.ts +20 -0
- package/skills/passflow-tanstack-start/assets/src/routes/account.tsx +25 -0
- package/skills/passflow-tanstack-start/assets/src/routes/api/me.ts +25 -0
- package/skills/passflow-tanstack-start/assets/src/start.ts +14 -0
- package/skills/passflow-tanstack-start/references/guardrails.md +73 -0
- package/skills/passflow-tanstack-start/scripts/index.json +405 -0
- package/skills/passflow-tanstack-start/scripts/lib.sh +1069 -0
- package/skills/passflow-tanstack-start/scripts/verify.sh +470 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Checks a Next.js 16 App Router app that signs users in with @passflow/nextjs.
|
|
3
|
+
#
|
|
4
|
+
# bash verify.sh static [--dir DIR]
|
|
5
|
+
# bash verify.sh run [--dir DIR] [--port N] [--page PATH] [--api PATH]
|
|
6
|
+
#
|
|
7
|
+
# Exit 0: every check passed. 1: a check failed. 2: refused or usage error.
|
|
8
|
+
# Prints one "PASS <check>", "FAIL <check>: <reason>" or "SKIP <check>: <reason>" line per check,
|
|
9
|
+
# then "RESULT pass|fail".
|
|
10
|
+
set -euo pipefail
|
|
11
|
+
|
|
12
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
13
|
+
# shellcheck source=SCRIPTDIR/lib.sh
|
|
14
|
+
. "$SCRIPT_DIR/lib.sh"
|
|
15
|
+
|
|
16
|
+
STACK_ID='nextjs'
|
|
17
|
+
PACKAGE='@passflow/nextjs'
|
|
18
|
+
ENV_FILE_NAME='.env.local'
|
|
19
|
+
ENV_KEYS='PASSFLOW_ISSUER PASSFLOW_CLIENT_ID PASSFLOW_CLIENT_SECRET PASSFLOW_SECRET APP_BASE_URL'
|
|
20
|
+
SECRET_KEYS='PASSFLOW_CLIENT_SECRET PASSFLOW_SECRET'
|
|
21
|
+
NODE_MIN='22.12'
|
|
22
|
+
DEFAULT_PORT='3100'
|
|
23
|
+
DEFAULT_PAGE='/account'
|
|
24
|
+
DEFAULT_API='/api/me'
|
|
25
|
+
# Where the build puts the files the browser loads.
|
|
26
|
+
CLIENT_BUNDLE_DIRS='.next/static'
|
|
27
|
+
# Prerendered output that is also sent to browsers.
|
|
28
|
+
PRERENDER_DIR='.next/server/app'
|
|
29
|
+
PRERENDER_GLOBS='*.html *.rsc *.body *.meta'
|
|
30
|
+
|
|
31
|
+
usage() {
|
|
32
|
+
cat >&2 <<'EOF'
|
|
33
|
+
Usage:
|
|
34
|
+
bash verify.sh static [--dir DIR]
|
|
35
|
+
Install with lifecycle scripts off, type-check, build, search the client bundle for the
|
|
36
|
+
secrets' names and values, and scan the source for guardrail breaks.
|
|
37
|
+
Needs no env values and no network beyond the install.
|
|
38
|
+
|
|
39
|
+
bash verify.sh run [--dir DIR] [--port N] [--page PATH] [--api PATH]
|
|
40
|
+
Build, start the app with .env.local, and check its answers while signed out.
|
|
41
|
+
--port port for `next start`, passed as PORT (default 3100)
|
|
42
|
+
--page a protected page (default /account)
|
|
43
|
+
--api a protected route handler (default /api/me)
|
|
44
|
+
|
|
45
|
+
DIR defaults to the current directory.
|
|
46
|
+
Exit 0 pass, 1 a check failed, 2 refused or usage error.
|
|
47
|
+
EOF
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
MODE="${1:-}"
|
|
51
|
+
if [ "$MODE" != static ] && [ "$MODE" != run ]; then
|
|
52
|
+
usage
|
|
53
|
+
exit 2
|
|
54
|
+
fi
|
|
55
|
+
shift
|
|
56
|
+
|
|
57
|
+
INDEX="$(pf_index_file)"
|
|
58
|
+
DIR="$PWD"
|
|
59
|
+
PORT_ARG="$DEFAULT_PORT"
|
|
60
|
+
PAGE_PATH="$DEFAULT_PAGE"
|
|
61
|
+
API_PATH="$DEFAULT_API"
|
|
62
|
+
|
|
63
|
+
while [ "$#" -gt 0 ]; do
|
|
64
|
+
case "$1" in
|
|
65
|
+
--dir) DIR="${2:-}"; shift 2 || { usage; exit 2; } ;;
|
|
66
|
+
--port) PORT_ARG="${2:-}"; shift 2 || { usage; exit 2; } ;;
|
|
67
|
+
--page) PAGE_PATH="${2:-}"; shift 2 || { usage; exit 2; } ;;
|
|
68
|
+
--api) API_PATH="${2:-}"; shift 2 || { usage; exit 2; } ;;
|
|
69
|
+
-h | --help) usage; exit 2 ;;
|
|
70
|
+
*) printf 'Unknown argument: %s\n' "$1" >&2; usage; exit 2 ;;
|
|
71
|
+
esac
|
|
72
|
+
done
|
|
73
|
+
|
|
74
|
+
if [ -z "$DIR" ] || [ ! -f "$DIR/package.json" ]; then
|
|
75
|
+
printf 'REFUSED: no package.json in %s\n' "${DIR:-<empty>}" >&2
|
|
76
|
+
exit 2
|
|
77
|
+
fi
|
|
78
|
+
DIR="$(cd "$DIR" && pwd)"
|
|
79
|
+
case "$PORT_ARG" in
|
|
80
|
+
'' | *[!0-9]*) printf 'REFUSED: --port must be a number\n' >&2; exit 2 ;;
|
|
81
|
+
esac
|
|
82
|
+
for p in "$PAGE_PATH" "$API_PATH"; do
|
|
83
|
+
case "$p" in
|
|
84
|
+
/*) ;;
|
|
85
|
+
*) printf 'REFUSED: --page and --api must start with /\n' >&2; exit 2 ;;
|
|
86
|
+
esac
|
|
87
|
+
done
|
|
88
|
+
|
|
89
|
+
pf_require_tool node || exit 2
|
|
90
|
+
if ! pf_node_at_least "$NODE_MIN"; then
|
|
91
|
+
printf 'REFUSED: Node.js %s or newer is required\n' "$NODE_MIN" >&2
|
|
92
|
+
exit 2
|
|
93
|
+
fi
|
|
94
|
+
|
|
95
|
+
STACK="$(pf_detect_stack "$DIR" "$INDEX")"
|
|
96
|
+
case "$STACK" in
|
|
97
|
+
"$STACK_ID") ;;
|
|
98
|
+
refused:*) printf 'REFUSED: %s\n' "$(pf_stack_refusal "$DIR" "$INDEX" "${STACK#refused:}")" >&2; exit 2 ;;
|
|
99
|
+
*) printf 'REFUSED: package.json does not describe a Next.js app (detected: %s)\n' "$STACK" >&2; exit 2 ;;
|
|
100
|
+
esac
|
|
101
|
+
if [ -d "$DIR/src/app" ]; then
|
|
102
|
+
ROOT="$DIR/src"
|
|
103
|
+
elif [ -d "$DIR/app" ]; then
|
|
104
|
+
ROOT="$DIR"
|
|
105
|
+
else
|
|
106
|
+
printf 'REFUSED: no app/ or src/app/ directory; this skill needs the App Router\n' >&2
|
|
107
|
+
exit 2
|
|
108
|
+
fi
|
|
109
|
+
# "src/" or "" (the project root), for messages.
|
|
110
|
+
REL_ROOT="${ROOT#"$DIR"}"
|
|
111
|
+
REL_ROOT="${REL_ROOT#/}"
|
|
112
|
+
REL_ROOT="${REL_ROOT:+$REL_ROOT/}"
|
|
113
|
+
|
|
114
|
+
PM="$(pf_detect_pm "$DIR")"
|
|
115
|
+
if ! pf_pm_supported "$PM"; then
|
|
116
|
+
printf 'REFUSED: %s\n' "$(pf_pm_refusal "$PM")" >&2
|
|
117
|
+
exit 2
|
|
118
|
+
fi
|
|
119
|
+
pf_require_tool "$(pf_pm_tool "$PM")" || exit 2
|
|
120
|
+
|
|
121
|
+
# ---------------------------------------------------------------- checks used by both modes
|
|
122
|
+
|
|
123
|
+
# Names Next.js reads the proxy from: proxy (Next.js 16) or the older middleware, in TypeScript or JavaScript.
|
|
124
|
+
PROXY_FILES='proxy.ts proxy.js middleware.ts middleware.js'
|
|
125
|
+
|
|
126
|
+
# The proxy file sits beside app/: one of PROXY_FILES, which an existing project may already have in any of
|
|
127
|
+
# its forms. Exactly one may exist there, and it must call passflow.proxy.
|
|
128
|
+
check_proxy() {
|
|
129
|
+
local f found='' present='' other_root='' other='' rel
|
|
130
|
+
for f in $PROXY_FILES; do
|
|
131
|
+
if [ -f "$ROOT/$f" ]; then
|
|
132
|
+
present="$present ${REL_ROOT}$f"
|
|
133
|
+
if grep -q 'passflow\.proxy(' "$ROOT/$f"; then found="$f"; fi
|
|
134
|
+
fi
|
|
135
|
+
done
|
|
136
|
+
# With src/app, Next.js reads src/proxy.*; a proxy file at the project root is ignored (and the other way round).
|
|
137
|
+
if [ "$ROOT" = "$DIR/src" ]; then other_root="$DIR"; rel=''; else other_root="$DIR/src"; rel='src/'; fi
|
|
138
|
+
for f in $PROXY_FILES; do
|
|
139
|
+
if [ -f "$other_root/$f" ]; then other="$other $rel$f"; fi
|
|
140
|
+
done
|
|
141
|
+
# shellcheck disable=SC2086
|
|
142
|
+
set -- $present
|
|
143
|
+
if [ "$#" -gt 1 ]; then
|
|
144
|
+
pf_fail proxy "more than one proxy file:$present; Next.js reads only one, so merge them into the existing one"
|
|
145
|
+
elif [ -n "$other" ]; then
|
|
146
|
+
pf_fail proxy "Next.js ignores$other here; the proxy file belongs in ${REL_ROOT:-the project root} beside app/"
|
|
147
|
+
elif [ -n "$found" ]; then
|
|
148
|
+
pf_pass "proxy (${REL_ROOT}$found calls passflow.proxy)"
|
|
149
|
+
elif [ -n "$present" ]; then
|
|
150
|
+
pf_fail proxy "${present# } does not call passflow.proxy(request); merge it into that file"
|
|
151
|
+
else
|
|
152
|
+
pf_fail proxy "no ${REL_ROOT}proxy.ts calls passflow.proxy(request)"
|
|
153
|
+
fi
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
# has_directive FILE 'use client': 0 when the directive is FILE's first statement, after any comments and
|
|
157
|
+
# blank lines.
|
|
158
|
+
has_directive() {
|
|
159
|
+
awk -v d="$2" '
|
|
160
|
+
BEGIN { found = 1 }
|
|
161
|
+
inblock { if (index($0, "*/")) { inblock = 0 } ; next }
|
|
162
|
+
/^[[:space:]]*$/ { next }
|
|
163
|
+
/^[[:space:]]*\/\// { next }
|
|
164
|
+
/^[[:space:]]*\/\*/ { if (!index($0, "*/")) inblock = 1; next }
|
|
165
|
+
{
|
|
166
|
+
s = $0; sub(/^[[:space:]]+/, "", s); sub(/[[:space:]\r]+$/, "", s)
|
|
167
|
+
if (s == "\"" d "\"" || s == "\"" d "\";" || s == "'\''" d "'\''" || s == "'\''" d "'\'';") found = 0
|
|
168
|
+
exit
|
|
169
|
+
}
|
|
170
|
+
END { exit found }' "$1" 2>/dev/null
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
is_client_file() { has_directive "$1" 'use client'; }
|
|
174
|
+
|
|
175
|
+
SERVER_MODULE_IMPORT="from[[:space:]]*['\"]([^'\"]*/)?(lib/passflow|@passflow/nextjs)(\\.[jt]sx?)?['\"]"
|
|
176
|
+
|
|
177
|
+
# A client component must not import the server module, directly or through a module of the app that imports
|
|
178
|
+
# it: it would ship to the browser and fail there.
|
|
179
|
+
client_import_scan() {
|
|
180
|
+
local files f stem stems='' hits=''
|
|
181
|
+
files="$(_pf_source_files "$DIR")"
|
|
182
|
+
# Server-side modules of the app that import the Passflow server module, by file name without extension.
|
|
183
|
+
# A 'use server' file is left out: a client component may import server actions.
|
|
184
|
+
while IFS= read -r f; do
|
|
185
|
+
[ -n "$f" ] || continue
|
|
186
|
+
if ! is_client_file "$f" && ! has_directive "$f" 'use server' && grep -qE "$SERVER_MODULE_IMPORT" "$f"; then
|
|
187
|
+
stem="$(basename "$f")"
|
|
188
|
+
stem="${stem%.*}"
|
|
189
|
+
case "$stem" in
|
|
190
|
+
index | page | layout | route | passflow | proxy | middleware | *[!A-Za-z0-9_-]*) ;;
|
|
191
|
+
*) stems="$stems|$stem" ;;
|
|
192
|
+
esac
|
|
193
|
+
fi
|
|
194
|
+
done <<EOF
|
|
195
|
+
$files
|
|
196
|
+
EOF
|
|
197
|
+
stems="${stems#|}"
|
|
198
|
+
while IFS= read -r f; do
|
|
199
|
+
[ -n "$f" ] || continue
|
|
200
|
+
is_client_file "$f" || continue
|
|
201
|
+
if grep -qE "$SERVER_MODULE_IMPORT" "$f"; then
|
|
202
|
+
hits="$hits client component imports the server-only Passflow module: $f
|
|
203
|
+
"
|
|
204
|
+
elif [ -n "$stems" ] && grep -qE "from[[:space:]]*['\"]([^'\"]*/)?($stems)(\\.[jt]sx?)?['\"]" "$f"; then
|
|
205
|
+
hits="$hits client component imports a module that uses the server-only Passflow module: $f
|
|
206
|
+
"
|
|
207
|
+
fi
|
|
208
|
+
done <<EOF
|
|
209
|
+
$files
|
|
210
|
+
EOF
|
|
211
|
+
if [ -n "$hits" ]; then
|
|
212
|
+
printf '%s' "$hits"
|
|
213
|
+
return 1
|
|
214
|
+
fi
|
|
215
|
+
return 0
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
# ---------------------------------------------------------------- static
|
|
219
|
+
|
|
220
|
+
run_static() {
|
|
221
|
+
local rc d scanned=0 found=0 bundle_dirs
|
|
222
|
+
|
|
223
|
+
if pf_has_dep "$DIR" "$PACKAGE"; then
|
|
224
|
+
pf_pass "dependency $PACKAGE"
|
|
225
|
+
else
|
|
226
|
+
pf_fail "dependency $PACKAGE" "not in dependencies or devDependencies of package.json"
|
|
227
|
+
fi
|
|
228
|
+
|
|
229
|
+
check_proxy
|
|
230
|
+
|
|
231
|
+
rc=0
|
|
232
|
+
pf_install "$DIR" >"$WORK/install.log" 2>&1 || rc=$?
|
|
233
|
+
if [ "$rc" -eq 0 ]; then
|
|
234
|
+
pf_pass "install ($PM)"
|
|
235
|
+
else
|
|
236
|
+
pf_fail "install ($PM)" "exit $rc; log: $WORK/install.log"
|
|
237
|
+
return 0
|
|
238
|
+
fi
|
|
239
|
+
|
|
240
|
+
# The build comes first: next build writes next-env.d.ts (git-ignored in a fresh clone), which the
|
|
241
|
+
# type-check needs for CSS-module and image imports.
|
|
242
|
+
if pf_has_script "$DIR" build; then
|
|
243
|
+
rc=0
|
|
244
|
+
pf_run_script "$DIR" build >"$WORK/build.log" 2>&1 || rc=$?
|
|
245
|
+
if [ "$rc" -eq 0 ]; then
|
|
246
|
+
pf_pass build
|
|
247
|
+
else
|
|
248
|
+
pf_fail build "exit $rc; log: $WORK/build.log"
|
|
249
|
+
fi
|
|
250
|
+
|
|
251
|
+
if [ "$rc" -ne 0 ]; then
|
|
252
|
+
# The folders hold an older build: scanning them would prove nothing.
|
|
253
|
+
pf_skip "client bundle" "the build failed"
|
|
254
|
+
else
|
|
255
|
+
bundle_dirs="$CLIENT_BUNDLE_DIRS"
|
|
256
|
+
: >"$WORK/bundle.log"
|
|
257
|
+
for d in $bundle_dirs; do
|
|
258
|
+
rc=0
|
|
259
|
+
# shellcheck disable=SC2086
|
|
260
|
+
pf_scan_bundle "$DIR/$d" "$DIR/$ENV_FILE_NAME" $SECRET_KEYS >>"$WORK/bundle.log" 2>&1 || rc=$?
|
|
261
|
+
case "$rc" in
|
|
262
|
+
0) scanned=1 ;;
|
|
263
|
+
1) scanned=1; found=1 ;;
|
|
264
|
+
esac
|
|
265
|
+
done
|
|
266
|
+
# Prerendered pages and RSC payloads are sent to every visitor too. Server code there names the
|
|
267
|
+
# secrets legitimately, so only their values are searched.
|
|
268
|
+
rc=0
|
|
269
|
+
# shellcheck disable=SC2086
|
|
270
|
+
pf_scan_values "$DIR/$PRERENDER_DIR" "$DIR/$ENV_FILE_NAME" "$PRERENDER_GLOBS" $SECRET_KEYS >>"$WORK/bundle.log" 2>&1 || rc=$?
|
|
271
|
+
[ "$rc" -ne 1 ] || found=1
|
|
272
|
+
if [ "$scanned" -eq 0 ]; then
|
|
273
|
+
pf_fail "client bundle" "no client bundle in: $(printf '%s' "$bundle_dirs" | tr '\n' ' ')"
|
|
274
|
+
elif [ "$found" -eq 1 ]; then
|
|
275
|
+
pf_fail "client bundle" "a secret's name or value is in the files sent to the browser"
|
|
276
|
+
cat "$WORK/bundle.log"
|
|
277
|
+
else
|
|
278
|
+
pf_pass "client bundle has no secret names or values; prerendered pages have no secret values"
|
|
279
|
+
fi
|
|
280
|
+
fi
|
|
281
|
+
else
|
|
282
|
+
pf_skip build "no build script"
|
|
283
|
+
pf_skip "client bundle" "nothing was built"
|
|
284
|
+
fi
|
|
285
|
+
|
|
286
|
+
rc=0
|
|
287
|
+
pf_typecheck "$DIR" >"$WORK/typecheck.log" 2>&1 || rc=$?
|
|
288
|
+
case "$rc" in
|
|
289
|
+
0) pf_pass typecheck ;;
|
|
290
|
+
3) pf_skip typecheck "no typecheck script and no tsconfig.json" ;;
|
|
291
|
+
*) pf_fail typecheck "exit $rc; log: $WORK/typecheck.log" ;;
|
|
292
|
+
esac
|
|
293
|
+
|
|
294
|
+
rc=0
|
|
295
|
+
pf_guardrail_scan "$DIR" server >"$WORK/guardrails.log" 2>&1 || rc=$?
|
|
296
|
+
client_import_scan >>"$WORK/guardrails.log" 2>&1 || rc=1
|
|
297
|
+
if [ "$rc" -eq 0 ]; then
|
|
298
|
+
pf_pass guardrails
|
|
299
|
+
else
|
|
300
|
+
pf_fail guardrails "see below"
|
|
301
|
+
cat "$WORK/guardrails.log"
|
|
302
|
+
fi
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
# ---------------------------------------------------------------- run
|
|
306
|
+
|
|
307
|
+
stop_server() {
|
|
308
|
+
if [ -n "$PF_BG_PID" ]; then
|
|
309
|
+
pf_kill_tree "$PF_BG_PID"
|
|
310
|
+
PF_BG_PID=''
|
|
311
|
+
fi
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
run_live() {
|
|
315
|
+
local env_file="$DIR/$ENV_FILE_NAME" missing base status location body_has issuer rc app_origin
|
|
316
|
+
if [ ! -f "$env_file" ]; then
|
|
317
|
+
printf 'REFUSED: %s not found; run mode needs it\n' "$env_file" >&2
|
|
318
|
+
exit 2
|
|
319
|
+
fi
|
|
320
|
+
# shellcheck disable=SC2086
|
|
321
|
+
missing="$(pf_env_missing_keys "$env_file" $ENV_KEYS)"
|
|
322
|
+
if [ -n "$missing" ]; then
|
|
323
|
+
printf 'REFUSED: %s has no value for: %s\n' "$ENV_FILE_NAME" "$(printf '%s' "$missing" | tr '\n' ' ')" >&2
|
|
324
|
+
exit 2
|
|
325
|
+
fi
|
|
326
|
+
pf_require_tool curl || exit 2
|
|
327
|
+
if ! pf_has_script "$DIR" build || ! pf_has_script "$DIR" start; then
|
|
328
|
+
printf 'REFUSED: package.json needs a build and a start script (next build, next start)\n' >&2
|
|
329
|
+
exit 2
|
|
330
|
+
fi
|
|
331
|
+
base="http://localhost:$PORT_ARG"
|
|
332
|
+
if ! pf_port_free "$PORT_ARG"; then
|
|
333
|
+
printf 'REFUSED: %s\n' "$(pf_port_busy_refusal "$PORT_ARG")" >&2
|
|
334
|
+
exit 2
|
|
335
|
+
fi
|
|
336
|
+
|
|
337
|
+
# Only the stack's own names; next build and next start read the rest of .env.local themselves.
|
|
338
|
+
# shellcheck disable=SC2086
|
|
339
|
+
pf_load_env "$env_file" $ENV_KEYS
|
|
340
|
+
|
|
341
|
+
rc=0
|
|
342
|
+
pf_run_script "$DIR" build >"$WORK/build.log" 2>&1 || rc=$?
|
|
343
|
+
if [ "$rc" -eq 0 ]; then
|
|
344
|
+
pf_pass build
|
|
345
|
+
else
|
|
346
|
+
pf_fail build "exit $rc; log: $WORK/build.log"
|
|
347
|
+
return 0
|
|
348
|
+
fi
|
|
349
|
+
|
|
350
|
+
export PORT="$PORT_ARG"
|
|
351
|
+
pf_traps stop_server
|
|
352
|
+
case "$PM" in
|
|
353
|
+
npm) pf_start_bg "$DIR" "$WORK/server.log" npm run start ;;
|
|
354
|
+
pnpm) pf_start_bg "$DIR" "$WORK/server.log" pnpm run start ;;
|
|
355
|
+
yarn-classic) pf_start_bg "$DIR" "$WORK/server.log" yarn run start ;;
|
|
356
|
+
esac
|
|
357
|
+
|
|
358
|
+
if pf_wait_http "$base/" 90 "$PF_BG_PID"; then
|
|
359
|
+
pf_pass "server starts on port $PORT_ARG (start script)"
|
|
360
|
+
elif pf_alive "$PF_BG_PID"; then
|
|
361
|
+
pf_fail "server starts on port $PORT_ARG (start script)" "no answer within 90 s; log: $WORK/server.log"
|
|
362
|
+
return 0
|
|
363
|
+
else
|
|
364
|
+
pf_fail "server starts on port $PORT_ARG (start script)" "the process exited; log: $WORK/server.log"
|
|
365
|
+
return 0
|
|
366
|
+
fi
|
|
367
|
+
pf_check_listener "$PORT_ARG" "$PF_BG_PID"
|
|
368
|
+
|
|
369
|
+
# 1. A protected page, signed out: no protected content, and the way out is sign-in. With
|
|
370
|
+
# cacheComponents the redirect can arrive in the stream after a 200, so the status is not checked.
|
|
371
|
+
pf_http "$WORK/page" GET "$base$PAGE_PATH" || true
|
|
372
|
+
status="$(cat "$WORK/page/status")"
|
|
373
|
+
location="$(pf_header "$WORK/page" Location)"
|
|
374
|
+
body_has=0
|
|
375
|
+
if grep -q 'data-passflow-protected' "$WORK/page/body"; then body_has=1; fi
|
|
376
|
+
if [ "$body_has" -eq 1 ]; then
|
|
377
|
+
pf_fail "GET $PAGE_PATH signed out" "status $status; the body has data-passflow-protected content"
|
|
378
|
+
else
|
|
379
|
+
case "$location" in
|
|
380
|
+
*/auth/login*) pf_pass "GET $PAGE_PATH signed out: no protected content, redirect to /auth/login ($status)" ;;
|
|
381
|
+
*)
|
|
382
|
+
# A streamed redirect, not a sign-in link in the page's navigation.
|
|
383
|
+
if grep -qE 'NEXT_REDIRECT;[a-z]+;[^;]*/auth/login|http-equiv="refresh"[^>]*/auth/login' "$WORK/page/body"; then
|
|
384
|
+
pf_pass "GET $PAGE_PATH signed out: no protected content, the stream redirects to /auth/login ($status)"
|
|
385
|
+
else
|
|
386
|
+
pf_fail "GET $PAGE_PATH signed out" "status $status; no redirect to /auth/login in Location or in the stream"
|
|
387
|
+
fi
|
|
388
|
+
;;
|
|
389
|
+
esac
|
|
390
|
+
fi
|
|
391
|
+
|
|
392
|
+
# 2. A protected route handler, signed out: 401 with no-store, never a redirect.
|
|
393
|
+
pf_http "$WORK/api" GET "$base$API_PATH" || true
|
|
394
|
+
status="$(cat "$WORK/api/status")"
|
|
395
|
+
if [ "$status" = 401 ] && pf_has_token "$(pf_header "$WORK/api" Cache-Control)" no-store; then
|
|
396
|
+
pf_pass "GET $API_PATH signed out: 401, Cache-Control no-store"
|
|
397
|
+
else
|
|
398
|
+
pf_fail "GET $API_PATH signed out" "status $status, Cache-Control '$(pf_header "$WORK/api" Cache-Control)'; want 401 and no-store (status 000: no answer)"
|
|
399
|
+
fi
|
|
400
|
+
|
|
401
|
+
# 3. Sign-in starts at the issuer with PKCE and sets the transaction cookie.
|
|
402
|
+
pf_http "$WORK/login" GET "$base/auth/login" || true
|
|
403
|
+
status="$(cat "$WORK/login/status")"
|
|
404
|
+
location="$(pf_header "$WORK/login" Location)"
|
|
405
|
+
issuer="${PASSFLOW_ISSUER%/}"
|
|
406
|
+
case "$status" in
|
|
407
|
+
30[1278])
|
|
408
|
+
if [ "${location#"$issuer"/oidc/authorize}" = "$location" ]; then
|
|
409
|
+
pf_fail "GET /auth/login" "Location is not PASSFLOW_ISSUER/oidc/authorize"
|
|
410
|
+
elif ! printf '%s' "$location" | grep -q 'code_challenge_method=S256'; then
|
|
411
|
+
pf_fail "GET /auth/login" "Location has no code_challenge_method=S256"
|
|
412
|
+
else
|
|
413
|
+
pf_pass "GET /auth/login: $status to the issuer's /oidc/authorize with S256"
|
|
414
|
+
fi
|
|
415
|
+
;;
|
|
416
|
+
5??) pf_fail "GET /auth/login" "status $status; the app could not read the issuer's discovery document (PASSFLOW_ISSUER/.well-known/openid-configuration); log: $WORK/server.log" ;;
|
|
417
|
+
*) pf_fail "GET /auth/login" "status $status; want a redirect" ;;
|
|
418
|
+
esac
|
|
419
|
+
if tr -d '\r' <"$WORK/login/headers" | grep -iE '^set-cookie:[[:space:]]*(__Host-)?passflow_txn=' | grep -qi 'httponly'; then
|
|
420
|
+
pf_pass "GET /auth/login sets an HttpOnly transaction cookie (passflow_txn, or __Host-passflow_txn on https)"
|
|
421
|
+
else
|
|
422
|
+
pf_fail "GET /auth/login" "no HttpOnly passflow_txn or __Host-passflow_txn cookie"
|
|
423
|
+
fi
|
|
424
|
+
location=''
|
|
425
|
+
issuer=''
|
|
426
|
+
|
|
427
|
+
# 4. A route handler that changes data refuses another origin before anything else, and answers the
|
|
428
|
+
# app's own origin with its signed-out 401. A route with no POST handler (405) is skipped.
|
|
429
|
+
pf_http "$WORK/cross" POST "$base$API_PATH" -H 'Origin: https://evil.example' || true
|
|
430
|
+
status="$(cat "$WORK/cross/status")"
|
|
431
|
+
case "$status" in
|
|
432
|
+
403) pf_pass "cross-origin POST $API_PATH: 403" ;;
|
|
433
|
+
405) pf_skip "POST $API_PATH" "the route has no POST handler" ;;
|
|
434
|
+
*) pf_fail "cross-origin POST $API_PATH" "status $status; want 403 from isSameOrigin" ;;
|
|
435
|
+
esac
|
|
436
|
+
if [ "$status" != 405 ]; then
|
|
437
|
+
app_origin="$(printf '%s' "$APP_BASE_URL" | sed -E 's#^([A-Za-z][A-Za-z0-9+.-]*://[^/?#]+).*#\1#')"
|
|
438
|
+
pf_http "$WORK/same" POST "$base$API_PATH" -H "Origin: $app_origin" || true
|
|
439
|
+
app_origin=''
|
|
440
|
+
status="$(cat "$WORK/same/status")"
|
|
441
|
+
if [ "$status" = 401 ]; then
|
|
442
|
+
pf_pass "same-origin POST $API_PATH signed out: 401"
|
|
443
|
+
else
|
|
444
|
+
pf_fail "same-origin POST $API_PATH signed out" "status $status; want 401 (the app's own origin is APP_BASE_URL's)"
|
|
445
|
+
fi
|
|
446
|
+
fi
|
|
447
|
+
|
|
448
|
+
if ! pf_alive "$PF_BG_PID"; then
|
|
449
|
+
pf_fail "server still running after the checks" "the process exited; log: $WORK/server.log"
|
|
450
|
+
fi
|
|
451
|
+
stop_server
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
WORK="$(mktemp -d "$PF_TMP/passflow-verify.XXXXXX")"
|
|
455
|
+
printf 'verify.sh %s: %s (%s, %s)\n' "$MODE" "$DIR" "$STACK_ID" "$PM"
|
|
456
|
+
printf 'logs: %s\n' "$WORK"
|
|
457
|
+
|
|
458
|
+
if [ "$MODE" = static ]; then
|
|
459
|
+
run_static
|
|
460
|
+
else
|
|
461
|
+
run_live
|
|
462
|
+
fi
|
|
463
|
+
|
|
464
|
+
if pf_result; then
|
|
465
|
+
exit 0
|
|
466
|
+
fi
|
|
467
|
+
exit 1
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: passflow-node-api
|
|
3
|
+
description: Protects a Node.js HTTP API with Passflow access tokens using @passflow/node, on Express, Fastify or Hono. Use when asked to add Passflow auth to a Node backend, verify Passflow bearer tokens, or protect API routes with Passflow. Applies when the root package.json depends on express, fastify or hono, and not on next, @tanstack/react-start, or vite together with react (those have their own Passflow skills).
|
|
4
|
+
compatibility: Node.js 22.12 or newer; Express 4 or 5, Fastify 5, Hono 4; npm, pnpm or Yarn 1.
|
|
5
|
+
license: MIT
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Passflow for a Node API
|
|
9
|
+
|
|
10
|
+
## What this does
|
|
11
|
+
|
|
12
|
+
Adds Passflow access-token checks to an existing Node API. One verifier is built at startup from two env vars. Protected routes answer 401 with a `WWW-Authenticate` challenge when the token is missing or invalid. Routes get the verified claims. No app is created and no sign-in page is added: the API accepts tokens that a Passflow app issued.
|
|
13
|
+
|
|
14
|
+
The only package this skill adds is `@passflow/node`. It never adds another dependency, never edits `package.json` scripts and never creates or edits a `.env*` file (`.env.example` included). Whatever else the developer has to do is listed in the last step.
|
|
15
|
+
|
|
16
|
+
## Where this runs
|
|
17
|
+
|
|
18
|
+
The skill runs on one of two paths. Where a step differs, it is labelled.
|
|
19
|
+
|
|
20
|
+
- **On the developer's machine** (`npx passflow integrate`, or an agent the developer runs): you have a shell. You install with the package manager and run `verify.sh` yourself.
|
|
21
|
+
- **In a Passflow GitHub App run**: you have no shell. Your tools are Read, Write, Edit, Glob, Grep and Skill, plus `add_passflow_package({ name })`, `run_build()` and `run_typecheck()`. The last two return an exit code and a short tail of the output. After you finish, the runner runs the install, `run_build()`, `run_typecheck()` and then `verify.sh static`. It never runs `verify.sh run`, because the issuer and the client secret stay out of the sandbox: the draft PR's description tells the developer to run `bash <skill>/scripts/verify.sh run` on their machine before merging.
|
|
22
|
+
|
|
23
|
+
Every other step uses only reading and writing files, so it is the same on both paths.
|
|
24
|
+
|
|
25
|
+
## Preconditions
|
|
26
|
+
|
|
27
|
+
Check each one before you change a file. Read `package.json` and look for the files named below with Glob (Grep for a line inside one). If one fails, stop and tell the user why.
|
|
28
|
+
|
|
29
|
+
1. **Stack.** The root `package.json` lists `express`, `fastify` or `hono` in `dependencies` or `devDependencies`. If it lists `@tanstack/react-start` with `@tanstack/react-router`, `next`, or both `vite` and `react`, use that stack's Passflow skill instead. Stacks are tried in this order, by package names, and the first match wins: TanStack Start, Next.js, React + Vite, Node API. If the first match fails its version rule, the project is refused; it never falls through to a later stack.
|
|
30
|
+
2. **Node.js 22.12 or newer.** On the developer's machine, check `node --version`. In a Passflow GitHub App run, the runner checks for Node.js 22.12 or newer; you do not.
|
|
31
|
+
3. **Package manager.** Look at the project root, which takes only `package.json` and a Glob for the lockfiles:
|
|
32
|
+
- a `packageManager` field in `package.json` that names `yarn@2` or newer, or `bun`, means Yarn Berry or Bun;
|
|
33
|
+
- an `npm-shrinkwrap.json`: stop. npm would write the change into it, and this skill updates `package-lock.json`, `pnpm-lock.yaml` or `yarn.lock` only;
|
|
34
|
+
- otherwise the lockfiles, in this order: `pnpm-lock.yaml` → pnpm, `package-lock.json` → npm, `yarn.lock` → Yarn 1 (classic), `.yarnrc.yml` → Yarn Berry, `bun.lock` or `bun.lockb` → Bun. If any file present points to Yarn Berry or Bun, that decides; otherwise the first one present does. A `yarn.lock` with a `__metadata:` line (Grep for it) is also Yarn Berry;
|
|
35
|
+
- no lockfile at all: stop. The change needs a committed lockfile. Tell the user to install once with npm, pnpm or Yarn 1 (classic) and commit the lockfile;
|
|
36
|
+
- a `packageManager` field naming npm, pnpm or Yarn 1 while the lockfile belongs to another manager (`pnpm@9` beside `package-lock.json`): stop. Tell the user to make them agree.
|
|
37
|
+
|
|
38
|
+
**Yarn Berry or Bun: stop.** Tell the user that this skill supports npm, pnpm and Yarn 1 only.
|
|
39
|
+
4. **Env file `.env`** at the project root, with `PASSFLOW_ISSUER` and `PASSFLOW_AUDIENCE`. You never create or edit it, and you never print its values.
|
|
40
|
+
- `npx passflow integrate` writes it. In a Passflow GitHub App run, the service writes `.env.example` with the public values filled in (`PASSFLOW_ISSUER`, `PASSFLOW_AUDIENCE`) and any secret empty; this stack has no secret. Leave that file as it is. If `.env` is missing or a value is empty, do not invent values: carry on with the code, and the last step tells the developer how to get them (the issuer URL and the client ID of the Passflow app whose tokens this API accepts).
|
|
41
|
+
- In a Passflow GitHub App run there is no `.env`: that is expected. Do not look for its values.
|
|
42
|
+
- A committed `.env`: on the developer's machine, check with `git ls-files --error-unmatch .env`. If it succeeds, git tracks the file: stop. Tell the user to run `git rm --cached .env`, commit, and add `.env` to `.gitignore`. In a Passflow GitHub App run, the service refuses a repository with a committed `.env`, `.env.local` or `.env.*.local` before you start, so there is nothing to check.
|
|
43
|
+
|
|
44
|
+
## Steps
|
|
45
|
+
|
|
46
|
+
1. **Install the package.** Install nothing else.
|
|
47
|
+
|
|
48
|
+
On the developer's machine, use the project's package manager. The version comes from `PASSFLOW_NODE_VERSION`; unset means the latest release. Run the line as it is:
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
npm install "@passflow/node${PASSFLOW_NODE_VERSION:+@$PASSFLOW_NODE_VERSION}"
|
|
52
|
+
pnpm add "@passflow/node${PASSFLOW_NODE_VERSION:+@$PASSFLOW_NODE_VERSION}"
|
|
53
|
+
yarn add "@passflow/node${PASSFLOW_NODE_VERSION:+@$PASSFLOW_NODE_VERSION}"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
In a Passflow GitHub App run, call `add_passflow_package({ name: "@passflow/node" })`. It runs the detected package manager's add with lifecycle scripts off. The service pins the version, and the tool adds that exact version. Do not edit `package.json` or the lockfile by hand. If it returns a non-zero exit code, stop and report its tail.
|
|
57
|
+
|
|
58
|
+
2. **Copy the assets.** Copy `assets/src/passflow.ts` and the file for the framework the project uses. Do not change the Passflow calls in them.
|
|
59
|
+
|
|
60
|
+
| Asset | Target | When |
|
|
61
|
+
|---|---|---|
|
|
62
|
+
| `assets/src/passflow.ts` | `src/passflow.ts` | Always. |
|
|
63
|
+
| `assets/src/express.ts` | `src/passflow-express.ts` | Express. |
|
|
64
|
+
| `assets/src/fastify.ts` | `src/passflow-fastify.ts` | Fastify. |
|
|
65
|
+
| `assets/src/hono.ts` | `src/passflow-hono.ts` | Hono. |
|
|
66
|
+
| `assets/src/express-cors.ts` | `src/passflow-express-cors.ts` | Express, called from a browser on another origin, and `cors` is already a dependency. See step 3. |
|
|
67
|
+
| `assets/src/fastify-cors.ts` | `src/passflow-fastify-cors.ts` | Fastify, called from a browser on another origin, and `@fastify/cors` is already a dependency. See step 3. |
|
|
68
|
+
| `assets/src/hono-cors.ts` | `src/passflow-hono-cors.ts` | Hono, called from a browser on another origin (`hono/cors` ships with `hono`). |
|
|
69
|
+
| `assets/src/fetch-handler.ts` | `src/passflow-fetch.ts` | A handler in the same app that receives a web `Request` and returns a `Response` (a Hono `c.req.raw` handler, for example). |
|
|
70
|
+
|
|
71
|
+
- The project has no `src/` folder (the entry is `server.js` or `index.ts` at the root, for example): put every target in `lib/` instead, as `lib/passflow.ts`, `lib/passflow-express.ts` and so on, and import them from the entry as `./lib/passflow-express.js`. Write nowhere else.
|
|
72
|
+
- **`/api/me`.** The framework assets add `GET /api/me`, which `verify.sh run` calls. Look at the app's routes first (Grep for `/api/me`). If the app already has `/api/me`, do not add a second one: protect its handler in place (step 4), or change only the path string in the copied asset to an unused one such as `/api/passflow/me`, and pass that path to `verify.sh run --api`.
|
|
73
|
+
- `assets/examples/` holds the patterns for the app's own routes (step 4) and for debugging a rejection. They import from the target names above. Copy single lines from them; do not copy the files as a whole.
|
|
74
|
+
- Imports use `./passflow.js`, which fits a project built with `tsc` and one run with `tsx`. Change it to match the project's own relative imports: drop the extension when they have none (a bundler); use `./passflow.ts` when Node runs the `.ts` files as they are (`node --experimental-strip-types`, or `node` on a `.ts` entry), because Node then loads the file by its real name.
|
|
75
|
+
- JavaScript project (no `tsconfig.json`): save the files as `.js` and remove everything that is TypeScript only: type annotations (`: Promise<void>`, `: RequestHandler`), type arguments (`new Hono<PassflowEnv>()` becomes `new Hono()`), `import type ...` lines, `type` names inside an import list, and `export type ...` declarations.
|
|
76
|
+
- CommonJS project (no `"type": "module"` and the code uses `require`): after that, turn each `import { a } from 'x'` into `const { a } = require('x')`, a default import `import cors from 'cors'` into `const cors = require('cors')`, and each `export` into a `module.exports` entry. The package ships CommonJS too. The Fastify assets use `await` inside async functions only, so they need no other change; a CommonJS file has no top-level `await`, so wire them as step 3 shows for CommonJS.
|
|
77
|
+
|
|
78
|
+
3. **Wire it into the app.** Mount CORS first (when there is CORS), then the routes.
|
|
79
|
+
|
|
80
|
+
Express (`app` is the existing `express()` instance):
|
|
81
|
+
```ts
|
|
82
|
+
import { passflowRoutes } from './passflow-express.js';
|
|
83
|
+
|
|
84
|
+
app.use(passflowRoutes);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Fastify (`app` is the existing `Fastify()` instance):
|
|
88
|
+
```ts
|
|
89
|
+
import { passflowPlugin } from './passflow-fastify.js';
|
|
90
|
+
|
|
91
|
+
await passflowPlugin(app);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
In CommonJS, where top-level `await` is a syntax error, put the calls in the async function that starts the server, before `app.listen`:
|
|
95
|
+
```js
|
|
96
|
+
const { passflowPlugin } = require('./passflow-fastify.js');
|
|
97
|
+
|
|
98
|
+
async function start() {
|
|
99
|
+
await passflowPlugin(app);
|
|
100
|
+
await app.listen({ port: Number(process.env.PORT ?? 3000) });
|
|
101
|
+
}
|
|
102
|
+
start();
|
|
103
|
+
```
|
|
104
|
+
If the app already has such a function, add the `await` line to it instead of adding a second one. The same goes for `await passflowCors(app, ...)` below.
|
|
105
|
+
|
|
106
|
+
Hono (`app` is the existing `new Hono()`; give it the `PassflowEnv` type so `getAuth(c)` is typed):
|
|
107
|
+
```ts
|
|
108
|
+
import { type PassflowEnv, passflowRoutes } from './passflow-hono.js';
|
|
109
|
+
|
|
110
|
+
const app = new Hono<PassflowEnv>();
|
|
111
|
+
app.route('/', passflowRoutes);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**CORS.** Only when a browser page on another origin calls the API. Pick the first case that fits:
|
|
115
|
+
- The app already mounts a CORS middleware (`cors(...)`, `app.register(cors, ...)`, `cors(...)` from `hono/cors`): keep it where it is, before the routes, and wrap its options in `exposeChallengeHeader` from `@passflow/node/express`, `/fastify` or `/hono`: `cors(exposeChallengeHeader({ origin: allowedOrigins }))`. Do not add a second one.
|
|
116
|
+
- The package is a dependency but not mounted yet: copy the CORS asset and mount it before every route with the origins the API already allows (from its config or env), never a new hard-coded one:
|
|
117
|
+
```ts
|
|
118
|
+
app.use(passflowCors({ origin: allowedOrigins })); // Express, from ./passflow-express-cors.js
|
|
119
|
+
await passflowCors(app, { origin: allowedOrigins }); // Fastify, from ./passflow-fastify-cors.js
|
|
120
|
+
app.use(passflowCors({ origin: allowedOrigins })); // Hono, from ./passflow-hono-cors.js
|
|
121
|
+
```
|
|
122
|
+
- Express without `cors`, or Fastify without `@fastify/cors`: change nothing for CORS. The last step tells the developer what to add.
|
|
123
|
+
|
|
124
|
+
Keep the app's existing `credentials` setting. Never turn `credentials` on yourself: a bearer token needs none, and credentials together with an origin that reflects or allows every site lets any site read the API's answers.
|
|
125
|
+
|
|
126
|
+
Fetch-style handler: return `handleMe(request)` for `GET /api/me`, and copy its first four lines into each protected handler. This is a manual path inside an app that already uses Express, Fastify or Hono: a server with none of the three is not detected, and `verify.sh` refuses it.
|
|
127
|
+
|
|
128
|
+
4. **Protect the existing routes.** Every route that returns or changes private data gets one of these. Public routes get none. The exact lines are in `assets/examples/express-routes.ts`, `fastify-routes.ts` and `hono-routes.ts`.
|
|
129
|
+
|
|
130
|
+
| Place | Call | Signed out or invalid token |
|
|
131
|
+
|---|---|---|
|
|
132
|
+
| Express route | `app.get('/path', passflow.authenticate, handler)` | 401, `WWW-Authenticate: Bearer` |
|
|
133
|
+
| Express, needs a second factor | `passflow.requireACR(AAL2)`, with `import { AAL2 } from '@passflow/node'` | 401 with `acr_values="2"` |
|
|
134
|
+
| Express, needs a recent sign-in | `passflow.requireFresh(300)` | 401 with `max_age=300` |
|
|
135
|
+
| Fastify route | `{ preHandler: app.passflow.authenticate }` (or `app.passflow.requireACR(AAL2)`, `app.passflow.requireFresh(300)`) | same |
|
|
136
|
+
| Hono route | `passflow.authenticate` (or `passflow.requireACR(AAL2)`, `passflow.requireFresh(300)`) | same |
|
|
137
|
+
| Fetch-style handler | `const result = await authenticateRequest(verifier, request); if ('response' in result) return result.response;` | same |
|
|
138
|
+
|
|
139
|
+
`passflow` is exported by `passflow-express.ts` and `passflow-hono.ts`. Read the claims in a handler with `getAuth(req)` (Express), `getAuth(request)` (Fastify) or `getAuth(c)` (Hono), each imported from the same adapter path. A missing role is a plain 403 with no `WWW-Authenticate`. In Fastify, `passflowPlugin` is registered once per instance; a scope without it uses `createFastifyHooks(verifier)` from `@passflow/node/fastify`, as `assets/examples/fastify-routes.ts` shows.
|
|
140
|
+
|
|
141
|
+
5. **Tell the developer.** End with a short message that lists what they still have to do, each as a command or a line to add. Include only the items that apply:
|
|
142
|
+
- **Env values.** If `.env` is missing or a value is empty: run `npx passflow integrate`, or create `.env` with `PASSFLOW_ISSUER=` and `PASSFLOW_AUDIENCE=` from the Passflow portal (the issuer URL, and the client ID of the app whose tokens the API accepts). Names only in your message, never values.
|
|
143
|
+
- **Loading `.env`.** If the app does not load it already (dotenv, `--env-file`, its framework): start Node with `--env-file=.env`, for example `node --env-file=.env dist/server.js` in the start script.
|
|
144
|
+
- **CORS package.** If a browser on another origin calls the API and the CORS package is missing: `npm install cors` (plus `npm install -D @types/cors` in a TypeScript project) for Express, or `npm install @fastify/cors` for Fastify, written for the project's package manager. Then copy the CORS asset and add the one line from step 3, before the routes.
|
|
145
|
+
- **Check.** `bash <skill>/scripts/verify.sh run --port <the API's port>`, with `--origin <a browser origin it allows>` when there is CORS, and `--api <path>` when the route is not `/api/me`.
|
|
146
|
+
- In a Passflow GitHub App run, say also which of `run_typecheck()` and `run_build()` passed, and name the route path in your final summary when it is not `/api/me`. The summary goes into the draft PR's description, which tells the developer to run `verify.sh run --api <path>` before merging.
|
|
147
|
+
|
|
148
|
+
## Guardrails
|
|
149
|
+
|
|
150
|
+
Read `references/guardrails.md` before you edit auth code. In short:
|
|
151
|
+
|
|
152
|
+
- Verify with `createVerifier` through the adapter or `authenticateRequest`. Never decode a token by hand (`jwt-decode`, `jsonwebtoken`, `atob`, base64 `Buffer`).
|
|
153
|
+
- Check the token on every protected route, on the server.
|
|
154
|
+
- Return the library's 401 or 503 unchanged; no `next(err)`, no rewritten body.
|
|
155
|
+
- When a browser calls the API, CORS goes first and exposes `WWW-Authenticate` through `exposeChallengeHeader`. Never turn on `credentials`.
|
|
156
|
+
- Issuer and audience come from `PASSFLOW_ISSUER` and `PASSFLOW_AUDIENCE`, never hard-coded.
|
|
157
|
+
- Copy Passflow calls from `assets/`, never from memory.
|
|
158
|
+
- Never print or log a token, an `Authorization` header or an env value.
|
|
159
|
+
- Add no dependency other than `@passflow/node`, edit no `package.json` script, and create or edit no `.env*` file.
|
|
160
|
+
|
|
161
|
+
## Verification
|
|
162
|
+
|
|
163
|
+
On the developer's machine, run these from the project root; `<skill>` is this skill's directory:
|
|
164
|
+
|
|
165
|
+
```sh
|
|
166
|
+
bash <skill>/scripts/verify.sh static
|
|
167
|
+
bash <skill>/scripts/verify.sh run --port <the API's port> [--origin <a browser origin it allows>]
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
In a Passflow GitHub App run, call `run_typecheck()`, then `run_build()` when `package.json` has a `build` script. If one returns a non-zero exit code, read its tail and fix the copied or wired code, then call it again. The fix never touches a `package.json` script, a dependency or a config file. `run_typecheck()` runs the same check as `verify.sh static`: the `typecheck` script, else `tsc -b` for a `tsconfig.json` with `references`, else `tsc -p tsconfig.json`. The runner then runs `verify.sh static` itself, and its result is part of the run. It never runs `verify.sh run`; the draft PR's description tells the developer to run it on their machine before merging, with `--api <path>` when the route moved.
|
|
171
|
+
|
|
172
|
+
What `verify.sh` checks:
|
|
173
|
+
|
|
174
|
+
- `static` needs no env values. It installs with lifecycle scripts off, type-checks (the `typecheck` script; else `tsc -b --noEmit` for a `tsconfig.json` with project references, `tsc -p tsconfig.json --noEmit` otherwise), builds if there is a `build` script, and scans the source for the guardrail breaks above. It proves the copied calls compile against the installed package.
|
|
175
|
+
- `run` needs `.env` with both values and a `start` (or `dev`) script that reads `PORT`. It refuses when something already listens on the port, so stop a running copy of the API first or pass another `--port`. With a `start` script and a `build` script it builds first and fails on a build error, so the run tests the current code. It starts the API, checks that the process it started is the one answering on the port (with `lsof`; skipped without it), calls `/api/me` (or the path given with `--api`, step 2) with no token and expects 401 `Bearer` with `Cache-Control: no-store`, then with an invalid token and expects 401 `invalid_token`. With `--origin`, it checks that the 401 allows that origin and exposes `WWW-Authenticate`. It fails if the API it started exits, and it stops that API at the end.
|
|
176
|
+
|
|
177
|
+
Each check prints `PASS`, `FAIL` or `SKIP` (a check that does not apply, such as CORS without `--origin` or a build without a `build` script), then `RESULT pass` or `RESULT fail`. Exit 0 is a pass, 1 a failed check, 2 a refusal (unsupported package manager, missing tool or env file, busy port).
|
|
178
|
+
|
|
179
|
+
## Troubleshooting
|
|
180
|
+
|
|
181
|
+
- **`TypeError: [passflow] issuer must be a non-empty string` at startup.** `.env` was not loaded, or `PASSFLOW_ISSUER` is empty. See step 5.
|
|
182
|
+
- **Type error on `createVerifier`: `string | undefined` is not assignable to `string`.** The installed `@passflow/node` is older than the one the assets need. Run the install line again.
|
|
183
|
+
- **Every token gets 401 `invalid_token`.** `PASSFLOW_ISSUER` differs from the token's `iss` (a trailing slash counts), or `PASSFLOW_AUDIENCE` is not the client ID of the app that issued the token. To see why, use `explainRejection` from `assets/examples/explain-rejection.ts` for a moment: it logs the error's `code` and `reason` only. Remove it afterwards.
|
|
184
|
+
- **Every request gets 503.** The API cannot load the issuer's key set. From the API's machine, `curl -i "$PASSFLOW_ISSUER/.well-known/jwks.json"` must answer 200.
|
|
185
|
+
- **The browser sees a bare 401 and the step-up dialog never opens.** CORS does not expose `WWW-Authenticate`, or it is mounted after the routes. See step 3.
|
|
186
|
+
- **Fastify throws `FST_ERR_DEC_ALREADY_PRESENT`.** `passflowPlugin` was registered twice on one instance. Register it once; use `createFastifyHooks(verifier)` elsewhere, as in `assets/examples/fastify-routes.ts`.
|
|
187
|
+
- **`verify.sh run` refuses: something already listens on the port.** A copy of the API (or another server) is running there. Stop it, or pass a free `--port`.
|
|
188
|
+
- **`npx passflow integrate` did not update this skill.** The CLI replaces only a skill copy it installed itself and that is unchanged, as recorded in `.passflow-skill.json`. A skill folder you changed is not overwritten: delete it to get the new version.
|