API auth with JWT-based authentication - issue tokens with jsonwebtoken, validate signatures and claims, and enforce expiry and audience. Use when issuing or validating JWTs for API access. Don't use for API-key auth (see api-auth-keys) or mutual-TLS auth (see api-auth-mtls).
Agentic Workflow: Read -> Reason -> Act (api-auth-jwt)
You are Api Auth JWT (security) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — security context for api-auth-jwt
- Domain: API auth with JWT-based authentication - issue tokens with jsonwebtoken, validate signatures and claims, and enforce expiry and audience. Use when issuing or validating JWTs for API access. Don't use
- jwt-auth: Issue and validate JWT tokens for API access —
npm install jsonwebtoken
- Check
knowledge and prerequisites: node.js, python, jsonwebtoken
2. Reason — think for api-auth-jwt
- For
jwt-auth: Issue and validate JWT tokens for API access — decide which checks to run
- Evaluate trust/policy:
kdesk trust + kdesk doctor patterns for your inputs
3. Act — execute with api-auth-jwt tools
- Tools:
Glob, Grep, Read, Bash (see frontmatter tools/allowed-tools)
- Use
safe_path for any write; record evidence (paths, checksums)
- Fingerprint:
api-auth-jwt:a5081012
API Auth (JWT)
What this skill does
Issue and validate JWTs for API authentication with the jsonwebtoken library. Covers signing, verification with audience/issuer enforcement, decoding for debugging, and expiry handling.
When to use
- Replacing opaque session tokens with JWTs
- Adding stateless auth to an API
- Debugging token expiry issues
Real commands
# Install
npm install jsonwebtoken
# Sign a token (1h, audience=api)
node -e "const jwt=require('jsonwebtoken');\
const t=jwt.sign({sub:'user-1',role:'admin'},process.env.SECRET,\
{expiresIn:'1h',audience:'api',issuer:'auth-api'});console.log(t)"
# Verify (fails on bad signature, wrong audience, or expiry)
node -e "const jwt=require('jsonwebtoken');\
const t=process.argv[1];\
try{jwt.verify(t,process.env.SECRET,{audience:'api'});console.log('valid')}\ncatch(e){console.error(e.message)}" $TOKEN
# Decode without verifying (debugging only)
node -e "const jwt=require('jsonwebtoken');console.log(jwt.decode(process.argv[1]))" $TOKEN
# Shell decode
echo $TOKEN | cut -d. -f2 | base64 -d | jq '.exp, .scope'
# Use the token
curl -s http://localhost:8080/api/users -H "Authorization: Bearer $TOKEN" | jq '.user'
Best practices
- Prefer RS256 with a JWKS; use HS256 only for single-service setups
- Always verify signature AND audience AND issuer
- Keep tokens short-lived (15m-1h); refresh via refresh token
- Never log tokens or put secrets in claims
Testing
BAD=invalid.token.here
curl -s http://localhost:8080/api/users -H "Authorization: Bearer $BAD" -o /dev/null -w '%{http_code}\n'
# Expect 401
Capabilities
jwt-auth
Issue and validate JWT tokens for API access
Parameters:
secret (string): HMAC secret for signing
expiresIn (string): Token lifetime, e.g. 1h, 15m, 7d
audience (string): Expected audience claim
Commands:
npm install jsonwebtoken
node -e "const jwt=require('jsonwebtoken');const t=jwt.sign({sub:'user-1',role:'admin'},process.env.SECRET,{expiresIn:'1h',audience:'api',issuer:'auth-api'});console.log(t)"
node -e "const jwt=require('jsonwebtoken');const t=process.argv[1];try{jwt.verify(t,process.env.SECRET,{audience:'api'});console.log('valid')}catch(e){console.error(e.message)}" $TOKEN
node -e "const jwt=require('jsonwebtoken');console.log(jwt.decode(process.argv[1]))" $TOKEN
curl -s http://localhost:8080/api/users -H "Authorization: Bearer $TOKEN" | jq '.user'
Examples:
- node -e "const jwt=require('jsonwebtoken');console.log(jwt.sign({scope:['read:orders']},process.env.SECRET,{expiresIn:'5m',audience:'api'}))"
- echo $TOKEN | cut -d. -f2 | base64 -d | jq '.exp, .scope'
- curl -s http://localhost:8080/api/users -H 'Authorization: Bearer invalid.token.here' -o /dev/null -w '%{http_code}'
References
1---2name: "api-auth-jwt"3description: "API auth with JWT-based authentication - issue tokens with jsonwebtoken, validate signatures and claims, and enforce expiry and audience. Use when issuing or validating JWTs for API access. Don't use for API-key auth (see api-auth-keys) or mutual-TLS auth (see api-auth-mtls)."4license: "MIT"5compatibility: "Requires node.js, python, jsonwebtoken, passport, keycloak. Needs network access."6metadata:7 source: https://skillmd.com/skills/opknock/api-auth-jwt8 author: Kdesk9 version: 2.0.010 category: security11allowed-tools: "Glob Grep Read Bash(curl:*) Bash(node:*) Bash(npm:*)"12---1314API auth with JWT-based authentication - issue tokens with jsonwebtoken, validate signatures and claims, and enforce expiry and audience. Use when issuing or validating JWTs for API access. Don't use for API-key auth (see api-auth-keys) or mutual-TLS auth (see api-auth-mtls).1516## Agentic Workflow: Read -> Reason -> Act (api-auth-jwt)1718You are **Api Auth JWT** (security) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.1920### 1. Read — security context for `api-auth-jwt`21- Domain: API auth with JWT-based authentication - issue tokens with jsonwebtoken, validate signatures and claims, and enforce expiry and audience. Use when issuing or validating JWTs for API access. Don't use 22- **jwt-auth**: Issue and validate JWT tokens for API access — `npm install jsonwebtoken`23- Check `knowledge` and `prerequisites: node.js, python, jsonwebtoken`2425### 2. Reason — think for `api-auth-jwt`26- For `jwt-auth`: Issue and validate JWT tokens for API access — decide which checks to run27- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs2829### 3. Act — execute with `api-auth-jwt` tools30- Tools: `Glob`, `Grep`, `Read`, `Bash` (see frontmatter `tools`/`allowed-tools`)31- Use `safe_path` for any write; record evidence (paths, checksums)32- Fingerprint: `api-auth-jwt:a5081012`3334# API Auth (JWT)3536## What this skill does37Issue and validate JWTs for API authentication with the jsonwebtoken library. Covers signing, verification with audience/issuer enforcement, decoding for debugging, and expiry handling.3839## When to use40- Replacing opaque session tokens with JWTs41- Adding stateless auth to an API42- Debugging token expiry issues4344## Real commands45```bash46# Install47npm install jsonwebtoken4849# Sign a token (1h, audience=api)50node -e "const jwt=require('jsonwebtoken');\51const t=jwt.sign({sub:'user-1',role:'admin'},process.env.SECRET,\52{expiresIn:'1h',audience:'api',issuer:'auth-api'});console.log(t)"5354# Verify (fails on bad signature, wrong audience, or expiry)55node -e "const jwt=require('jsonwebtoken');\56const t=process.argv[1];\57try{jwt.verify(t,process.env.SECRET,{audience:'api'});console.log('valid')}\ncatch(e){console.error(e.message)}" $TOKEN5859# Decode without verifying (debugging only)60node -e "const jwt=require('jsonwebtoken');console.log(jwt.decode(process.argv[1]))" $TOKEN6162# Shell decode63echo $TOKEN | cut -d. -f2 | base64 -d | jq '.exp, .scope'6465# Use the token66curl -s http://localhost:8080/api/users -H "Authorization: Bearer $TOKEN" | jq '.user'67```6869## Best practices70- Prefer RS256 with a JWKS; use HS256 only for single-service setups71- Always verify signature AND audience AND issuer72- Keep tokens short-lived (15m-1h); refresh via refresh token73- Never log tokens or put secrets in claims7475## Testing76```bash77BAD=invalid.token.here78curl -s http://localhost:8080/api/users -H "Authorization: Bearer $BAD" -o /dev/null -w '%{http_code}\n'79# Expect 40180```8182## Capabilities8384### jwt-auth85Issue and validate JWT tokens for API access8687**Parameters:**88- `secret` (string): HMAC secret for signing89- `expiresIn` (string): Token lifetime, e.g. 1h, 15m, 7d90- `audience` (string): Expected audience claim9192**Commands:**93- `npm install jsonwebtoken`94- `node -e "const jwt=require('jsonwebtoken');const t=jwt.sign({sub:'user-1',role:'admin'},process.env.SECRET,{expiresIn:'1h',audience:'api',issuer:'auth-api'});console.log(t)"`95- `node -e "const jwt=require('jsonwebtoken');const t=process.argv[1];try{jwt.verify(t,process.env.SECRET,{audience:'api'});console.log('valid')}catch(e){console.error(e.message)}" $TOKEN`96- `node -e "const jwt=require('jsonwebtoken');console.log(jwt.decode(process.argv[1]))" $TOKEN`97- `curl -s http://localhost:8080/api/users -H "Authorization: Bearer $TOKEN" | jq '.user'`9899**Examples:**100- node -e "const jwt=require('jsonwebtoken');console.log(jwt.sign({scope:['read:orders']},process.env.SECRET,{expiresIn:'5m',audience:'api'}))"101- echo $TOKEN | cut -d. -f2 | base64 -d | jq '.exp, .scope'102- curl -s http://localhost:8080/api/users -H 'Authorization: Bearer invalid.token.here' -o /dev/null -w '%{http_code}'103104## References105- [jwt.io Introduction](https://jwt.io/introduction)106- [jsonwebtoken npm](https://www.npmjs.com/package/jsonwebtoken)