3.1.4 • Published 3 months ago

@tsndr/cloudflare-worker-jwt v3.1.4

Weekly downloads
105
License
MIT
Repository
github
Last release
3 months ago

Cloudflare Worker JWT

A lightweight JWT implementation with ZERO dependencies for Cloudflare Workers.

Contents

Install

npm i @tsndr/cloudflare-worker-jwt

Examples

Basic Example

async () => {
    import jwt from "@tsndr/cloudflare-worker-jwt"

    // Create a token
    const token = await jwt.sign({
        sub: "1234",
        name: "John Doe",
        email: "john.doe@gmail.com"
    }, "secret")

    // Verify token
    const verifiedToken = await jwt.verify(token, "secret")

    // Abort if token isn't valid
    if (!verifiedToken)
        return

    // Access token payload
    const { payload } = verifiedToken

    // { sub: "1234", name: "John Doe", email: "john.doe@gmail.com" }
}

Restrict Timeframe

async () => {
    import jwt from "@tsndr/cloudflare-worker-jwt"

    // Create a token
    const token = await jwt.sign({
        sub: "1234",
        name: "John Doe",
        email: "john.doe@gmail.com",
        nbf: Math.floor(Date.now() / 1000) + (60 * 60),      // Not before: Now + 1h
        exp: Math.floor(Date.now() / 1000) + (2 * (60 * 60)) // Expires: Now + 2h
    }, "secret")

    // Verify token
    const verifiedToken = await jwt.verify(token, "secret")

    // Abort if token isn't valid
    if (!verifiedToken)
        return

    // Access token payload
    const { payload } = verifiedToken

    // { sub: "1234", name: "John Doe", email: "john.doe@gmail.com", ... }
}

Usage

Sign

sign(payload, secret, [options])

Signs a payload and returns the token.

Arguments

ArgumentTypeStatusDefaultDescription
payloadobjectrequired-The payload object. To use nbf (Not Before) and/or exp (Expiration Time) add nbf and/or exp to the payload.
secretstring, JsonWebKey, CryptoKeyrequired-A string which is used to sign the payload.
optionsstring, objectoptionalHS256Either the algorithm string or an object.
options.algorithmstringoptionalHS256See Available Algorithms
options.headerobjectoptionalundefinedExtend the header of the resulting JWT.

return

Returns token as a string.

Verify

verify(token, secret, [options])

Verifies the integrity of the token.

ArgumentTypeStatusDefaultDescription
tokenstringrequired-The token string generated by sign().
secretstring, JsonWebKey, CryptoKeyrequired-The string which was used to sign the payload.
optionsstring, objectoptionalHS256Either the algorithm string or an object.
options.algorithmstringoptionalHS256See Available Algorithms
options.clockTolerancenumberoptional0Clock tolerance in seconds, to help with slighly out of sync systems.
options.throwErrorbooleanoptionalfalseBy default this we will only throw integration errors, only set this to true if you want verification errors to be thrown as well.

throws

Throws integration errors and if options.throwError is set to true also throws ALG_MISMATCH, NOT_YET_VALID, EXPIRED or INVALID_SIGNATURE.

return

Returns the decoded token or undefined.

{
    header: {
        alg: "HS256",
        typ: "JWT"
    },
    payload: {
        sub: "1234",
        name: "John Doe",
        email: "john.doe@gmail.com"
    }
}

Decode

decode(token)

Just returns the decoded token without verifying verifying it. Please use verify() if you intend to verify it as well.

ArgumentTypeStatusDefaultDescription
tokenstringrequired-The token string generated by sign().

return

Returns an object containing header and payload:

{
    header: {
        alg: "HS256",
        typ: "JWT"
    },
    payload: {
        sub: "1234",
        name: "John Doe",
        email: "john.doe@gmail.com"
    }
}

Available Algorithms

  • ES256, ES384, ES512
  • HS256, HS384, HS512
  • RS256, RS384, RS512
3.1.4

3 months ago

3.1.3

8 months ago

3.0.0

9 months ago

2.5.4

9 months ago

3.1.2

9 months ago

3.1.1

9 months ago

3.1.0

9 months ago

2.5.3

1 year ago

2.5.2

1 year ago

2.5.0

1 year ago

2.5.1

1 year ago

2.4.7

1 year ago

2.4.6

1 year ago

2.4.5

1 year ago

2.4.4

1 year ago

2.4.3

1 year ago

2.4.1

1 year ago

2.4.2

1 year ago

2.4.0

1 year ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.5

2 years ago

2.2.4

2 years ago

2.2.7

2 years ago

2.2.6

2 years ago

2.2.10

2 years ago

2.3.0

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.2.9

2 years ago

2.2.8

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.3.0-0

2 years ago

2.1.4

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.3

3 years ago

2.1.0

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

2.0.0-pre.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago