Keylab is a Node.js/TypeScript library designed to simplify the creation and validation of JSON Web Tokens, eliminating the need for prior knowledge in cryptography. Built with full TypeScript support and comprehensive type exports for enhanced developer experience.
yarn add keylab
pnpm add keylab
npm install keylab
- π Complete JWT Support: Create, sign, verify, and validate JSON Web Tokens
- π― Full TypeScript Support: Comprehensive type definitions for all interfaces
- π οΈ Multiple Algorithms: Support for RS256, ES256, HS256, EdDSA, ES256K, and more
- π Universal Compatibility: Works in Node.js, Cloudflare Workers, and modern browsers
- π Key Management: Generate key pairs, handle JWKs, and PEM formats
- π JWKS Integration: Built-in support for JWKS endpoints and verification
- β‘ Zero Dependencies: Lightweight with minimal external dependencies
- π Cryptographically Secure: Built on industry-standard jose library
Keylab is built with TypeScript and exports comprehensive type definitions for all interfaces and functions. Import types alongside functions for full type safety:
import {
getKeyPair,
signJwtWithPrivateKey,
JwtAlgorithmsEnum,
type IGetKeyPair,
type IKeyPair,
type IJwtTokenClaims
} from "keylab"
// ES256 with TypeScript support
import {
getKeyPair,
signJwtWithPrivateKey,
JwtAlgorithmsEnum as Algs,
type IGetKeyPair,
type IKeyPair
} from "keylab"
const keyPairConfig: IGetKeyPair = {
keyFormat: "pem",
algorithmIdentifier: Algs.ES256,
keySize: 4096
};
const keyPairES256: IKeyPair = await getKeyPair(keyPairConfig);
const payload: Partial<IJwtTokenClaims> = {
aid: "12345",
sub: "sub:12345",
iss: "issuer:12345",
aud: ["aud:12345"],
scp: ["a", "b:c", "d"].join(" ")
};
const signedPayloadEs256: string = await signJwtWithPrivateKey(
payload,
Algs.ES256,
keyPairES256.privateKey
);
- HS256
import {verifyHSTokenWithSecretString, Algs} from "keylab"
const isVerified = await verifyHSTokenWithSecretString(
signedToken,
SECRET_STRING,
Algs.HS256
);
- ES512
import {
checkTokenValidness,
type IcheckTokenValidnessCredentials
} from "keylab"
const signedPayloadEs512: string = "ey.....";
const jwksUri: string = "https://id.authdog.com/oidc/.well-known/jwks.json";
const credentials: IcheckTokenValidnessCredentials = {
jwksUri,
requiredIssuer: "https://id.authdog.com",
requiredAudiences: ["your-app-id"]
};
const isValid: boolean = await checkTokenValidness(
signedPayloadEs512,
credentials
);
Keylab exports all TypeScript interfaces and types for comprehensive type safety:
IDecodedJwt
- Structure of decoded JWT payloadIGetKeyPair
- Parameters for key pair generationIJwkPrivateKey
- JWK private key structureIJwkPublicKey
- JWK public key structureIKeyPair
- Key pair generation result
IcheckTokenValidnessCredentials
- Token validation parametersISignTokenCredentials
- Token signing credentialsIJwtTokenClaims
- JWT claims structureIJwtTokenOpts
- JWT creation optionsICheckJwtFields
- JWT field validation optionsICreateSignedJwtOptions
- Complete JWT creation configuration
IJwksClient
- JWKS client configurationIJwkRecordVisible
- Public JWK record structureIVerifyRSATokenCredentials
- RSA token verification parametersIRSAKeyStore
- RSA key store structureITokenExtractedWithPubKey
- Token extraction result
JwtAlgorithmsEnum
- All supported JWT algorithmsJwtPublicKeyUse
- Key usage types (sig, enc)JwtKeyTypes
- Key type definitions (RSA, EC, OKP, oct)JwtParts
- JWT part identifiers (header, payload, signature)
- 0.1.34: Major Type Export Release
- Comprehensive TypeScript type exports for all interfaces and functions
- Fix dynamic require error for crypto module (bundler compatibility)
- Enhanced developer experience with full type safety
- Add 20+ exported interfaces including JWT, JWKS, and operation types
- Export all enums (JwtAlgorithmsEnum, JwtKeyTypes, etc.)
- Maintain full backward compatibility with existing code
- 0.1.33:
- Upgrade to jose v6 with full compatibility
- Add support for all JOSE standard algorithms (RSA-OAEP, ECDH-ES, X25519, X448)
- Implement Node.js crypto fallbacks for ES256K and EdDSA variants
- Add ESM support to eliminate Vite CJS deprecation warning
- Enhanced JWK handling with proper curve normalization
- 0.1.32: Leverage Adhoc keys
- 0.1.31: Bump jose dependency, fix fetch in Jest, enable Ed25519, Ed448
- 0.1.30: Fix support for adhoc jwks
- 0.1.29: Bug fixes and stability improvements
- 0.1.28: Performance optimizations and dependency updates
- 0.1.27: Minor bug fixes and improvements
- 0.1.26: Remove fetchJwksWithUri, remove node-fetch dependency
- 0.1.25: Bug fixes and error handling improvements
- 0.1.24: TypeScript improvements and build optimizations
- 0.1.23: Security patches and dependency updates
- 0.1.22: Performance improvements and bug fixes
- 0.1.21: Enhanced compatibility and stability fixes
- 0.1.20: Improve Cloudflare Workers compatibility for verify
- 0.1.19: Export
getKeyPair
- 0.1.18: Export
pemToJwk
- 0.1.17: Bug fixes and stability improvements
- 0.1.16: Enhanced error handling and validation
- 0.1.15: Build optimizations and dependency updates
- 0.1.14: Extract alg to verify token with PEM
- 0.1.13: Validate tokens signed with PEM (version 0.1.13 was skipped)
- 0.1.12: Remove
jws
from dependencies, improve compatibility with Cloudflare Workers - 0.1.11: Add support for keyid injection in jwt header (required for Apple OAuth2.0 flow)
- 0.1.10: Bug fixes and improvements
- 0.1.9: Stability and performance improvements
- 0.1.8: Enhanced validation and error handling
- 0.1.7: Build improvements and optimizations
- 0.1.6: Bug fixes and dependency updates
- 0.1.5: Performance enhancements
- 0.1.4: Security improvements and bug fixes
- 0.1.3: Enhanced compatibility and stability
- 0.1.2: Bug fixes and improvements
- 0.1.1: Initial stability improvements
- 0.1.0: Initial release with basic JWT functionality
If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
This project is licensed under the MIT license. See the LICENSE file for more info.