Compare commits
50 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
c9da88c4bb
|
|||
|
21ec1b6f2a
|
|||
|
|
97df6e7f81 | ||
|
|
7198501a40 | ||
|
cb6209b1b6
|
|||
|
8f4e5e3199
|
|||
|
594cdd6c05
|
|||
|
f85abf30a8
|
|||
|
095b1d43f3
|
|||
|
0bc128fec1
|
|||
|
f846695242
|
|||
|
695e1c0dfe
|
|||
|
64da4c625f
|
|||
|
e4038ae0a7
|
|||
|
44329617de
|
|||
|
|
e7964b63c2 | ||
|
b733a0650d
|
|||
|
43879de15e
|
|||
|
0c8f476751
|
|||
|
3c5d178fec
|
|||
|
|
f12cafd9d0 | ||
| e0219ff21f | |||
|
|
bc7fa845ed | ||
|
|
5ee043e597 | ||
|
|
9c52217ca2 | ||
|
|
5160cfa416 | ||
| 430fa0eb84 | |||
| 4d4cf316d0 | |||
| e244ff0618 | |||
|
|
13943f64d7 | ||
| 8952a12e4f | |||
| af3ea4cf8d | |||
| d99e361d65 | |||
| 79f030a35a | |||
| 5fd472c33d | |||
| 54ad90f6fd | |||
| 8d9f70ca36 | |||
| e34e1aedab | |||
| d31254de78 | |||
| 8fadfab966 | |||
| 187bd60b57 | |||
| f905176573 | |||
| 39c3fb790d | |||
| 5d8345996a | |||
| eebc0e988a | |||
| bf90a7c855 | |||
| 6e0ce1cf82 | |||
| 4ed24b18c5 | |||
| d12809e15d | |||
| 7a093c2c85 |
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
github: tsndr
|
||||
@@ -1,4 +1,4 @@
|
||||
name: Publish NPM Package
|
||||
name: Publish
|
||||
|
||||
on:
|
||||
release:
|
||||
144
.gitignore
vendored
Normal file
144
.gitignore
vendored
Normal file
@@ -0,0 +1,144 @@
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
# node.js
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
.env.test
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
205
README.md
205
README.md
@@ -2,80 +2,149 @@
|
||||
|
||||
A lightweight JWT implementation with ZERO dependencies for Cloudflare Workers.
|
||||
|
||||
|
||||
## Contents
|
||||
|
||||
- [Usage](#usage)
|
||||
- [Install](#install)
|
||||
|
||||
## Usage
|
||||
|
||||
### Simple Example
|
||||
|
||||
```javascript
|
||||
const jwt = require('@tsndr/cloudflare-worker-jwt')
|
||||
|
||||
// Creating a token
|
||||
const token = jwt.sign({ name: 'John Doe', email: 'john.doe@gmail.com' }, 'secret')
|
||||
|
||||
// Verifing token
|
||||
const isValid = jwt.verify(token, secret)
|
||||
|
||||
// Decoding token
|
||||
const payload = jwt.decode(token)
|
||||
|
||||
```
|
||||
|
||||
### `jwt.sign(payload, secret, [algorithm])`
|
||||
|
||||
Signs a payload and returns the token.
|
||||
|
||||
|
||||
`payload`
|
||||
|
||||
The payload object.
|
||||
|
||||
|
||||
`secret`
|
||||
|
||||
A string which is used to sign the payload.
|
||||
|
||||
|
||||
`algorithm` (optional, default: `HS256`)
|
||||
|
||||
The algorithm used to sign the payload, possible values: `HS256` or `HS512`
|
||||
|
||||
|
||||
### `jwt.verify(token, secret, [algorithm])`
|
||||
|
||||
Verifies the integrity of the token and returns a boolean value.
|
||||
|
||||
|
||||
`token`
|
||||
|
||||
The token string generated by `jwt.sign()`.
|
||||
|
||||
|
||||
`secret`
|
||||
|
||||
A string which is used to sign the payload.
|
||||
|
||||
|
||||
`algorithm` (optional, default: `HS256`)
|
||||
|
||||
The algorithm used to sign the payload, possible values: `HS256` or `HS512`
|
||||
|
||||
### `jwt.decode(token)`
|
||||
|
||||
Returns the payload without verifying the integrity of the token.
|
||||
|
||||
|
||||
`token`
|
||||
|
||||
The token string generated by `jwt.sign()`.
|
||||
- [Examples](#examples)
|
||||
- [Usage](#usage)
|
||||
- [Sign](#sign)
|
||||
- [Verify](#verify)
|
||||
- [Decode](#decode)
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
npm i @tsndr/cloudflare-worker-jwt
|
||||
npm i -D @tsndr/cloudflare-worker-jwt
|
||||
```
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
### Basic Example
|
||||
|
||||
```javascript
|
||||
async () => {
|
||||
const jwt = require('@tsndr/cloudflare-worker-jwt')
|
||||
|
||||
// Creating a token
|
||||
const token = await jwt.sign({ name: 'John Doe', email: 'john.doe@gmail.com' }, 'secret')
|
||||
|
||||
// Verifing token
|
||||
const isValid = await jwt.verify(token, 'secret')
|
||||
|
||||
// Check for validity
|
||||
if (!isValid)
|
||||
return
|
||||
|
||||
// Decoding token
|
||||
const { payload } = jwt.decode(token)
|
||||
}
|
||||
```
|
||||
|
||||
### Restrict Timeframe
|
||||
|
||||
```javascript
|
||||
async () => {
|
||||
const jwt = require('@tsndr/cloudflare-worker-jwt')
|
||||
|
||||
// Creating a token
|
||||
const token = await jwt.sign({
|
||||
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')
|
||||
|
||||
// Verifing token
|
||||
const isValid = await jwt.verify(token, 'secret') // false
|
||||
|
||||
// Check for validity
|
||||
if (!isValid)
|
||||
return
|
||||
|
||||
// Decoding token
|
||||
const { payload } = jwt.decode(token) // { name: 'John Doe', email: 'john.doe@gmail.com', ... }
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
- [Sign](#sign)
|
||||
- [Verify](#verify)
|
||||
- [Decode](#decode)
|
||||
|
||||
<hr>
|
||||
|
||||
### Sign
|
||||
#### `jwt.sign(payload, secret, [options])`
|
||||
|
||||
Signs a payload and returns the token.
|
||||
|
||||
#### Arguments
|
||||
|
||||
Argument | Type | Satus | Default | Description
|
||||
----------- | -------- | -------- | ------- | -----------
|
||||
`payload` | `object` | required | - | The payload object. To use `nbf` (Not Before) and/or `exp` (Expiration Time) add `nbf` and/or `exp` to the payload.
|
||||
`secret` | `string` | required | - | A string which is used to sign the payload.
|
||||
`options` | `object` | optional | `{ algorithm: 'HS256' }` | The options object supporting `algorithm` and `keyid`. (See [Available Algorithms](#available-algorithms))
|
||||
|
||||
#### `return`
|
||||
Returns token as a `string`.
|
||||
|
||||
<hr>
|
||||
|
||||
### Verify
|
||||
#### `jwt.verify(token, secret, [options])`
|
||||
|
||||
Verifies the integrity of the token and returns a boolean value.
|
||||
|
||||
Argument | Type | Satus | Default | Description
|
||||
----------- | -------- | -------- | ------- | -----------
|
||||
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
|
||||
`secret` | `string` | required | - | The string which was used to sign the payload.
|
||||
`options` | `object` | optional | `{ algorithm: 'HS256', throwError: false }` | The options object supporting `algorithm` and `throwError`. (See [Available Algorithms](#available-algorithms))
|
||||
|
||||
#### `throws`
|
||||
If `options.throwError` is `true` and the token is invalid, an error will be thrown.
|
||||
|
||||
#### `return`
|
||||
Returns `true` if signature, `nbf` (if set) and `exp` (if set) are valid, otherwise returns `false`.
|
||||
|
||||
<hr>
|
||||
|
||||
### Decode
|
||||
#### `jwt.decode(token)`
|
||||
|
||||
Returns the payload **without** verifying the integrity of the token. Please use `jwt.verify()` first to keep your application secure!
|
||||
|
||||
Argument | Type | Satus | Default | Description
|
||||
----------- | -------- | -------- | ------- | -----------
|
||||
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
|
||||
|
||||
#### `return`
|
||||
Returns an `object` containing `header` and `payload`:
|
||||
```javascript
|
||||
{
|
||||
header: {
|
||||
alg: 'HS256',
|
||||
typ: 'JWT'
|
||||
},
|
||||
payload: {
|
||||
name: 'John Doe',
|
||||
email: 'john.doe@gmail.com'
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Available Algorithms
|
||||
- ES256
|
||||
- ES384
|
||||
- ES512
|
||||
- HS256
|
||||
- HS384
|
||||
- HS512
|
||||
- RS256
|
||||
- RS384
|
||||
- RS512
|
||||
60
index.d.ts
vendored
Normal file
60
index.d.ts
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* JWT
|
||||
*
|
||||
* @class
|
||||
* @constructor
|
||||
* @public
|
||||
*/
|
||||
declare class JWT {
|
||||
|
||||
/**
|
||||
* Signs a payload and returns the token
|
||||
*
|
||||
* @param {object} payload The payload object. To use `nbf` (Not Before) and/or `exp` (Expiration Time) add `nbf` and/or `exp` to the payload.
|
||||
* @param {string} secret A string which is used to sign the payload.
|
||||
* @param {JWTSignOptions | JWTAlgorithm} options The options object or the algorithm.
|
||||
* @throws {Error} If there's a validation issue.
|
||||
* @returns {Promise<string>} Returns token as a `string`.
|
||||
*/
|
||||
sign(payload: object, secret: string, options?: JWTSignOptions | JWTAlgorithm): Promise<string>
|
||||
|
||||
/**
|
||||
* Verifies the integrity of the token and returns a boolean value.
|
||||
*
|
||||
* @param {string} token The token string generated by `jwt.sign()`.
|
||||
* @param {string} secret The string which was used to sign the payload.
|
||||
* @param {JWTVerifyOptions | JWTAlgorithm} options The options object or the algorithm.
|
||||
* @throws {Error | string} Throws an error `string` if the token is invalid or an `Error-Object` if there's a validation issue.
|
||||
* @returns {Promise<boolean>} Returns `true` if signature, `nbf` (if set) and `exp` (if set) are valid, otherwise returns `false`.
|
||||
*/
|
||||
verify(token: string, secret: string, options?: JWTVerifyOptions | JWTAlgorithm): Promise<boolean>
|
||||
|
||||
/**
|
||||
* Returns the payload **without** verifying the integrity of the token. Please use `jwt.verify()` first to keep your application secure!
|
||||
*
|
||||
* @param {string} token The token string generated by `jwt.sign()`.
|
||||
* @returns {JWTDecodeReturn} Returns an `object` containing `header` and `payload`.
|
||||
*/
|
||||
decode(token: string): JWTDecodeReturn
|
||||
}
|
||||
declare const _exports: JWT
|
||||
|
||||
type JWTAlgorithm = 'ES256' | 'ES384' | 'ES512' | 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512'
|
||||
|
||||
type JWTSignOptions = {
|
||||
algorithm?: JWTAlgorithm,
|
||||
keyid?: string
|
||||
header?: object
|
||||
}
|
||||
|
||||
type JWTVerifyOptions = {
|
||||
algorithm?: JWTAlgorithm
|
||||
throwError?: boolean
|
||||
}
|
||||
|
||||
type JWTDecodeReturn = {
|
||||
header: object,
|
||||
payload: object
|
||||
}
|
||||
|
||||
export = _exports
|
||||
160
index.js
160
index.js
@@ -9,94 +9,120 @@ class Base64URL {
|
||||
|
||||
class JWT {
|
||||
constructor() {
|
||||
if (!crypto || !crypto.subtle)
|
||||
if (typeof crypto === 'undefined' || !crypto.subtle)
|
||||
throw new Error('Crypto not supported!')
|
||||
this.algorithms = {
|
||||
HS256: {
|
||||
name: 'HMAC',
|
||||
hash: {
|
||||
name: 'SHA-256'
|
||||
}
|
||||
},
|
||||
HS512: {
|
||||
name: 'HMAC',
|
||||
hash: {
|
||||
name: 'SHA-512'
|
||||
ES256: { name: 'ECDSA', namedCurve: 'P-256', hash: { name: 'SHA-256' } },
|
||||
ES384: { name: 'ECDSA', namedCurve: 'P-384', hash: { name: 'SHA-384' } },
|
||||
ES512: { name: 'ECDSA', namedCurve: 'P-521', hash: { name: 'SHA-512' } },
|
||||
HS256: { name: 'HMAC', hash: { name: 'SHA-256' } },
|
||||
HS384: { name: 'HMAC', hash: { name: 'SHA-384' } },
|
||||
HS512: { name: 'HMAC', hash: { name: 'SHA-512' } },
|
||||
RS256: { name: 'RSASSA-PKCS1-v1_5', hash: { name: 'SHA-256' } },
|
||||
RS384: { name: 'RSASSA-PKCS1-v1_5', hash: { name: 'SHA-384' } },
|
||||
RS512: { name: 'RSASSA-PKCS1-v1_5', hash: { name: 'SHA-512' } },
|
||||
}
|
||||
}
|
||||
_utf8ToUint8Array(str) {
|
||||
return Base64URL.parse(btoa(unescape(encodeURIComponent(str))))
|
||||
}
|
||||
_str2ab(str) {
|
||||
const buf = new ArrayBuffer(str.length);
|
||||
const bufView = new Uint8Array(buf);
|
||||
for (let i = 0, strLen = str.length; i < strLen; i++) {
|
||||
bufView[i] = str.charCodeAt(i);
|
||||
}
|
||||
utf8ToUint8Array(str) {
|
||||
const chars = []
|
||||
str = btoa(unescape(encodeURIComponent(str)))
|
||||
return Base64URL.parse(str)
|
||||
return buf;
|
||||
}
|
||||
async sign(payload, secret, alg = 'HS256') {
|
||||
if (payload === null || typeof payload !== 'object')
|
||||
throw new Error('payload must be an object')
|
||||
if (typeof secret !== 'string')
|
||||
throw new Error('secret must be a string')
|
||||
if (typeof alg !== 'string')
|
||||
throw new Error('alg must be a string')
|
||||
const importAlgorithm = this.algorithms[alg]
|
||||
if (!importAlgorithm)
|
||||
throw new Error('algorithm not found')
|
||||
const payloadAsJSON = JSON.stringify(payload)
|
||||
const partialToken = `${Base64URL.stringify(this.utf8ToUint8Array(JSON.stringify({ alg, typ: 'JWT' })))}.${Base64URL.stringify(this.utf8ToUint8Array(payloadAsJSON))}`
|
||||
const key = await crypto.subtle.importKey('raw', this.utf8ToUint8Array(secret), importAlgorithm, false, ['sign'])
|
||||
const characters = payloadAsJSON.split('')
|
||||
const it = this.utf8ToUint8Array(payloadAsJSON).entries()
|
||||
let i = 0
|
||||
const result = []
|
||||
let current
|
||||
while (!(current = it.next()).done) {
|
||||
result.push([current.value[1], characters[i]])
|
||||
i++
|
||||
}
|
||||
const signature = await crypto.subtle.sign(importAlgorithm.name, key, this.utf8ToUint8Array(partialToken))
|
||||
return `${partialToken}.${Base64URL.stringify(new Uint8Array(signature))}`
|
||||
}
|
||||
async verify(token, secret, alg = 'HS256') {
|
||||
if (typeof token !== 'string')
|
||||
throw new Error('token must be a string')
|
||||
if (typeof secret !== 'string')
|
||||
throw new Error('secret must be a string')
|
||||
if (typeof alg !== 'string')
|
||||
throw new Error('alg must be a string')
|
||||
const tokenParts = token.split('.')
|
||||
if (tokenParts.length !== 3)
|
||||
throw new Error('token must have 3 parts')
|
||||
const importAlgorithm = this.algorithms[alg]
|
||||
if (!importAlgorithm)
|
||||
throw new Error('algorithm not found')
|
||||
const keyData = this.utf8ToUint8Array(secret)
|
||||
const key = await crypto.subtle.importKey('raw', keyData, importAlgorithm, false, ['sign'])
|
||||
const partialToken = tokenParts.slice(0, 2).join('.')
|
||||
const signaturePart = tokenParts[2]
|
||||
const messageAsUint8Array = this.utf8ToUint8Array(partialToken)
|
||||
const res = await crypto.subtle.sign(importAlgorithm.name, key, messageAsUint8Array)
|
||||
return Base64URL.stringify(new Uint8Array(res)) === signaturePart
|
||||
}
|
||||
decode(token) {
|
||||
let output = token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')
|
||||
switch (output.length % 4) {
|
||||
_decodePayload(raw) {
|
||||
switch (raw.length % 4) {
|
||||
case 0:
|
||||
break
|
||||
case 2:
|
||||
output += '=='
|
||||
raw += '=='
|
||||
break
|
||||
case 3:
|
||||
output += '='
|
||||
raw += '='
|
||||
break
|
||||
default:
|
||||
throw new Error('Illegal base64url string!')
|
||||
}
|
||||
try {
|
||||
return JSON.parse(decodeURIComponent(escape(atob(output))))
|
||||
return JSON.parse(decodeURIComponent(escape(atob(raw))))
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
async sign(payload, secret, options = { algorithm: 'HS256', header: { typ: 'JWT' } }) {
|
||||
if (typeof options === 'string')
|
||||
options = { algorithm: options, header: { typ: 'JWT' } }
|
||||
options = { algorithm: 'HS256', header: { typ: 'JWT' }, ...options }
|
||||
if (payload === null || typeof payload !== 'object')
|
||||
throw new Error('payload must be an object')
|
||||
if (typeof secret !== 'string')
|
||||
throw new Error('secret must be a string')
|
||||
if (typeof options.algorithm !== 'string')
|
||||
throw new Error('options.algorithm must be a string')
|
||||
const importAlgorithm = this.algorithms[options.algorithm]
|
||||
if (!importAlgorithm)
|
||||
throw new Error('algorithm not found')
|
||||
payload.iat = Math.floor(Date.now() / 1000)
|
||||
const payloadAsJSON = JSON.stringify(payload)
|
||||
const partialToken = `${Base64URL.stringify(this._utf8ToUint8Array(JSON.stringify({ ...options.header, alg: options.algorithm, kid: options.keyid })))}.${Base64URL.stringify(this._utf8ToUint8Array(payloadAsJSON))}`
|
||||
let keyFormat = 'raw'
|
||||
let keyData
|
||||
if (secret.startsWith('-----BEGIN')) {
|
||||
keyFormat = 'pkcs8'
|
||||
keyData = this._str2ab(atob(secret.replace(/-----BEGIN.*?-----/g, '').replace(/-----END.*?-----/g, '').replace(/\s/g, '')))
|
||||
} else
|
||||
keyData = this._utf8ToUint8Array(secret)
|
||||
const key = await crypto.subtle.importKey(keyFormat, keyData, importAlgorithm, false, ['sign'])
|
||||
const signature = await crypto.subtle.sign(importAlgorithm, key, this._utf8ToUint8Array(partialToken))
|
||||
return `${partialToken}.${Base64URL.stringify(new Uint8Array(signature))}`
|
||||
}
|
||||
async verify(token, secret, options = { algorithm: 'HS256', throwError: false }) {
|
||||
if (typeof options === 'string')
|
||||
options = { algorithm: options }
|
||||
options = { algorithm: 'HS256', throwError: false, ...options }
|
||||
if (typeof token !== 'string')
|
||||
throw new Error('token must be a string')
|
||||
if (typeof secret !== 'string')
|
||||
throw new Error('secret must be a string')
|
||||
if (typeof options.algorithm !== 'string')
|
||||
throw new Error('options.algorithm must be a string')
|
||||
const tokenParts = token.split('.')
|
||||
if (tokenParts.length !== 3)
|
||||
throw new Error('token must consist of 3 parts')
|
||||
const importAlgorithm = this.algorithms[options.algorithm]
|
||||
if (!importAlgorithm)
|
||||
throw new Error('algorithm not found')
|
||||
const { payload } = this.decode(token)
|
||||
if (payload.nbf && payload.nbf > Math.floor(Date.now() / 1000)) {
|
||||
if (options.throwError)
|
||||
throw 'NOT_YET_VALID'
|
||||
return false
|
||||
}
|
||||
if (payload.exp && payload.exp <= Math.floor(Date.now() / 1000)) {
|
||||
if (options.throwError)
|
||||
throw 'EXPIRED'
|
||||
return false
|
||||
}
|
||||
let keyFormat = 'raw'
|
||||
let keyData
|
||||
if (secret.startsWith('-----BEGIN')) {
|
||||
keyFormat = 'spki'
|
||||
keyData = this._str2ab(atob(secret.replace(/-----BEGIN.*?-----/g, '').replace(/-----END.*?-----/g, '').replace(/\s/g, '')))
|
||||
} else
|
||||
keyData = this._utf8ToUint8Array(secret)
|
||||
const key = await crypto.subtle.importKey(keyFormat, keyData, importAlgorithm, false, ['verify'])
|
||||
return await crypto.subtle.verify(importAlgorithm, key, Base64URL.parse(tokenParts[2]), this._utf8ToUint8Array(`${tokenParts[0]}.${tokenParts[1]}`))
|
||||
}
|
||||
decode(token) {
|
||||
return {
|
||||
header: this._decodePayload(token.split('.')[0].replace(/-/g, '+').replace(/_/g, '/')),
|
||||
payload: this._decodePayload(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new JWT
|
||||
|
||||
168
index.test.js
Normal file
168
index.test.js
Normal file
@@ -0,0 +1,168 @@
|
||||
const { subtle } = require('node:crypto').webcrypto
|
||||
Object.defineProperty(global, 'crypto', {
|
||||
value: { subtle }
|
||||
})
|
||||
|
||||
const JWT = require('./index')
|
||||
const oneDay = (60 * 60 * 24)
|
||||
const now = Date.now() / 1000
|
||||
|
||||
const secrets = {}
|
||||
|
||||
// Keypairs
|
||||
for (const algorithm of Object.keys(JWT.algorithms)) {
|
||||
if (algorithm.startsWith('HS'))
|
||||
secrets[algorithm] = 'secret'
|
||||
else if (algorithm.startsWith('RS')) {
|
||||
secrets[algorithm] = {
|
||||
public: `-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1SU1LfVLPHCozMxH2Mo
|
||||
4lgOEePzNm0tRgeLezV6ffAt0gunVTLw7onLRnrq0/IzW7yWR7QkrmBL7jTKEn5u
|
||||
+qKhbwKfBstIs+bMY2Zkp18gnTxKLxoS2tFczGkPLPgizskuemMghRniWaoLcyeh
|
||||
kd3qqGElvW/VDL5AaWTg0nLVkjRo9z+40RQzuVaE8AkAFmxZzow3x+VJYKdjykkJ
|
||||
0iT9wCS0DRTXu269V264Vf/3jvredZiKRkgwlL9xNAwxXFg0x/XFw005UWVRIkdg
|
||||
cKWTjpBP2dPwVZ4WWC+9aGVd+Gyn1o0CLelf4rEjGoXbAAEgAqeGUxrcIlbjXfbc
|
||||
mwIDAQAB
|
||||
-----END PUBLIC KEY-----`,
|
||||
private: `-----BEGIN PRIVATE KEY-----
|
||||
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7VJTUt9Us8cKj
|
||||
MzEfYyjiWA4R4/M2bS1GB4t7NXp98C3SC6dVMvDuictGeurT8jNbvJZHtCSuYEvu
|
||||
NMoSfm76oqFvAp8Gy0iz5sxjZmSnXyCdPEovGhLa0VzMaQ8s+CLOyS56YyCFGeJZ
|
||||
qgtzJ6GR3eqoY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qMhf0DaegrlBuJllFVVq9qPVRnK
|
||||
xt1Il2HgxOBvbhOT+9in1BzA+YJ99UzC85O0Qz06A+CmtHEy4aZ2kj5hHjECgYEA
|
||||
mNS4+A8Fkss8Js1RieK2LniBxMgmYml3pfVLKGnzmng7H2+cwPLhPIzIuwytXywh
|
||||
2bzbsYEfYx3EoEVgMEpPhoarQnYPukrJO4gwE2o5Te6T5mJSZGlQJQj9q4ZB2Dfz
|
||||
et6INsK0oG8XVGXSpQvQh3RUYekCZQkBBFcpqWpbIEsCgYAnM3DQf3FJoSnXaMhr
|
||||
VBIovic5l0xFkEHskAjFTevO86Fsz1C2aSeRKSqGFoOQ0tmJzBEs1R6KqnHInicD
|
||||
TQrKhArgLXX4v3CddjfTRJkFWDbE/CkvKZNOrcf1nhaGCPspRJj2KUkj1Fhl9Cnc
|
||||
dn/RsYEONbwQSjIfMPkvxF+8HQ==
|
||||
-----END PRIVATE KEY-----`
|
||||
}
|
||||
} else if (algorithm.startsWith('ES')) {
|
||||
if (algorithm === 'ES256') {
|
||||
secrets[algorithm] = {
|
||||
public: `-----BEGIN PUBLIC KEY-----
|
||||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEVs/o5+uQbTjL3chynL4wXgUg2R9
|
||||
q9UU8I5mEovUf86QZ7kOBIjJwqnzD1omageEHWwHdBO6B+dFabmdT9POxg==
|
||||
-----END PUBLIC KEY-----`,
|
||||
private: `-----BEGIN PRIVATE KEY-----
|
||||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgevZzL1gdAFr88hb2
|
||||
OF/2NxApJCzGCEDdfSp6VQO30hyhRANCAAQRWz+jn65BtOMvdyHKcvjBeBSDZH2r
|
||||
1RTwjmYSi9R/zpBnuQ4EiMnCqfMPWiZqB4QdbAd0E7oH50VpuZ1P087G
|
||||
-----END PRIVATE KEY-----`
|
||||
}
|
||||
} else if (algorithm === 'ES384') {
|
||||
secrets[algorithm] = {
|
||||
public: `-----BEGIN PUBLIC KEY-----
|
||||
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEC1uWSXj2czCDwMTLWV5BFmwxdM6PX9p+
|
||||
Pk9Yf9rIf374m5XP1U8q79dBhLSIuaojsvOT39UUcPJROSD1FqYLued0rXiooIii
|
||||
1D3jaW6pmGVJFhodzC31cy5sfOYotrzF
|
||||
-----END PUBLIC KEY-----`,
|
||||
private: `-----BEGIN PRIVATE KEY-----
|
||||
MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDCAHpFQ62QnGCEvYh/p
|
||||
E9QmR1C9aLcDItRbslbmhen/h1tt8AyMhskeenT+rAyyPhGhZANiAAQLW5ZJePZz
|
||||
MIPAxMtZXkEWbDF0zo9f2n4+T1h/2sh/fviblc/VTyrv10GEtIi5qiOy85Pf1RRw
|
||||
8lE5IPUWpgu553SteKigiKLUPeNpbqmYZUkWGh3MLfVzLmx85ii2vMU=
|
||||
-----END PRIVATE KEY-----`
|
||||
}
|
||||
} else if (algorithm === 'ES512') {
|
||||
secrets[algorithm] = {
|
||||
public: `-----BEGIN PUBLIC KEY-----
|
||||
MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBgc4HZz+/fBbC7lmEww0AO3NK9wVZ
|
||||
PDZ0VEnsaUFLEYpTzb90nITtJUcPUbvOsdZIZ1Q8fnbquAYgxXL5UgHMoywAib47
|
||||
6MkyyYgPk0BXZq3mq4zImTRNuaU9slj9TVJ3ScT3L1bXwVuPJDzpr5GOFpaj+WwM
|
||||
Al8G7CqwoJOsW7Kddns=
|
||||
-----END PUBLIC KEY-----`,
|
||||
private: `-----BEGIN PRIVATE KEY-----
|
||||
MIHuAgEAMBAGByqGSM49AgEGBSuBBAAjBIHWMIHTAgEBBEIBiyAa7aRHFDCh2qga
|
||||
9sTUGINE5jHAFnmM8xWeT/uni5I4tNqhV5Xx0pDrmCV9mbroFtfEa0XVfKuMAxxf
|
||||
Z6LM/yKhgYkDgYYABAGBzgdnP798FsLuWYTDDQA7c0r3BVk8NnRUSexpQUsRilPN
|
||||
v3SchO0lRw9Ru86x1khnVDx+duq4BiDFcvlSAcyjLACJvjvoyTLJiA+TQFdmrear
|
||||
jMiZNE25pT2yWP1NUndJxPcvVtfBW48kPOmvkY4WlqP5bAwCXwbsKrCgk6xbsp12
|
||||
ew==
|
||||
-----END PRIVATE KEY-----`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Payload
|
||||
const testPayload = {
|
||||
sub: "1234567890",
|
||||
name: "John Doe"
|
||||
}
|
||||
|
||||
// Self test
|
||||
test.each(Object.entries(secrets))(`Self test: %s`, async (algorithm, key) => {
|
||||
let privateKey = key
|
||||
let publicKey = key
|
||||
if (typeof key === 'object') {
|
||||
privateKey = key.private
|
||||
publicKey = key.public
|
||||
}
|
||||
|
||||
const token = await JWT.sign(testPayload, privateKey, { algorithm })
|
||||
expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/)
|
||||
|
||||
const verified = await JWT.verify(token, publicKey, { algorithm })
|
||||
expect(verified).toBeTruthy()
|
||||
|
||||
const { payload } = JWT.decode(token)
|
||||
expect({
|
||||
sub: payload.sub,
|
||||
name: payload.name
|
||||
}).toMatchObject({
|
||||
sub: testPayload.sub,
|
||||
name: testPayload.name
|
||||
})
|
||||
})
|
||||
|
||||
// External token test
|
||||
const externalTokens = {
|
||||
ES256: 'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.Ri9u3ZJpyoHf1_i3KpVE5gMggyU3VMYPeEVktAsG1kGLOxFNJBXydQls3WFBaXXH2-sN74IMe-nDcM7NoJ6GMQ',
|
||||
ES384: 'eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwib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nHHU_0bcGXMd5LA7vF87yQgXNaioPHP4lU4O3JJYuZ54fJdv3HT58xk-MFEDuWro_5fvNIp2VM-PlkZvYWrhQkJ-c-seoSa3ANq_PciC3bGfzYHEdjAE71GrAMI4FlcAGsq3ChkOnCTFqjWDmVwaRYCgMsFQ-U5cjvFhndFMizrkRljTF4v5oFdWytV_J-UafPtNdQXcGND1M74DqObnTHhZHg8aDfNzZcvnIeKcDVGUlUEL5ia1kPMrVhCtOAOJmEU8ivCdWWzt-jMQBf7cZeoCzDKHG72ysTTCfRoBVc1_SrQTHcHDiiBeW9nCazMLkltyP5NeawR_RNlg'
|
||||
}
|
||||
|
||||
test.each(Object.entries(externalTokens))('Verify external tokens: %s', async (algorithm, token) => {
|
||||
const key = secrets[algorithm]
|
||||
let privateKey = key
|
||||
let publicKey = key
|
||||
if (typeof key === 'object') {
|
||||
privateKey = key.private
|
||||
publicKey = key.public
|
||||
}
|
||||
|
||||
const verified = await JWT.verify(token, publicKey, { algorithm })
|
||||
expect(verified).toBeTruthy()
|
||||
|
||||
const { payload } = JWT.decode(token)
|
||||
expect({
|
||||
sub: payload.sub,
|
||||
name: payload.name
|
||||
}).toMatchObject({
|
||||
sub: testPayload.sub,
|
||||
name: testPayload.name
|
||||
})
|
||||
})
|
||||
8505
package-lock.json
generated
Normal file
8505
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "@tsndr/cloudflare-worker-jwt",
|
||||
"version": "1.0.5",
|
||||
"version": "1.4.4",
|
||||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"test": "jest"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -22,5 +22,8 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/tsndr/cloudflare-worker-jwt/issues"
|
||||
},
|
||||
"homepage": "https://github.com/tsndr/cloudflare-worker-jwt#readme"
|
||||
"homepage": "https://github.com/tsndr/cloudflare-worker-jwt#readme",
|
||||
"devDependencies": {
|
||||
"jest": "^28.1.0"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user