lib/JWT

Description:
  • JSON Web Tokens support

Source:

Methods

(async, static) importPrivateKey(key, algorithm) → {Promise}

Source:
Parameters:
Name Type Description
key string | object | CryptoKey

a private key to import, string can be a secret or PEM

algorithm string
Returns:
Type Description
Promise

A Promise that fulfills with the imported key as a CryptoKey object.

(async, static) importPublicKey(key, algorithm) → {Promise}

Source:
Parameters:
Name Type Description
key string | object | CryptoKey

a public key to import, string can be a secret or PEM

algorithm string
Returns:
Type Description
Promise

A Promise that fulfills with the imported key as a CryptoKey object.

(async, static) sign(payload, privateKey, algopt) → {Promise}

Source:
Parameters:
Name Type Attributes Default Description
payload object

data to sign including JWT reserved properties below

Properties
Name Type Attributes Description
exp number <optional>

The token is checked to ensure it has not expired.

nbf number <optional>

The token is checked to ensure it is not being used before a specified time.

iat number <optional>

The token is checked to ensure it is not issued in the future.

iss string <optional>

The token is checked to ensure it has been issued by a trusted issuer.

aud string | Array.<string> <optional>

The token is checked to ensure it is intended for a specific audience.

privateKey string | object | CryptoKey
alg string <optional>
HS256
Returns:
Type Description
Promise

with object result in format { header, token, err }

Example
const { token } = await lib.JWT.sign(payload, secret, "HS512")
const { err } = await lib.JWT.verify(token, secret)

(async, static) verify(token, publicKey, optionsopt) → {Promise}

Source:
Parameters:
Name Type Attributes Description
token string
publicKey string | object | CryptoKey
options object <optional>
Properties
Name Type Attributes Description
iss string | RegExp <optional>

The expected issuer used for verifying the token

nbf boolean <optional>

Verify the nbf claim (default: true)

exp boolean <optional>

Verify the exp claim (default: true)

iat boolean <optional>

Verify the iat claim (default: true)

aud string | Array.<string> | RegExp <optional>

Acceptable audience(s) for the token

Returns:
Type Description
Promise

withh object result in format { header, payload, err }