Implementing JWT Signing and Verification
Overview
JSON Web Tokens (JWT) defined in RFC 7519 are compact, URL-safe tokens used for authentication and authorization in web applications. This skill covers implementing secure JWT signing with HMAC-SHA256, RSA-PSS, and EdDSA algorithms, along with verification, token expiration, claims validation, and defense against common JWT attacks (algorithm confusion, none algorithm, key injection).
When to Use
- When deploying or configuring implementing jwt signing and verification capabilities in your environment
- When establishing security controls aligned to compliance requirements
- When building or improving security architecture for this domain
- When conducting security assessments that require this implementation
Prerequisites
- Familiarity with cryptography concepts and tools
- Access to a test or lab environment for safe execution
- Python 3.8+ with required dependencies installed
- Appropriate authorization for any testing activities
Objectives
- Implement JWT signing with HS256, RS256, ES256, and EdDSA
- Verify JWT signatures and validate standard claims
- Implement token expiration, not-before, and audience validation
- Defend against algorithm confusion and none algorithm attacks
- Implement JWT key rotation with JWK Sets
- Build a complete authentication middleware
Key Concepts
JWT Algorithms
| Algorithm |
Type |
Key |
Security Level |
| HS256 |
Symmetric (HMAC) |
Shared secret |
128-bit |
| RS256 |
Asymmetric (RSA) |
RSA key pair |
112-bit |
| ES256 |
Asymmetric (ECDSA) |
P-256 key pair |
128-bit |
| EdDSA |
Asymmetric (Ed25519) |
Ed25519 pair |
128-bit |
Common JWT Attacks
- Algorithm confusion: Switching from RS256 to HS256, using public key as HMAC secret
- None algorithm: Setting alg=none to bypass signature verification
- Key injection: Embedding key in JWK header
- Weak secrets: Brute-forcing short HMAC secrets
- Token replay: Reusing valid tokens without expiration
Security Considerations
- Always validate the algorithm header against an allowlist
- Never accept alg=none in production
- Use asymmetric algorithms (RS256, ES256) for distributed systems
- Set short expiration times (15 min for access tokens)
- Implement token refresh mechanism
- Store secrets securely (not in source code)
Validation Criteria
1---2name: implementing-jwt-signing-and-verification3description: "Implement JWT signing and verification. Signals: API attack, interface testing, GraphQL."4zh-signals: "Implementing JWT Signing and Verification 信号:api攻击,接口测试,graphql。"5when-to-use: Implementing JWT Signing and Verification6 Implements secure JWT (RFC 7519) signing and verification using HMAC-SHA256,7 RSA-PSS, ES256, and EdDSA, including token expiration, claims validation, and8 defenses against algorithm-confusion, none-algorithm, and key-injection9 attacks. Use when adding or hardening JWT-based authentication/authorization,10 or when auditing token verification code for common JWT vulnerabilities.11domain: cybersecurity12subdomain: cryptography13tags:14- cryptography15- jwt16- authentication17- token-security18- digital-signatures19version: '1.0'20author: mahipal21license: Apache-2.022nist_csf:23- PR.DS-0124- PR.DS-0225- PR.DS-1026mitre_attack:27- T160028- T157329- T155330metadata:31 source: https://skillmd.com/skills/dangeroussong/implementing-jwt-signing-and-verification32---3334# Implementing JWT Signing and Verification3536## Overview3738JSON Web Tokens (JWT) defined in RFC 7519 are compact, URL-safe tokens used for authentication and authorization in web applications. This skill covers implementing secure JWT signing with HMAC-SHA256, RSA-PSS, and EdDSA algorithms, along with verification, token expiration, claims validation, and defense against common JWT attacks (algorithm confusion, none algorithm, key injection).394041## When to Use4243- When deploying or configuring implementing jwt signing and verification capabilities in your environment44- When establishing security controls aligned to compliance requirements45- When building or improving security architecture for this domain46- When conducting security assessments that require this implementation4748## Prerequisites4950- Familiarity with cryptography concepts and tools51- Access to a test or lab environment for safe execution52- Python 3.8+ with required dependencies installed53- Appropriate authorization for any testing activities5455## Objectives5657- Implement JWT signing with HS256, RS256, ES256, and EdDSA58- Verify JWT signatures and validate standard claims59- Implement token expiration, not-before, and audience validation60- Defend against algorithm confusion and none algorithm attacks61- Implement JWT key rotation with JWK Sets62- Build a complete authentication middleware6364## Key Concepts6566### JWT Algorithms6768| Algorithm | Type | Key | Security Level |69|-----------|------|-----|---------------|70| HS256 | Symmetric (HMAC) | Shared secret | 128-bit |71| RS256 | Asymmetric (RSA) | RSA key pair | 112-bit |72| ES256 | Asymmetric (ECDSA) | P-256 key pair | 128-bit |73| EdDSA | Asymmetric (Ed25519) | Ed25519 pair | 128-bit |7475### Common JWT Attacks7677- **Algorithm confusion**: Switching from RS256 to HS256, using public key as HMAC secret78- **None algorithm**: Setting alg=none to bypass signature verification79- **Key injection**: Embedding key in JWK header80- **Weak secrets**: Brute-forcing short HMAC secrets81- **Token replay**: Reusing valid tokens without expiration8283## Security Considerations8485- Always validate the algorithm header against an allowlist86- Never accept alg=none in production87- Use asymmetric algorithms (RS256, ES256) for distributed systems88- Set short expiration times (15 min for access tokens)89- Implement token refresh mechanism90- Store secrets securely (not in source code)9192## Validation Criteria9394- [ ] JWT signing produces valid tokens for all algorithms95- [ ] Signature verification rejects tampered tokens96- [ ] Expired tokens are rejected97- [ ] Algorithm confusion attack is prevented98- [ ] None algorithm is rejected99- [ ] JWK key rotation works correctly100- [ ] Claims validation enforces all required claims