Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
b733a0650d
|
|||
|
43879de15e
|
|||
|
0c8f476751
|
|||
|
3c5d178fec
|
|||
| e0219ff21f | |||
|
|
bc7fa845ed | ||
|
|
5ee043e597 | ||
|
|
9c52217ca2 | ||
|
|
5160cfa416 |
19
.github/workflows/lint.yml
vendored
19
.github/workflows/lint.yml
vendored
@@ -1,19 +0,0 @@
|
|||||||
name: Lint
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches: [ main ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- name: Use Node.js
|
|
||||||
uses: actions/setup-node@v1
|
|
||||||
with:
|
|
||||||
node-version: 15.x
|
|
||||||
- run: npm ci
|
|
||||||
- run: npm run lint --if-present
|
|
||||||
@@ -95,7 +95,7 @@ Argument | Type | Satus | 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' }` | The options object supporting `algorithm` or just the algorithm string. (See [Available Algorithms](#available-algorithms))
|
`algorithm` | `object`, `string` | optional | `{ algorithm: 'HS256', throwError: false }` | The options object supporting `algorithm` or just the algorithm string. (See [Available Algorithms](#available-algorithms))
|
||||||
|
|
||||||
#### `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`.
|
||||||
@@ -120,3 +120,6 @@ Returns payload `object`.
|
|||||||
- HS256
|
- HS256
|
||||||
- HS384
|
- HS384
|
||||||
- HS512
|
- HS512
|
||||||
|
- RS256
|
||||||
|
- RS384
|
||||||
|
- RS512
|
||||||
5
index.d.ts
vendored
5
index.d.ts
vendored
@@ -13,6 +13,7 @@ declare class JWT {
|
|||||||
* @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 {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 {string} secret A string which is used to sign the payload.
|
||||||
* @param {JWTSignOptions | JWTAlgorithm} options The options object or the algorithm.
|
* @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`.
|
* @returns {Promise<string>} Returns token as a `string`.
|
||||||
*/
|
*/
|
||||||
sign(payload: object, secret: string, options?: JWTSignOptions | JWTAlgorithm): Promise<string>
|
sign(payload: object, secret: string, options?: JWTSignOptions | JWTAlgorithm): Promise<string>
|
||||||
@@ -23,6 +24,7 @@ declare class JWT {
|
|||||||
* @param {string} token The token string generated by `jwt.sign()`.
|
* @param {string} token The token string generated by `jwt.sign()`.
|
||||||
* @param {string} secret The string which was used to sign the payload.
|
* @param {string} secret The string which was used to sign the payload.
|
||||||
* @param {JWTVerifyOptions | JWTAlgorithm} options The options object or the algorithm.
|
* @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`.
|
* @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>
|
verify(token: string, secret: string, options?: JWTVerifyOptions | JWTAlgorithm): Promise<boolean>
|
||||||
@@ -37,7 +39,7 @@ declare class JWT {
|
|||||||
}
|
}
|
||||||
declare const _exports: JWT
|
declare const _exports: JWT
|
||||||
|
|
||||||
type JWTAlgorithm = 'ES256' | 'ES384' | 'ES512' | 'HS256' | 'HS384' | 'HS512'
|
type JWTAlgorithm = 'ES256' | 'ES384' | 'ES512' | 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512'
|
||||||
|
|
||||||
type JWTSignOptions = {
|
type JWTSignOptions = {
|
||||||
algorithm?: JWTAlgorithm,
|
algorithm?: JWTAlgorithm,
|
||||||
@@ -46,6 +48,7 @@ type JWTSignOptions = {
|
|||||||
|
|
||||||
type JWTVerifyOptions = {
|
type JWTVerifyOptions = {
|
||||||
algorithm?: JWTAlgorithm
|
algorithm?: JWTAlgorithm
|
||||||
|
throwError?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export = _exports
|
export = _exports
|
||||||
19
index.js
19
index.js
@@ -9,7 +9,7 @@ class Base64URL {
|
|||||||
|
|
||||||
class JWT {
|
class JWT {
|
||||||
constructor() {
|
constructor() {
|
||||||
if (!crypto || !crypto.subtle)
|
if (typeof crypto === 'undefined' || !crypto.subtle)
|
||||||
throw new Error('Crypto not supported!')
|
throw new Error('Crypto not supported!')
|
||||||
this.algorithms = {
|
this.algorithms = {
|
||||||
ES256: { name: 'ECDSA', namedCurve: 'P-256', hash: { name: 'SHA-256' } },
|
ES256: { name: 'ECDSA', namedCurve: 'P-256', hash: { name: 'SHA-256' } },
|
||||||
@@ -17,7 +17,10 @@ class JWT {
|
|||||||
ES512: { name: 'ECDSA', namedCurve: 'P-512', hash: { name: 'SHA-512' } },
|
ES512: { name: 'ECDSA', namedCurve: 'P-512', hash: { name: 'SHA-512' } },
|
||||||
HS256: { name: 'HMAC', hash: { name: 'SHA-256' } },
|
HS256: { name: 'HMAC', hash: { name: 'SHA-256' } },
|
||||||
HS384: { name: 'HMAC', hash: { name: 'SHA-384' } },
|
HS384: { name: 'HMAC', hash: { name: 'SHA-384' } },
|
||||||
HS512: { name: 'HMAC', hash: { name: 'SHA-512' } }
|
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) {
|
_utf8ToUint8Array(str) {
|
||||||
@@ -76,7 +79,7 @@ class JWT {
|
|||||||
const signature = await crypto.subtle.sign(importAlgorithm, key, this._utf8ToUint8Array(partialToken))
|
const signature = await crypto.subtle.sign(importAlgorithm, key, this._utf8ToUint8Array(partialToken))
|
||||||
return `${partialToken}.${Base64URL.stringify(new Uint8Array(signature))}`
|
return `${partialToken}.${Base64URL.stringify(new Uint8Array(signature))}`
|
||||||
}
|
}
|
||||||
async verify(token, secret, options = { algorithm: 'HS256' }) {
|
async verify(token, secret, options = { algorithm: 'HS256', throwError: false }) {
|
||||||
if (typeof options === 'string')
|
if (typeof options === 'string')
|
||||||
options = { algorithm: options }
|
options = { algorithm: options }
|
||||||
if (typeof token !== 'string')
|
if (typeof token !== 'string')
|
||||||
@@ -92,10 +95,16 @@ class JWT {
|
|||||||
if (!importAlgorithm)
|
if (!importAlgorithm)
|
||||||
throw new Error('algorithm not found')
|
throw new Error('algorithm not found')
|
||||||
const payload = this.decode(token)
|
const payload = this.decode(token)
|
||||||
if (payload.nbf && payload.nbf >= Math.floor(Date.now() / 1000))
|
if (payload.nbf && payload.nbf > Math.floor(Date.now() / 1000)) {
|
||||||
|
if (options.throwError)
|
||||||
|
throw 'NOT_YET_VALID'
|
||||||
return false
|
return false
|
||||||
if (payload.exp && payload.exp < Math.floor(Date.now() / 1000))
|
}
|
||||||
|
if (payload.exp && payload.exp <= Math.floor(Date.now() / 1000)) {
|
||||||
|
if (options.throwError)
|
||||||
|
throw 'EXPIRED'
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
let keyFormat = 'raw'
|
let keyFormat = 'raw'
|
||||||
let keyData
|
let keyData
|
||||||
if (secret.startsWith('-----BEGIN')) {
|
if (secret.startsWith('-----BEGIN')) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "1.1.5",
|
"version": "1.2.0",
|
||||||
"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",
|
"main": "index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
Reference in New Issue
Block a user