jwt-verification-patterns
暂无简介
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-14
- Stars
- ★ 13
- Forks
- 2
03 SKILL.md 正文
/jwt-verification-patterns
When to Use
- Legacy Node.js codebases using jsonwebtoken (not panva/jose)
- Simple HS256 tokens for internal service-to-service auth
- Privilege-decay: token scope narrows on each hop through agent hierarchy
- Token blacklist for immediate invalidation on agent revocation
Do NOT use for
- New greenfield code — prefer [[jwt-jws-jwe-patterns]] (panva/jose, Web Crypto API)
- JWE payload encryption (jsonwebtoken only does signing, not encryption)
Sign with RS256 (asymmetric)
import jwt from 'jsonwebtoken'
import { readFileSync } from 'fs'
const PRIVATE_KEY = readFileSync('/run/secrets/jwt-signing.key')
const PUBLIC_KEY = readFileSync('/run/secrets/jwt-signing.pub')
function issueToken(agentId: string, scopes: string[]): string {
return jwt.sign(
{
sub: agentId,
scopes,
tier: 'fast',
iss: 'yana-ai',
aud: 'swarm-bus',
},
PRIVATE_KEY,
{
algorithm: 'RS256',
expiresIn: '15m',
notBefore: '0s',
}
)
}
Verify with algorithm lockdown
function verifyToken(token: string): jwt.JwtPayload {
// CRITICAL: always specify algorithms — prevents 'none' and algorithm confusion
const payload = jwt.verify(token, PUBLIC_KEY, {
algorithms: ['RS256'], // never omit this
issuer: 'yana-ai',
audience: 'swarm-bus',
complete: false,
}) as jwt.JwtPayload
// Validate required claims
if (!payload.sub) throw new Error('[jwt] missing sub claim')
if (!payload.scopes) throw new Error('[jwt] missing scopes claim')
return payload
}
Privilege-decay on hierarchy hop
const SCOPE_HIERARCHY: Record<string, string[]> = {
'tool:exec': ['sandbox:run'], // exec → can only sandbox
'sandbox:run': ['sandbox:read'], // run → can only read
'sandbox:read': [], // leaf — no further delegation
}
function decayToken(token: string, hop: number): string {
const payload = verifyToken(token)
const currentScopes: string[] = payload.scopes ?? []
// Each hop narrows scopes to children only
const decayedScopes = currentScopes
.flatMap(s => SCOPE_HIERARCHY[s] ?? [])
return issueToken(payload.sub!, decayedScopes)
}
In-memory token blacklist
const BLACKLIST = new Set<string>()
function revokeToken(token: string): void {
const payload = jwt.decode(token) as jwt.JwtPayload
if (payload?.jti) BLACKLIST.add(payload.jti)
}
function isRevoked(token: string): boolean {
const payload = jwt.decode(token) as jwt.JwtPayload
return payload?.jti ? BLACKLIST.has(payload.jti) : false
}
// Add jti to all issued tokens:
jwt.sign({ ..., jti: crypto.randomUUID() }, PRIVATE_KEY, { algorithm: 'RS256' })
Anti-Fake-Pass Checklist
❌ algorithms not specified in verify() → algorithm confusion attack ('none' or RS→HS swap)
❌ jwt.decode() instead of jwt.verify() → no signature validation
❌ HS256 with weak secret (< 256 bits) → offline brute-force
❌ No jti claim → cannot revoke individual tokens
❌ Blacklist in memory → lost on restart; use Redis for persistent revocation
❌ Public key for RS256 passed to HS256 verify → treats PEM as HMAC secret
04 同仓库更多 Skills
openai--sentry--sentry
Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.
ito-basket-compare
Compare Itô prediction-market baskets against a user's knowledge base, portfolio notes, financial context, watchlist, or research thesis. Use for read-only basket comparison and gap analysis without investment advice or live trading.
implementing-network-traffic-baselining
Build network traffic baselines from NetFlow/IPFIX data using Python pandas for statistical analysis, z-score anomaly detection, and hourly/daily traffic pattern profiling
detecting-evasion-techniques-in-endpoint-logs
Detects defense evasion techniques used by adversaries in endpoint logs including log tampering, timestomping, process injection, and security tool disabling. Use when investigating suspicious endpoint behavior, building detection rules for evasion tactics, or conducting threat hunting for stealthy adversary activity. Activates for requests involving evasion detection, defense evasion analysis, log tampering detection, or MITRE ATT&CK TA0005.
implementing-microsegmentation-with-guardicore
Implementing microsegmentation using Akamai Guardicore Segmentation to map application dependencies, create granular network policies, visualize east-west traffic flows, and enforce least-privilege communication between workloads across data centers and cloud.
ramdisk-tmpfs-patterns
暂无简介