Przejdź do treści
Biblioteka skilli
Bezpieczeństwo · Budowanie i kod

jwt-handler

/jwt-handler

Generuje, waliduje, odświeża i debuguje tokeny JWT oraz obsługuje ich cykl życia: podpisywanie, weryfikację, rotację odświeżania i unieważnianie.

Repozytorium
skills ↗
Licencja
Apache-2.0
Ostatnia zmiana repozytorium
25.10.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
2
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 25.10.2026, bez odwołań do wycofanych modeli i API.

Licencja

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

Co robi i kiedy

Generuje, waliduje, odświeża i debuguje tokeny JWT oraz obsługuje ich cykl życia: podpisywanie, weryfikację, rotację odświeżania i unieważnianie.

Wyzwalacze z opisu

JWT,access token,refresh token,token rotation,token expiration,token validation,bearer token,decode JWT.
Oryginalny opis (EN)

When the user needs to generate, validate, refresh, or debug JSON Web Tokens. Use when the user mentions "JWT," "access token," "refresh token," "token rotation," "token expiration," "token validation," "bearer token," or "decode JWT." Handles secure token lifecycle including signing, verification, refresh rotation, and revocation. For full auth system design, see auth-system-setup.

Jak działa

  1. Overview
  2. Instructions
  3. Examples
  4. Guidelines
Sekcje z nagłówków SKILL.md, w oryginale · 139 linii, 810 słów
Podgląd SKILL.md
---
name: jwt-handler
description: >-
  When the user needs to generate, validate, refresh, or debug JSON Web Tokens.
  Use when the user mentions "JWT," "access token," "refresh token," "token rotation,"
  "token expiration," "token validation," "bearer token," or "decode JWT."
  Handles secure token lifecycle including signing, verification, refresh rotation,
  and revocation. For full auth system design, see auth-system-setup.
license: Apache-2.0
compatibility: "Any language with JWT library support"
metadata:
  author: terminal-skills
  version: "1.1.0"
  category: development
  tags: "jwt, tokens, authentication, security"
---

# JWT Handler

## Overview

Implements secure JWT token lifecycle for web applications — generation, validation, refresh rotation, revocation, and debugging. Produces code that follows current security best practices including short-lived access tokens, one-time refresh rotation with family tracking, and proper key management.

## Instructions

### Token Generation

When creating JWT tokens:

1. **Access token**: Short-lived (15 min), contains user ID and roles, signed with RS256 or ES256
2. **Refresh token**: Longer-lived (7-30 days), opaque or JWT, stored hashed in database
3. **Always use asymmetric signing** (RS256/ES256) for production — allows verification without the private key
4. **Minimal payload**: user ID, roles, issued-at, expiration. No PII, no secrets.

```typescript
// Access token payload — keep it minimal
{
  sub: "user_abc123",
  roles: ["member"],
  iat: 1708185600,
  exp: 1708186500  // 15 minutes
}
```

### Refresh Token Rotation

Implement one-time rotation with reuse detection:

1. Each refresh token belongs to a "family" (first token in chain)
2. On refresh: invalidate old token, issue new access + refresh pair
3. If a revoked token is presented → revoke ALL tokens in that family (theft detected)
4. Store in database: token_hash, family_id, user_id, expires_at, revoked_at

### Token Validation Middleware

```
1. Extract token from Authorization header (Bearer scheme)
2. Verify signature with the public key, passing an explicit allowlist: jwt.verify(token, publicKey, { algorithms: ['RS256'], issuer: 'app', audience: 'api' })
3. Check expiration (exp claim) — done by jwt.verify; clockTolerance: 30 allows 30 s of drift
4. Check issuer and audience (passed in step 2)
5. Attach decoded payload to request context
6. Return 401 with a specific error, mapped from the library's error name:
   - TokenExpiredError → token_expired
   - JsonWebTokenError (malformed, bad signature, wrong issuer or audience) → token_invalid
   - NotBeforeError → token_invalid
   - no Authorization header → token_missing
```

### Key Management

- Generate RSA 2048+ or EC P-256 key pairs
- Store private key in environment variable or secrets manager
- Expose public key via /.well-known/jwks.json endpoint
- Support key rotation: include key ID (kid) in token header (`keyid` option in jsonwebtoken), and look up the public key by `kid` when verifying
- Env vars hold PEM text with line breaks; a `.env` file needs `\n` escapes that the loader must turn back into newlines

## Examples

### Example 1: Node.js Token Service

**Prompt**: "Create a JWT token service with refresh rotation for my Express app"

**Output**:
```typescript
// token.service.ts
import jwt from 'jsonwebtoken';
import crypto from 'crypto';

export class TokenService {
  generateAccessToken(user: User): string {
    return jwt.sign(
      { sub: user.id, roles: user.roles },
      process.env.JWT_PRIVATE_KEY, // PEM; keep the line breaks (see Key Management)
      { algorithm: 'RS256', expiresIn: '15m', issuer: 'app', audience: 'api', keyid: process.env.JWT_KEY_ID }
    );
  }

  async generateRefreshToken(userId: string, familyId?: string): Promise<string> {
    const token = crypto.randomBytes(32).toString('hex');
    const hash = crypto.createHash('sha256').update(token).digest('hex');
    await db.refreshTokens.create({
      tokenHash: hash,
      userId,
      familyId: familyId || crypto.randomUUID(),
      expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
    });
    return token;
  }

  async rotateRefreshToken(oldToken: string) {
    const hash = crypto.createHash('sha256').update(oldToken).digest('hex');
    const stored = await db.refreshTokens.findByHash(hash);

    if (!stored || stored.revokedAt) {
      // Reuse detected — revoke entire family
      if (stored) await db.refreshTokens.revokeFamily(stored.familyId);
      throw new TokenReuseError();
    }

    await db.refreshTokens.revoke(stored.id);
    const newRefresh = await this.generateRefreshToken(stored.userId, stored.familyId);
    const user = await db.users.findById(stored.userId);
    const newAccess = this.generateAccessToken(user);
    return { accessToken: newAccess, refreshToken: newRefresh };
  }
}
```

### Example 2: Debug a JWT Token

**Prompt**: "My API returns token_expired for this JWT, can you decode it? eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleS0yMDI2LTEwIn0.eyJzdWIiOiJ1c3JfN2YzYTljIiwicm9sZXMiOlsibWVtYmVyIl0sImlhdCI6MTc5MTgwMDAwMCwiZXhwIjoxNzkxODAwOTAwfQ.iQvA-PS2MWDxkpNyTq6bY7NynWud4HHjqUR2Npufl4m-PBZko8-mDia64a09TBJt51_HF7J3Pw3gwk8svIHc81Mqr2YfFU-_4vGYSC7OiMiF3qoJhvFuoe3jwBY3G_y92R6qQxyrBClfe3ec9BaCMwiGR9XTyo9Iwa-7Rse20G3abdyFFnRMdNLB-qwxsyT1tcX5CELmd5Tx184QAa-fD3uYzE7LEvvxzotxwg02eX83vdNpaF62MeAcZtuoC8z_gltwu-wHv1BSBNdwlThFTUX2ToZ09-LZUn_cUApVGa4BBTrOBX6OL2Ko_oGbb52_wmIjQ8T204mgu4OgmL6V-g"

**Output**:
```
Decoded JWT (signature not checked here; the public key is needed for that):
  Header:  { alg: "RS256", typ: "JWT", kid: "key-2026-10" }
  Payload: { sub: "usr_7f3a9c", roles: ["member"], iat: 1791800000, exp: 1791800900 }

Cause: exp is 2026-10-12T10:28:20Z, so the token expired before today.
The 15-minute lifetime (iat to exp) matches the access token rule; the client must refresh.
No "iss" or "aud" claims: add them so the API can pin both in jwt.verify.
The kid header lets the API pick the right public key after a key rotation.
```

## Guidelines

- **Never use HS256 with a weak secret** — use RS256/ES256 or HS256 with 256+ bit random key
- **Never store access tokens in localStorage** — use memory variables; refresh tokens in httpOnly cookies
- **Always hash refresh tokens** before database storage — SHA-256 is sufficient
- **Pin the algorithm** — always pass `algorithms` to `jwt.verify`. jsonwebtoken 9.x also rejects an HMAC token checked against a PEM key by default, but do not rely on that default: the explicit allowlist is required
- **Keep the "iat" claim** — jsonwebtoken adds it on sign; use `maxAge` in verify to cap token age for audit-sensitive routes
- **Set clock skew tolerance** with `clockTolerance: 30` (seconds) for verification across services
- **Node only for jsonwebtoken** — it uses Node's crypto module, so edge runtimes need a Web Crypto library such as jose
- **Return specific 401 errors**: "token_expired," "token_invalid," "token_missing" — helps client-side handling
Pliki w paczce (2)
  • SKILL.md 7 KB
  • _scores.json 3 KB

Wersje

W bazie nie ma innych skilli o nazwie jwt-handler.

Migawka bazy z 28.10.2026 · wersja paczki 1c250a16ec7b (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100jwt
    Wspiera pracę z tokenami JWT: podpisy RS256 i HS256, walidacja claimów, wygasanie i rotacja refresh tokenów oraz debugowanie błędów podpisu.
  • 100jwt
    Opisuje użycie JSON Web Tokens do bezpaństwowego uwierzytelniania: budowę tokenu, podpisywanie i walidację.
  • 100jwt-authentication
    Wdraża bezstanowe uwierzytelnianie JWT w Symfony z LexikJWTAuthenticationBundle: endpoint logowania, wydawanie tokenów, refresh tokeny, własne claims i firewall JWT.
  • 100token-lifecycle
    Wymusza rotację, unieważnianie i ochronę przed powtórzeniem tokenów uwierzytelniających w silnikach inferencji.
  • 90backend-auth-patterns
    Wdraża i sprawdza uwierzytelnianie backendu: strukturę i walidację JWT, rotację refresh tokenów, przepływy OAuth2 i OIDC oraz wybór RBAC lub ABAC.
  • 100jwt-token-validator
    Prawdopodobnie pomaga w walidacji tokenów JWT w ramach podstaw bezpieczeństwa aplikacji.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.