Modules@robinpath/jwt
jwt

@robinpath/jwt

0.1.1Node.jsPublic

JWT (JSON Web Token) creation, signing, verification, and decoding using HMAC (HS256, HS384, HS512)

JWT

JWT (JSON Web Token) creation, signing, verification, and decoding using HMAC (HS256, HS384, HS512)

Package: @robinpath/jwt | Category: Web | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the jwt module when you need to:

  • Create a signed JWT token from a payload object using HMAC (HS256, HS384, or HS512) -- Use jwt.sign to perform this operation
  • Verify a JWT token signature and expiration, returning the decoded payload -- Use jwt.verify to perform this operation
  • Decode a JWT token WITHOUT verifying its signature (unsafe — use for inspection only) -- Use jwt.decode to perform this operation
  • Extract and decode the header from a JWT token (no verification) -- Use jwt.getHeader to perform this operation
  • Extract and decode the payload from a JWT token without verification -- Use jwt.getPayload to perform this operation

Quick Reference

FunctionDescriptionReturns
signCreate a signed JWT token from a payload object using HMAC (HS256, HS384, or HS512)The signed JWT token string (header.payload.signature)
verifyVerify a JWT token signature and expiration, returning the decoded payloadThe decoded payload object if the token is valid
decodeDecode a JWT token WITHOUT verifying its signature (unsafe — use for inspection only)An object containing { header, payload, signature }
getHeaderExtract and decode the header from a JWT token (no verification)The decoded header object (e.g. { alg: "HS256", typ: "JWT" })
getPayloadExtract and decode the payload from a JWT token without verificationThe decoded payload object with all claims
isExpiredCheck whether a JWT token has expired based on its exp claimTrue if the token is expired, false if it is still valid or has no exp claim
getExpirationGet the expiration timestamp (exp claim) from a JWT tokenThe exp timestamp as a number (Unix epoch seconds), or null if not set

Functions

sign

Create a signed JWT token from a payload object using HMAC (HS256, HS384, or HS512)

Module: jwt | Returns: string -- The signed JWT token string (header.payload.signature)

jwt.sign {"sub":"1234","name":"Alice"} "my-secret" {"expiresIn":3600}
ParameterTypeRequiredDescription
payloadobjectYesThe claims object to encode in the JWT
secretstringYesThe HMAC secret key used to sign the token
optionsobjectNoOptional settings: { algorithm: "HS256"

verify

Verify a JWT token signature and expiration, returning the decoded payload

Module: jwt | Returns: object -- The decoded payload object if the token is valid

jwt.verify "eyJhbGciOiJIUzI1NiJ9..." "my-secret"
ParameterTypeRequiredDescription
tokenstringYesThe JWT token string to verify
secretstringYesThe HMAC secret key used to verify the token signature
optionsobjectNoOptional settings: { algorithms: ["HS256","HS384","HS512"] }

decode

Decode a JWT token WITHOUT verifying its signature (unsafe — use for inspection only)

Module: jwt | Returns: object -- An object containing { header, payload, signature }

jwt.decode "eyJhbGciOiJIUzI1NiJ9..."
ParameterTypeRequiredDescription
tokenstringYesThe JWT token string to decode

getHeader

Extract and decode the header from a JWT token (no verification)

Module: jwt | Returns: object -- The decoded header object (e.g. { alg: "HS256", typ: "JWT" })

jwt.getHeader "eyJhbGciOiJIUzI1NiJ9..."
ParameterTypeRequiredDescription
tokenstringYesThe JWT token string

getPayload

Extract and decode the payload from a JWT token without verification

Module: jwt | Returns: object -- The decoded payload object with all claims

jwt.getPayload "eyJhbGciOiJIUzI1NiJ9..."
ParameterTypeRequiredDescription
tokenstringYesThe JWT token string

isExpired

Check whether a JWT token has expired based on its exp claim

Module: jwt | Returns: boolean -- True if the token is expired, false if it is still valid or has no exp claim

jwt.isExpired "eyJhbGciOiJIUzI1NiJ9..."
ParameterTypeRequiredDescription
tokenstringYesThe JWT token string

getExpiration

Get the expiration timestamp (exp claim) from a JWT token

Module: jwt | Returns: number -- The exp timestamp as a number (Unix epoch seconds), or null if not set

jwt.getExpiration "eyJhbGciOiJIUzI1NiJ9..."
ParameterTypeRequiredDescription
tokenstringYesThe JWT token string

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Unsupported algorithm: ${algorithm}Check the error message for details
Invalid JWT: token must have three parts separated by dotsCheck the error message for details
jwt.sign: first argument must be a payload objectCheck the error message for details
jwt.sign: second argument must be a non-empty secret stringCheck the error message for details
jwt.verify: first argument must be a JWT token stringCheck the error message for details
jwt.verify: second argument must be a non-empty secret stringCheck the error message for details
jwt.verify: invalid JWT — token must have three partsCheck the error message for details
jwt.verify: invalid signatureCheck the error message for details
@desc "Sign and validate result"
do
  set $result as jwt.sign {"sub":"1234","name":"Alice"} "my-secret" {"expiresIn":3600}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate Header

Retrieve all items and loop through them.

@desc "Get header and iterate results"
do
  set $result as jwt.getHeader "eyJhbGciOiJIUzI1NiJ9..."
  each $item in $result
    print $item
  end
enddo

2. Multi-step JWT workflow

Chain multiple jwt operations together.

@desc "Sign, verify, and more"
do
  set $r_sign as jwt.sign {"sub":"1234","name":"Alice"} "my-secret" {"expiresIn":3600}
  set $r_verify as jwt.verify "eyJhbGciOiJIUzI1NiJ9..." "my-secret"
  set $r_decode as jwt.decode "eyJhbGciOiJIUzI1NiJ9..."
  print "All operations complete"
enddo

3. Safe sign with validation

Check results before proceeding.

@desc "Sign and validate result"
do
  set $result as jwt.sign {"sub":"1234","name":"Alice"} "my-secret" {"expiresIn":3600}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/jwt

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.1 KB
Versions1
Weekly Downloads22
Total Downloads22
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web