feat: Implement JWT signing and verification utility#1
Open
charliewwdev wants to merge 2 commits intomainfrom
Open
feat: Implement JWT signing and verification utility#1charliewwdev wants to merge 2 commits intomainfrom
charliewwdev wants to merge 2 commits intomainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Create a module that generates signed JWTs (RS256 or HS256) and verifies/decodes them. Include configurable expiry, issuer, and audience claims.
Generated by OpenDev AI
Uses the
joselibrary (ESM-native, zero-dependency) which supports both HS256 and RS256 out of the box and integrates cleanly with Node16 module resolution. Key decisions:HS256Config/RS256Config) give the compiler a discriminated union — TypeScript enforces that RS256 callers supply both keys and HS256 callers supply a secret, catching misconfiguration at compile time.resolveSigningKeyimports viaimportPKCS8,resolveVerificationKeyviaimportSPKI. This makes the public-only verification path explicit and prevents accidental private-key exposure to verification consumers.JwtExpiredError,JwtInvalidError) wrap jose's internal errors so callers caninstanceof-check without depending on jose's error hierarchy directly.expiresInin seconds (default 3600) is the simplest contract for service-to-service use; jose accepts the"Xs"string format internally.decodeJwtwithout verification is intentionally kept as a separate export — it's only for debugging or reading claims from already-trusted internal tokens, not a verification bypass.Dependency to add:
pnpm add josein this workspace (or the monorepo root).josev5 is the current stable release and is used here.