Compare commits
99 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
91f30929da
|
|||
|
93082884fa
|
|||
|
a9e83968b9
|
|||
|
4c480d5ac7
|
|||
|
adf522c3ab
|
|||
|
7c82dff259
|
|||
|
4ceac0270f
|
|||
|
32e00ac6b9
|
|||
|
|
247da9b396 | ||
|
db0e5b51e0
|
|||
|
6594895273
|
|||
|
61a3a2ed50
|
|||
|
d7a6847206
|
|||
|
5ab19c4dc0
|
|||
|
0308d20c38
|
|||
|
703c0c4131
|
|||
|
6b3e828126
|
|||
|
35dc875f56
|
|||
|
|
11afa8eb87 | ||
|
|
b05345279d
|
||
|
|
55bc15bec4 | ||
|
|
b0d4084a0f | ||
|
|
3fd594bbb5 | ||
|
|
f8a216574a | ||
|
|
1f511549f5 | ||
|
|
4be64469d3 | ||
|
72e64f1316
|
|||
|
e235d835aa
|
|||
|
03c66f4223
|
|||
|
70488a6a48
|
|||
|
760245d1c7
|
|||
|
d40d924176
|
|||
|
c2e4fccf56
|
|||
|
0c0919f78d
|
|||
|
7299ea0614
|
|||
|
7004cd16ed
|
|||
|
5d4c51ad8a
|
|||
|
78a0eeee9c
|
|||
|
565c623005
|
|||
|
6b3192b4b9
|
|||
|
a659abecda
|
|||
|
b16cc23654
|
|||
|
0f347c77e1
|
|||
|
39e2770ab3
|
|||
|
78ef16e356
|
|||
|
c4559460fb
|
|||
|
7027999975
|
|||
|
166b074782
|
|||
|
decbd238e9
|
|||
|
894ac2c9f5
|
|||
|
1394e4e33d
|
|||
|
47028970b4
|
|||
|
6ac43d493d
|
|||
|
cc66bd9c47
|
|||
|
a8f95f1c6d
|
|||
|
|
b2886c0778 | ||
|
76b7fcef27
|
|||
|
3b5d8bcde8
|
|||
|
e22c509852
|
|||
| 7ed375e876 | |||
| de658122b9 | |||
|
|
10dcf2f01b | ||
|
1de6acbfff
|
|||
|
b002db4547
|
|||
|
1bdbd0a5c5
|
|||
|
883ec55b73
|
|||
|
|
c515fb76e6 | ||
|
ec41207832
|
|||
|
e64e89f325
|
|||
|
fb573d928a
|
|||
|
382ad66ea9
|
|||
|
ffc1e87b32
|
|||
|
decfa65391
|
|||
|
|
67f1a8e5ed | ||
|
fe1baf46b4
|
|||
|
81f9eafd86
|
|||
|
747931ba69
|
|||
|
261069d5cb
|
|||
|
7466a881a0
|
|||
|
81dde823d3
|
|||
|
210b733591
|
|||
|
bf11705d11
|
|||
|
99d172179e
|
|||
|
63de8056a5
|
|||
|
b953f4165a
|
|||
|
c9da88c4bb
|
|||
|
21ec1b6f2a
|
|||
|
|
97df6e7f81 | ||
|
|
7198501a40 | ||
|
cb6209b1b6
|
|||
|
8f4e5e3199
|
|||
|
594cdd6c05
|
|||
|
f85abf30a8
|
|||
|
095b1d43f3
|
|||
|
0bc128fec1
|
|||
|
f846695242
|
|||
|
695e1c0dfe
|
|||
|
64da4c625f
|
|||
|
e4038ae0a7
|
10
.editorconfig
Normal file
10
.editorconfig
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = false
|
||||||
|
|
||||||
|
[src/**/*.ts]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
28
.github/workflows/publish.yml
vendored
28
.github/workflows/publish.yml
vendored
@@ -2,29 +2,29 @@ name: Publish
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
release:
|
release:
|
||||||
types: [created]
|
types: [published]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
publish-npm:
|
publish:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: 12
|
node-version: latest
|
||||||
registry-url: https://registry.npmjs.org/
|
registry-url: https://registry.npmjs.org/
|
||||||
- run: npm publish --access public
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
- name: Build
|
||||||
|
run: npm run build
|
||||||
|
- name: Publish to npmjs
|
||||||
|
run: npm publish --tag latest --access public
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
||||||
|
- uses: actions/setup-node@v3
|
||||||
publish-gpr:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- uses: actions/setup-node@v1
|
|
||||||
with:
|
with:
|
||||||
node-version: 12
|
|
||||||
registry-url: https://npm.pkg.github.com/
|
registry-url: https://npm.pkg.github.com/
|
||||||
- run: npm publish --access public
|
- name: Publish to GPR
|
||||||
|
run: npm publish --tag latest --access public
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||||
|
|||||||
21
.github/workflows/test.yml
vendored
Normal file
21
.github/workflows/test.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
name: Test
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
run:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: latest
|
||||||
|
registry-url: https://registry.npmjs.org/
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
- name: Run tests
|
||||||
|
run: npm test
|
||||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -141,4 +141,10 @@ dist
|
|||||||
.yarn/unplugged
|
.yarn/unplugged
|
||||||
.yarn/build-state.yml
|
.yarn/build-state.yml
|
||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
# Custom
|
||||||
|
/index.js
|
||||||
|
/index.d.ts
|
||||||
|
/utils.js
|
||||||
|
/utils.d.ts
|
||||||
11
.npmignore
Normal file
11
.npmignore
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
.editorconfig
|
||||||
|
.github/
|
||||||
|
.gitignore
|
||||||
|
.nvmrc
|
||||||
|
coverage/
|
||||||
|
vite.config.ts
|
||||||
|
src/
|
||||||
|
tests/
|
||||||
|
tsconfig.json
|
||||||
|
utils.*
|
||||||
|
*.tgz
|
||||||
59
README.md
59
README.md
@@ -8,12 +8,15 @@ A lightweight JWT implementation with ZERO dependencies for Cloudflare Workers.
|
|||||||
- [Install](#install)
|
- [Install](#install)
|
||||||
- [Examples](#examples)
|
- [Examples](#examples)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
|
- [Sign](#sign)
|
||||||
|
- [Verify](#verify)
|
||||||
|
- [Decode](#decode)
|
||||||
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
```
|
```
|
||||||
npm i -D @tsndr/cloudflare-worker-jwt
|
npm i @tsndr/cloudflare-worker-jwt
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@@ -21,9 +24,9 @@ npm i -D @tsndr/cloudflare-worker-jwt
|
|||||||
|
|
||||||
### Basic Example
|
### Basic Example
|
||||||
|
|
||||||
```javascript
|
```typescript
|
||||||
async () => {
|
async () => {
|
||||||
const jwt = require('@tsndr/cloudflare-worker-jwt')
|
import jwt from '@tsndr/cloudflare-worker-jwt'
|
||||||
|
|
||||||
// Creating a token
|
// Creating a token
|
||||||
const token = await jwt.sign({ name: 'John Doe', email: 'john.doe@gmail.com' }, 'secret')
|
const token = await jwt.sign({ name: 'John Doe', email: 'john.doe@gmail.com' }, 'secret')
|
||||||
@@ -36,15 +39,15 @@ async () => {
|
|||||||
return
|
return
|
||||||
|
|
||||||
// Decoding token
|
// Decoding token
|
||||||
const payload = jwt.decode(token)
|
const { payload } = jwt.decode(token)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Restrict Timeframe
|
### Restrict Timeframe
|
||||||
|
|
||||||
```javascript
|
```typescript
|
||||||
async () => {
|
async () => {
|
||||||
const jwt = require('@tsndr/cloudflare-worker-jwt')
|
import jwt from '@tsndr/cloudflare-worker-jwt'
|
||||||
|
|
||||||
// Creating a token
|
// Creating a token
|
||||||
const token = await jwt.sign({
|
const token = await jwt.sign({
|
||||||
@@ -62,56 +65,78 @@ async () => {
|
|||||||
return
|
return
|
||||||
|
|
||||||
// Decoding token
|
// Decoding token
|
||||||
const payload = jwt.decode(token) // { name: 'John Doe', email: 'john.doe@gmail.com', ... }
|
const { payload } = jwt.decode(token) // { name: 'John Doe', email: 'john.doe@gmail.com', ... }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
- [Sign](#sign)
|
||||||
|
- [Verify](#verify)
|
||||||
|
- [Decode](#decode)
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
### `jwt.sign(payload, secret, [options])`
|
### Sign
|
||||||
|
#### `jwt.sign(payload, secret, [options])`
|
||||||
|
|
||||||
Signs a payload and returns the token.
|
Signs a payload and returns the token.
|
||||||
|
|
||||||
#### Arguments
|
#### Arguments
|
||||||
|
|
||||||
Argument | Type | Satus | Default | Description
|
Argument | Type | Status | 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.
|
`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.
|
`secret` | `string` | required | - | A string which is used to sign the payload.
|
||||||
`options` | `object`, `string` | optional | `{ algorithm: 'HS256' }` | The options object supporting `algorithm` and `keyid` or just the algorithm string. (See [Available Algorithms](#available-algorithms))
|
`options` | `object` | optional | `{ algorithm: 'HS256' }` | The options object supporting `algorithm` and `keyid`. (See [Available Algorithms](#available-algorithms))
|
||||||
|
|
||||||
#### `return`
|
#### `return`
|
||||||
Returns token as a `string`.
|
Returns token as a `string`.
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
### `jwt.verify(token, secret, [options])`
|
### Verify
|
||||||
|
#### `jwt.verify(token, secret, [options])`
|
||||||
|
|
||||||
Verifies the integrity of the token and returns a boolean value.
|
Verifies the integrity of the token and returns a boolean value.
|
||||||
|
|
||||||
Argument | Type | Satus | Default | Description
|
Argument | Type | Status | Default | Description
|
||||||
----------- | -------- | -------- | ------- | -----------
|
----------- | -------- | -------- | ------- | -----------
|
||||||
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
|
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
|
||||||
`secret` | `string` | required | - | The string which was used to sign the payload.
|
`secret` | `string` | required | - | The string which was used to sign the payload.
|
||||||
`algorithm` | `object`, `string` | optional | `{ algorithm: 'HS256', throwError: false }` | The options object supporting `algorithm` or just the algorithm string. (See [Available Algorithms](#available-algorithms))
|
`options` | `object` | optional | `{ algorithm: 'HS256', skipValidation: false, throwError: false }` | The options object supporting `algorithm`, `skipValidation` and `throwError`. (See [Available Algorithms](#available-algorithms))
|
||||||
|
|
||||||
|
#### `throws`
|
||||||
|
If `options.throwError` is `true` and the token is invalid, an error will be thrown.
|
||||||
|
|
||||||
#### `return`
|
#### `return`
|
||||||
Returns `true` if signature, `nbf` (if set) and `exp` (if set) are valid, otherwise returns `false`.
|
Returns `true` if signature, `nbf` (if set) and `exp` (if set) are valid, otherwise returns `false`.
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
### `jwt.decode(token)`
|
### Decode
|
||||||
|
#### `jwt.decode(token)`
|
||||||
|
|
||||||
Returns the payload **without** verifying the integrity of the token. Please use `jwt.verify()` first to keep your application secure!
|
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
|
Argument | Type | Status | Default | Description
|
||||||
----------- | -------- | -------- | ------- | -----------
|
----------- | -------- | -------- | ------- | -----------
|
||||||
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
|
`token` | `string` | required | - | The token string generated by `jwt.sign()`.
|
||||||
|
|
||||||
#### `return`
|
#### `return`
|
||||||
Returns payload `object`.
|
Returns an `object` containing `header` and `payload`:
|
||||||
|
```javascript
|
||||||
|
{
|
||||||
|
header: {
|
||||||
|
alg: 'HS256',
|
||||||
|
typ: 'JWT'
|
||||||
|
},
|
||||||
|
payload: {
|
||||||
|
name: 'John Doe',
|
||||||
|
email: 'john.doe@gmail.com'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Available Algorithms
|
### Available Algorithms
|
||||||
- ES256
|
- ES256
|
||||||
@@ -122,4 +147,4 @@ Returns payload `object`.
|
|||||||
- HS512
|
- HS512
|
||||||
- RS256
|
- RS256
|
||||||
- RS384
|
- RS384
|
||||||
- RS512
|
- RS512
|
||||||
|
|||||||
55
index.d.ts
vendored
55
index.d.ts
vendored
@@ -1,55 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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 {object | null} Returns payload `object`.
|
|
||||||
*/
|
|
||||||
decode(token: string): object | null
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
export = _exports
|
|
||||||
123
index.js
123
index.js
@@ -1,123 +0,0 @@
|
|||||||
class Base64URL {
|
|
||||||
static parse(s) {
|
|
||||||
return new Uint8Array(Array.prototype.map.call(atob(s.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, '')), c => c.charCodeAt(0)))
|
|
||||||
}
|
|
||||||
static stringify(a) {
|
|
||||||
return btoa(String.fromCharCode.apply(0, a)).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class JWT {
|
|
||||||
constructor() {
|
|
||||||
if (typeof crypto === 'undefined' || !crypto.subtle)
|
|
||||||
throw new Error('Crypto not supported!')
|
|
||||||
this.algorithms = {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
_decodePayload(raw) {
|
|
||||||
switch (raw.length % 4) {
|
|
||||||
case 0:
|
|
||||||
break
|
|
||||||
case 2:
|
|
||||||
raw += '=='
|
|
||||||
break
|
|
||||||
case 3:
|
|
||||||
raw += '='
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw new Error('Illegal base64url string!')
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
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' } }
|
|
||||||
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 }
|
|
||||||
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]), `${tokenParts[0]}.${tokenParts[1]}`)
|
|
||||||
}
|
|
||||||
decode(token) {
|
|
||||||
return this._decodePayload(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = new JWT
|
|
||||||
164
index.test.js
164
index.test.js
@@ -1,164 +0,0 @@
|
|||||||
const { subtle } = require('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-----
|
|
||||||
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7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AU8lYZXm8ubbqAlwz2VSVunD2tOplHyMUrtCtObAfVDU
|
|
||||||
AhCndKaA9gApgfb3xw1IKbuQ1u4IF1FJl3VtumfQn//LiH1B3rXhcdyo3/vIttEk
|
|
||||||
48RakUKClU8CgYEAzV7W3COOlDDcQd935DdtKBFRAPRPAlspQUnzMi5eSHMD/ISL
|
|
||||||
DY5IiQHbIH83D4bvXq0X7qQoSBSNP7Dvv3HYuqMhf0DaegrlBuJllFVVq9qPVRnK
|
|
||||||
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(payload).toBeTruthy()
|
|
||||||
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.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.6nmlvcCpsfENb6ssgX3rJ2XSjpFSXD1RPS1CK0iDZFdi6I6Gnmpi456RSW6-0XSSgq2E2XBcWCSYE6TeI63jOGZJTQ6-65g4sndbzBPqYPWbLny00NQ4MQgQXVu6tRzg',
|
|
||||||
ES512: 'eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.AbRwaCPJM2X3XjE2kInClHVGJNIcpL5C4a1ZrgwEM05RTryyNbazWRFbXAEDtHm8crvXpqw3a8JQwYDwvMyoOr4jAJ4RbhJitLgCGEzhKjvNy4xGrg3dV8gGEShFowgDfVz0KqHOX_Bc_DbyL-gtZPdfGTwT2upLkJE-lj47RStPDh0b',
|
|
||||||
HS256: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIx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M0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.oPvWzaCp8xUpt5mHhPSn0qLsZfCFj0NVmb4mz4dFQPCCMj-F5zVn9e3zZoj0lIXWM8rxB69QHC3Er47mtDt3BKgysTL3BvvV89kD6UjLoUcAI3lwj0mi7acLoE27i1_TnIBqWNRPAsdvTDawNE0_4lvI5bxEWQCqisJwxCoMDIeJsmDzfyApgU_SAFSVULxXwU2VewaxdQB-41OZdWwUEAxh81iB6DFWrqd2CaJkUYoWjgYpeWsyeC2m_-ECGrHGEz1nKTm9c7BaPxurz7fHD7RJd9Wpx-mKDVsfspO9quWb_OLeGGbxTtAomMvjQjut56kx2fqTleDnNDh_0GE88w',
|
|
||||||
RS512: 'eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.kEZmDAnHHU_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
|
|
||||||
})
|
|
||||||
})
|
|
||||||
9183
package-lock.json
generated
9183
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@@ -1,10 +1,16 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "1.3.0",
|
"version": "2.4.6",
|
||||||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
||||||
"main": "index.js",
|
"type": "module",
|
||||||
|
"exports": "./index.js",
|
||||||
|
"types": "index.d.ts",
|
||||||
|
"engine": {
|
||||||
|
"node": ">=18"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest"
|
"build": "tsc & esbuild --bundle --target=esnext --platform=neutral --outfile=index.js src/index.ts & wait",
|
||||||
|
"test": "vitest"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -24,6 +30,11 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/tsndr/cloudflare-worker-jwt#readme",
|
"homepage": "https://github.com/tsndr/cloudflare-worker-jwt#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"jest": "^28.1.0"
|
"@cloudflare/workers-types": "^4.20240208.0",
|
||||||
|
"@edge-runtime/vm": "^3.2.0",
|
||||||
|
"@types/node": "^20.11.19",
|
||||||
|
"ts-node": "^10.9.2",
|
||||||
|
"typescript": "^5.3.3",
|
||||||
|
"vitest": "^1.3.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
239
src/index.ts
Normal file
239
src/index.ts
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
import {
|
||||||
|
textToArrayBuffer,
|
||||||
|
arrayBufferToBase64Url,
|
||||||
|
base64UrlToArrayBuffer,
|
||||||
|
textToBase64Url,
|
||||||
|
importKey,
|
||||||
|
decodePayload
|
||||||
|
} from "./utils"
|
||||||
|
|
||||||
|
if (typeof crypto === 'undefined' || !crypto.subtle)
|
||||||
|
throw new Error('SubtleCrypto not supported!')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef JwtAlgorithm
|
||||||
|
* @type {'ES256' | 'ES384' | 'ES512' | 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512'}
|
||||||
|
*/
|
||||||
|
export type JwtAlgorithm = 'ES256' | 'ES384' | 'ES512' | 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef JwtAlgorithms
|
||||||
|
*/
|
||||||
|
export type JwtAlgorithms = {
|
||||||
|
[key: string]: SubtleCryptoImportKeyAlgorithm
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef JwtHeader
|
||||||
|
* @prop {string} [typ] Type
|
||||||
|
*/
|
||||||
|
export type JwtHeader<T = {}> = {
|
||||||
|
/**
|
||||||
|
* Type (default: `"JWT"`)
|
||||||
|
*
|
||||||
|
* @default "JWT"
|
||||||
|
*/
|
||||||
|
typ?: string
|
||||||
|
} & T
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef JwtPayload
|
||||||
|
* @prop {string} [iss] Issuer
|
||||||
|
* @prop {string} [sub] Subject
|
||||||
|
* @prop {string | string[]} [aud] Audience
|
||||||
|
* @prop {string} [exp] Expiration Time
|
||||||
|
* @prop {string} [nbf] Not Before
|
||||||
|
* @prop {string} [iat] Issued At
|
||||||
|
* @prop {string} [jti] JWT ID
|
||||||
|
*/
|
||||||
|
export type JwtPayload<T = { [key: string]: any }> = {
|
||||||
|
/** Issuer */
|
||||||
|
iss?: string
|
||||||
|
|
||||||
|
/** Subject */
|
||||||
|
sub?: string
|
||||||
|
|
||||||
|
/** Audience */
|
||||||
|
aud?: string | string[]
|
||||||
|
|
||||||
|
/** Expiration Time */
|
||||||
|
exp?: number
|
||||||
|
|
||||||
|
/** Not Before */
|
||||||
|
nbf?: number
|
||||||
|
|
||||||
|
/** Issued At */
|
||||||
|
iat?: number
|
||||||
|
|
||||||
|
/** JWT ID */
|
||||||
|
jti?: string
|
||||||
|
} & T
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef JwtOptions
|
||||||
|
* @prop {JwtAlgorithm | string} algorithm
|
||||||
|
*/
|
||||||
|
export type JwtOptions = {
|
||||||
|
algorithm?: JwtAlgorithm | string
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef JwtSignOptions
|
||||||
|
* @extends JwtOptions
|
||||||
|
* @prop {JwtHeader} [header]
|
||||||
|
*/
|
||||||
|
export type JwtSignOptions<T> = {
|
||||||
|
header?: JwtHeader<T>
|
||||||
|
} & JwtOptions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef JwtVerifyOptions
|
||||||
|
* @extends JwtOptions
|
||||||
|
* @prop {boolean} [throwError=false] If `true` throw error if checks fail. (default: `false`)
|
||||||
|
*/
|
||||||
|
export type JwtVerifyOptions = {
|
||||||
|
/**
|
||||||
|
* If `true` throw error if checks fail. (default: `false`)
|
||||||
|
*
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
throwError?: boolean
|
||||||
|
} & JwtOptions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef JwtData
|
||||||
|
* @prop {JwtHeader} header
|
||||||
|
* @prop {JwtPayload} payload
|
||||||
|
*/
|
||||||
|
export type JwtData<Payload = {}, Header = {}> = {
|
||||||
|
header?: JwtHeader<Header>
|
||||||
|
payload?: JwtPayload<Payload>
|
||||||
|
}
|
||||||
|
|
||||||
|
const algorithms: JwtAlgorithms = {
|
||||||
|
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' } }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signs a payload and returns the token
|
||||||
|
*
|
||||||
|
* @param {JwtPayload} payload The payload object. To use `nbf` (Not Before) and/or `exp` (Expiration Time) add `nbf` and/or `exp` to the payload.
|
||||||
|
* @param {string | JsonWebKey | CryptoKey} secret A string which is used to sign the payload.
|
||||||
|
* @param {JwtSignOptions | JwtAlgorithm | string} [options={ algorithm: 'HS256', header: { typ: 'JWT' } }] The options object or the algorithm.
|
||||||
|
* @throws {Error} If there's a validation issue.
|
||||||
|
* @returns {Promise<string>} Returns token as a `string`.
|
||||||
|
*/
|
||||||
|
export async function sign<Payload = {}, Header = {}>(payload: JwtPayload<Payload>, secret: string | JsonWebKey, options: JwtSignOptions<Header> | JwtAlgorithm = 'HS256'): Promise<string> {
|
||||||
|
if (typeof options === 'string')
|
||||||
|
options = { algorithm: options }
|
||||||
|
|
||||||
|
options = { algorithm: 'HS256', header: { typ: 'JWT' } as JwtHeader<Header>, ...options }
|
||||||
|
|
||||||
|
if (!payload || typeof payload !== 'object')
|
||||||
|
throw new Error('payload must be an object')
|
||||||
|
|
||||||
|
if (!secret || (typeof secret !== 'string' && typeof secret !== 'object'))
|
||||||
|
throw new Error('secret must be a string, a JWK object or a CryptoKey object')
|
||||||
|
|
||||||
|
if (typeof options.algorithm !== 'string')
|
||||||
|
throw new Error('options.algorithm must be a string')
|
||||||
|
|
||||||
|
const algorithm: SubtleCryptoImportKeyAlgorithm = algorithms[options.algorithm]
|
||||||
|
|
||||||
|
if (!algorithm)
|
||||||
|
throw new Error('algorithm not found')
|
||||||
|
|
||||||
|
if (!payload.iat)
|
||||||
|
payload.iat = Math.floor(Date.now() / 1000)
|
||||||
|
|
||||||
|
const partialToken = `${textToBase64Url(JSON.stringify({ ...options.header, alg: options.algorithm }))}.${textToBase64Url(JSON.stringify(payload))}`
|
||||||
|
|
||||||
|
const key = secret instanceof CryptoKey ? secret : await importKey(secret, algorithm, ['sign'])
|
||||||
|
const signature = await crypto.subtle.sign(algorithm, key, textToArrayBuffer(partialToken))
|
||||||
|
|
||||||
|
return `${partialToken}.${arrayBufferToBase64Url(signature)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies the integrity of the token and returns a boolean value.
|
||||||
|
*
|
||||||
|
* @param {string} token The token string generated by `jwt.sign()`.
|
||||||
|
* @param {string | JsonWebKey | CryptoKey} 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`.
|
||||||
|
*/
|
||||||
|
export async function verify(token: string, secret: string | JsonWebKey | CryptoKey, options: JwtVerifyOptions | JwtAlgorithm = { algorithm: 'HS256', throwError: false }): Promise<boolean> {
|
||||||
|
if (typeof options === 'string')
|
||||||
|
options = { algorithm: options, throwError: false }
|
||||||
|
|
||||||
|
options = { algorithm: 'HS256', throwError: false, ...options }
|
||||||
|
|
||||||
|
if (typeof token !== 'string')
|
||||||
|
throw new Error('token must be a string')
|
||||||
|
|
||||||
|
if (typeof secret !== 'string' && typeof secret !== 'object')
|
||||||
|
throw new Error('secret must be a string, a JWK object or a CryptoKey object')
|
||||||
|
|
||||||
|
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 algorithm: SubtleCryptoImportKeyAlgorithm = algorithms[options.algorithm]
|
||||||
|
|
||||||
|
if (!algorithm)
|
||||||
|
throw new Error('algorithm not found')
|
||||||
|
|
||||||
|
const { payload } = decode(token)
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!payload)
|
||||||
|
throw new Error('PARSE_ERROR')
|
||||||
|
|
||||||
|
if (payload.nbf && payload.nbf > Math.floor(Date.now() / 1000))
|
||||||
|
throw new Error('NOT_YET_VALID')
|
||||||
|
|
||||||
|
if (payload.exp && payload.exp <= Math.floor(Date.now() / 1000))
|
||||||
|
throw new Error('EXPIRED')
|
||||||
|
|
||||||
|
const key = secret instanceof CryptoKey ? secret : await importKey(secret, algorithm, ['verify'])
|
||||||
|
|
||||||
|
return await crypto.subtle.verify(algorithm, key, base64UrlToArrayBuffer(tokenParts[2]), textToArrayBuffer(`${tokenParts[0]}.${tokenParts[1]}`))
|
||||||
|
} catch(err) {
|
||||||
|
if (options.throwError)
|
||||||
|
throw err
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {JwtData} Returns an `object` containing `header` and `payload`.
|
||||||
|
*/
|
||||||
|
export function decode<Payload = {}, Header = {}>(token: string): JwtData<Payload, Header> {
|
||||||
|
return {
|
||||||
|
header: decodePayload<JwtHeader<Header>>(token.split('.')[0].replace(/-/g, '+').replace(/_/g, '/')),
|
||||||
|
payload: decodePayload<JwtPayload<Payload>>(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
sign,
|
||||||
|
verify,
|
||||||
|
decode
|
||||||
|
}
|
||||||
94
src/utils.ts
Normal file
94
src/utils.ts
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
export function bytesToByteString(bytes: Uint8Array): string {
|
||||||
|
let byteStr = ''
|
||||||
|
for (let i = 0; i < bytes.byteLength; i++) {
|
||||||
|
byteStr += String.fromCharCode(bytes[i])
|
||||||
|
}
|
||||||
|
return byteStr
|
||||||
|
}
|
||||||
|
|
||||||
|
export function byteStringToBytes(byteStr: string): Uint8Array {
|
||||||
|
let bytes = new Uint8Array(byteStr.length)
|
||||||
|
for (let i = 0; i < byteStr.length; i++) {
|
||||||
|
bytes[i] = byteStr.charCodeAt(i)
|
||||||
|
}
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
export function arrayBufferToBase64String(arrayBuffer: ArrayBuffer): string {
|
||||||
|
return btoa(bytesToByteString(new Uint8Array(arrayBuffer)))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function base64StringToArrayBuffer(b64str: string): ArrayBuffer {
|
||||||
|
return byteStringToBytes(atob(b64str)).buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
export function textToArrayBuffer(str: string): ArrayBuffer {
|
||||||
|
return byteStringToBytes(decodeURI(encodeURIComponent(str)))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function arrayBufferToText(arrayBuffer: ArrayBuffer): string {
|
||||||
|
return bytesToByteString(new Uint8Array(arrayBuffer))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function arrayBufferToBase64Url(arrayBuffer: ArrayBuffer): string {
|
||||||
|
return arrayBufferToBase64String(arrayBuffer).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function base64UrlToArrayBuffer(b64url: string): ArrayBuffer {
|
||||||
|
return base64StringToArrayBuffer(b64url.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, ''))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function textToBase64Url(str: string): string {
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const charCodes = encoder.encode(str);
|
||||||
|
const binaryStr = String.fromCharCode(...charCodes);
|
||||||
|
return btoa(binaryStr).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function pemToBinary(pem: string): ArrayBuffer {
|
||||||
|
return base64StringToArrayBuffer(pem.replace(/-+(BEGIN|END).*/g, '').replace(/\s/g, ''))
|
||||||
|
}
|
||||||
|
|
||||||
|
type KeyUsages = 'sign' | 'verify';
|
||||||
|
export async function importTextSecret(key: string, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey> {
|
||||||
|
return await crypto.subtle.importKey("raw", textToArrayBuffer(key), algorithm, true, keyUsages)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function importJwk(key: JsonWebKey, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey> {
|
||||||
|
return await crypto.subtle.importKey("jwk", key, algorithm, true, keyUsages)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function importPublicKey(key: string, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey> {
|
||||||
|
return await crypto.subtle.importKey("spki", pemToBinary(key), algorithm, true, keyUsages)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function importPrivateKey(key: string, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey> {
|
||||||
|
return await crypto.subtle.importKey("pkcs8", pemToBinary(key), algorithm, true, keyUsages)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function importKey(key: string | JsonWebKey, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey> {
|
||||||
|
if (typeof key === 'object')
|
||||||
|
return importJwk(key, algorithm, keyUsages)
|
||||||
|
|
||||||
|
if (typeof key !== 'string')
|
||||||
|
throw new Error('Unsupported key type!')
|
||||||
|
|
||||||
|
if (key.includes('PUBLIC'))
|
||||||
|
return importPublicKey(key, algorithm, keyUsages)
|
||||||
|
|
||||||
|
if (key.includes('PRIVATE'))
|
||||||
|
return importPrivateKey(key, algorithm, keyUsages)
|
||||||
|
|
||||||
|
return importTextSecret(key, algorithm, keyUsages)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decodePayload<T = any>(raw: string): T | undefined {
|
||||||
|
try {
|
||||||
|
const bytes = Array.from(atob(raw), char => char.charCodeAt(0));
|
||||||
|
const decodedString = new TextDecoder('utf-8').decode(new Uint8Array(bytes));
|
||||||
|
|
||||||
|
return JSON.parse(decodedString);
|
||||||
|
} catch {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
122
tests/index.spec.ts
Normal file
122
tests/index.spec.ts
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
import { describe, expect, test } from 'vitest'
|
||||||
|
import jwt, { JwtAlgorithm } from '../src/index'
|
||||||
|
|
||||||
|
type Dataset = {
|
||||||
|
public: string
|
||||||
|
private: string
|
||||||
|
token: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Data = {
|
||||||
|
[key in JwtAlgorithm]: Dataset
|
||||||
|
}
|
||||||
|
|
||||||
|
type Payload = {
|
||||||
|
sub: string
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const data: Data = {
|
||||||
|
'ES256': {
|
||||||
|
public: '-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEVs/o5+uQbTjL3chynL4wXgUg2R9\nq9UU8I5mEovUf86QZ7kOBIjJwqnzD1omageEHWwHdBO6B+dFabmdT9POxg==\n-----END PUBLIC KEY-----',
|
||||||
|
private: '-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgevZzL1gdAFr88hb2\nOF/2NxApJCzGCEDdfSp6VQO30hyhRANCAAQRWz+jn65BtOMvdyHKcvjBeBSDZH2r\n1RTwjmYSi9R/zpBnuQ4EiMnCqfMPWiZqB4QdbAd0E7oH50VpuZ1P087G\n-----END PRIVATE KEY-----',
|
||||||
|
token: 'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.Ri9u3ZJpyoHf1_i3KpVE5gMggyU3VMYPeEVktAsG1kGLOxFNJBXydQls3WFBaXXH2-sN74IMe-nDcM7NoJ6GMQ'
|
||||||
|
},
|
||||||
|
'ES384': {
|
||||||
|
public: '-----BEGIN PUBLIC KEY-----\nMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEC1uWSXj2czCDwMTLWV5BFmwxdM6PX9p+\nPk9Yf9rIf374m5XP1U8q79dBhLSIuaojsvOT39UUcPJROSD1FqYLued0rXiooIii\n1D3jaW6pmGVJFhodzC31cy5sfOYotrzF\n-----END PUBLIC KEY-----',
|
||||||
|
private: '-----BEGIN PRIVATE KEY-----\nMIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDCAHpFQ62QnGCEvYh/p\nE9QmR1C9aLcDItRbslbmhen/h1tt8AyMhskeenT+rAyyPhGhZANiAAQLW5ZJePZz\nMIPAxMtZXkEWbDF0zo9f2n4+T1h/2sh/fviblc/VTyrv10GEtIi5qiOy85Pf1RRw\n8lE5IPUWpgu553SteKigiKLUPeNpbqmYZUkWGh3MLfVzLmx85ii2vMU=\n-----END PRIVATE KEY-----',
|
||||||
|
token: 'eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.6nmlvcCpsfENb6ssgX3rJ2XSjpFSXD1RPS1CK0iDZFdi6I6Gnmpi456RSW6-0XSSgq2E2XBcWCSYE6TeI63jOGZJTQ6-65g4sndbzBPqYPWbLny00NQ4MQgQXVu6tRzg'
|
||||||
|
},
|
||||||
|
'ES512': {
|
||||||
|
public: '-----BEGIN PUBLIC KEY-----\nMIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBgc4HZz+/fBbC7lmEww0AO3NK9wVZ\nPDZ0VEnsaUFLEYpTzb90nITtJUcPUbvOsdZIZ1Q8fnbquAYgxXL5UgHMoywAib47\n6MkyyYgPk0BXZq3mq4zImTRNuaU9slj9TVJ3ScT3L1bXwVuPJDzpr5GOFpaj+WwM\nAl8G7CqwoJOsW7Kddns=\n-----END PUBLIC KEY-----',
|
||||||
|
private: '-----BEGIN PRIVATE KEY-----\nMIHuAgEAMBAGByqGSM49AgEGBSuBBAAjBIHWMIHTAgEBBEIBiyAa7aRHFDCh2qga\n9sTUGINE5jHAFnmM8xWeT/uni5I4tNqhV5Xx0pDrmCV9mbroFtfEa0XVfKuMAxxf\nZ6LM/yKhgYkDgYYABAGBzgdnP798FsLuWYTDDQA7c0r3BVk8NnRUSexpQUsRilPN\nv3SchO0lRw9Ru86x1khnVDx+duq4BiDFcvlSAcyjLACJvjvoyTLJiA+TQFdmrear\njMiZNE25pT2yWP1NUndJxPcvVtfBW48kPOmvkY4WlqP5bAwCXwbsKrCgk6xbsp12\new==\n-----END PRIVATE KEY-----',
|
||||||
|
token: 'eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.AbRwaCPJM2X3XjE2kInClHVGJNIcpL5C4a1ZrgwEM05RTryyNbazWRFbXAEDtHm8crvXpqw3a8JQwYDwvMyoOr4jAJ4RbhJitLgCGEzhKjvNy4xGrg3dV8gGEShFowgDfVz0KqHOX_Bc_DbyL-gtZPdfGTwT2upLkJE-lj47RStPDh0b'
|
||||||
|
},
|
||||||
|
'HS256': {
|
||||||
|
public: 'secret',
|
||||||
|
private: 'secret',
|
||||||
|
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o'
|
||||||
|
},
|
||||||
|
'HS384': {
|
||||||
|
public: 'secret',
|
||||||
|
private: 'secret',
|
||||||
|
token: 'eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.hO2sthNQUSfvI9ylUdMKDxcrm8jB3KL6Rtkd3FOskL-jVqYh2CK1es8FKCQO8_tW'
|
||||||
|
},
|
||||||
|
'HS512': {
|
||||||
|
public: 'secret',
|
||||||
|
private: 'secret',
|
||||||
|
token: 'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.wUVS6tazE2N98_J4SH_djkEe1igXPu0qILAvVXCiO6O20gdf5vZ2sYFWX3c-Hy6L4TD47b3DSAAO9XjSqpJfag'
|
||||||
|
},
|
||||||
|
'RS256': {
|
||||||
|
public: '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8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IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.kEZmDAnHHU_0bcGXMd5LA7vF87yQgXNaioPHP4lU4O3JJYuZ54fJdv3HT58xk-MFEDuWro_5fvNIp2VM-PlkZvYWrhQkJ-c-seoSa3ANq_PciC3bGfzYHEdjAE71GrAMI4FlcAGsq3ChkOnCTFqjWDmVwaRYCgMsFQ-U5cjvFhndFMizrkRljTF4v5oFdWytV_J-UafPtNdQXcGND1M74DqObnTHhZHg8aDfNzZcvnIeKcDVGUlUEL5ia1kPMrVhCtOAOJmEU8ivCdWWzt-jMQBf7cZeoCzDKHG72ysTTCfRoBVc1_SrQTHcHDiiBeW9nCazMLkltyP5NeawR_RNlg'
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload: Payload = {
|
||||||
|
sub: "1234567890",
|
||||||
|
name: "John Doe",
|
||||||
|
}
|
||||||
|
|
||||||
|
const unicodePayload: Payload = {
|
||||||
|
sub: "1234567890",
|
||||||
|
name: "John Doe 😎",
|
||||||
|
}
|
||||||
|
|
||||||
|
describe.each(Object.entries(data) as [JwtAlgorithm, Dataset][])('%s', (algorithm, data) => {
|
||||||
|
let token = ''
|
||||||
|
|
||||||
|
test('verify external', async () => {
|
||||||
|
const verified = await jwt.verify(data.token, data.public, algorithm)
|
||||||
|
expect(verified).toBeTruthy()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('decode external', async () => {
|
||||||
|
const decoded = jwt.decode<Payload>(data.token)
|
||||||
|
expect({
|
||||||
|
sub: payload.sub,
|
||||||
|
name: payload.name
|
||||||
|
}).toMatchObject({
|
||||||
|
sub: decoded.payload?.sub,
|
||||||
|
name: payload.name
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('sign internal', async () => {
|
||||||
|
token = await jwt.sign<Payload>(payload, data.private, algorithm)
|
||||||
|
expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('sign unciode', async () => {
|
||||||
|
token = await jwt.sign<Payload>(unicodePayload, data.private, algorithm)
|
||||||
|
expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('decode internal', async () => {
|
||||||
|
const decoded = jwt.decode(token)
|
||||||
|
expect({
|
||||||
|
sub: payload.sub,
|
||||||
|
name: payload.name
|
||||||
|
}).toMatchObject({
|
||||||
|
sub: decoded.payload?.sub,
|
||||||
|
name: payload.name
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('verify internal', async () => {
|
||||||
|
const verified = await jwt.verify(token, data.public, algorithm)
|
||||||
|
expect(verified).toBeTruthy()
|
||||||
|
})
|
||||||
|
})
|
||||||
81
tests/utils.spec.ts
Normal file
81
tests/utils.spec.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { describe, expect, test } from 'vitest'
|
||||||
|
import {
|
||||||
|
bytesToByteString,
|
||||||
|
byteStringToBytes,
|
||||||
|
arrayBufferToBase64String,
|
||||||
|
base64StringToArrayBuffer,
|
||||||
|
textToArrayBuffer,
|
||||||
|
arrayBufferToText,
|
||||||
|
arrayBufferToBase64Url,
|
||||||
|
base64UrlToArrayBuffer,
|
||||||
|
textToBase64Url,
|
||||||
|
pemToBinary,
|
||||||
|
importTextSecret
|
||||||
|
} from '../src/utils'
|
||||||
|
|
||||||
|
describe('Converters', () => {
|
||||||
|
const testString = 'cloudflare-worker-jwt'
|
||||||
|
const testByteArray = [ 99, 108, 111, 117, 100, 102, 108, 97, 114, 101, 45, 119, 111, 114, 107, 101, 114, 45, 106, 119, 116 ]
|
||||||
|
const testUint8Array = new Uint8Array(testByteArray)
|
||||||
|
const testBase64String = 'Y2xvdWRmbGFyZS13b3JrZXItand0'
|
||||||
|
const testArrayBuffer = testUint8Array.buffer
|
||||||
|
|
||||||
|
test('bytesToByteString', () => {
|
||||||
|
expect(bytesToByteString(testUint8Array)).toStrictEqual(testString)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('byteStringToBytes', () => {
|
||||||
|
expect(byteStringToBytes(testString)).toStrictEqual(testUint8Array)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('arrayBufferToBase64String', () => {
|
||||||
|
expect(arrayBufferToBase64String(testArrayBuffer)).toStrictEqual(testBase64String)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('base64StringToArrayBuffer', () => {
|
||||||
|
expect(base64StringToArrayBuffer(testBase64String)).toStrictEqual(testArrayBuffer)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('textToArrayBuffer', () => {
|
||||||
|
expect(textToArrayBuffer(testString)).toStrictEqual(testUint8Array)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('arrayBufferToText', () => {
|
||||||
|
expect(arrayBufferToText(testArrayBuffer)).toStrictEqual(testString)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('arrayBufferToBase64Url', () => {
|
||||||
|
expect(arrayBufferToBase64Url(testArrayBuffer)).toStrictEqual(testBase64String)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('base64UrlToArrayBuffer', () => {
|
||||||
|
expect(base64UrlToArrayBuffer(testBase64String)).toStrictEqual(testArrayBuffer)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('textToBase64Url', () => {
|
||||||
|
expect(textToBase64Url(testString)).toStrictEqual(testBase64String)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('pemToBinary', () => {
|
||||||
|
expect(pemToBinary(`-----BEGIN PUBLIC KEY-----\n${testBase64String}\n-----END PUBLIC KEY-----`)).toStrictEqual(testArrayBuffer)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Imports', () => {
|
||||||
|
test('importTextSecret', async () => {
|
||||||
|
const testKey = 'cloudflare-worker-jwt'
|
||||||
|
const testAlgorithm = { name: 'HMAC', hash: { name: 'SHA-256' } }
|
||||||
|
const testCryptoKey = { type: 'secret', extractable: true, algorithm: { ...testAlgorithm, length: 168 }, usages: ['verify', 'sign'] }
|
||||||
|
|
||||||
|
expect(await importTextSecret(testKey, testAlgorithm, ['verify', 'sign'])).toMatchObject(testCryptoKey)
|
||||||
|
})
|
||||||
|
|
||||||
|
//test('importJwk', async () => {})
|
||||||
|
//test('importPublicKey', async () => {})
|
||||||
|
//test('importPrivateKey', async () => {})
|
||||||
|
//test('importKey', async () => {})
|
||||||
|
})
|
||||||
|
|
||||||
|
//describe('Payload', () => {
|
||||||
|
// test('decodePayload', () => {})
|
||||||
|
//})
|
||||||
28
tsconfig.json
Normal file
28
tsconfig.json
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": ".",
|
||||||
|
"module": "esnext",
|
||||||
|
"target": "esnext",
|
||||||
|
"lib": ["esnext"],
|
||||||
|
"declaration": true,
|
||||||
|
"emitDeclarationOnly": true,
|
||||||
|
"strict": true,
|
||||||
|
"noImplicitAny": true,
|
||||||
|
"strictNullChecks": true,
|
||||||
|
"strictFunctionTypes": true,
|
||||||
|
"strictBindCallApply": true,
|
||||||
|
"strictPropertyInitialization": true,
|
||||||
|
"noImplicitThis": true,
|
||||||
|
"alwaysStrict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"noImplicitReturns": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"preserveConstEnums": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"types": ["@cloudflare/workers-types"]
|
||||||
|
},
|
||||||
|
"include": ["src"],
|
||||||
|
"exclude": ["node_modules", "src/**/*.spec.ts"]
|
||||||
|
}
|
||||||
9
vite.config.ts
Normal file
9
vite.config.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { defineConfig } from 'vitest/config'
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: {
|
||||||
|
environment: 'edge-runtime',
|
||||||
|
watch: false,
|
||||||
|
reporters: ['verbose']
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user