Added more algorithms, keyid and did some cleanup
This commit is contained in:
87
index.js
87
index.js
@@ -12,23 +12,25 @@ class JWT {
|
||||
if (!crypto || !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-512', 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' } }
|
||||
}
|
||||
}
|
||||
_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:
|
||||
@@ -48,57 +50,62 @@ class JWT {
|
||||
return null
|
||||
}
|
||||
}
|
||||
async sign(payload, secret, algorithm = 'HS256') {
|
||||
async sign(payload, secret, options = { algorithm: 'HS256' }) {
|
||||
if (typeof options === 'string')
|
||||
options = { algorithm: 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 algorithm !== 'string')
|
||||
throw new Error('algorithm must be a string')
|
||||
const importAlgorithm = this.algorithms[algorithm]
|
||||
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({ alg: algorithm, 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))
|
||||
const partialToken = `${Base64URL.stringify(this._utf8ToUint8Array(JSON.stringify({ 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, algorithm = 'HS256') {
|
||||
async verify(token, secret, options = { algorithm: 'HS256' }) {
|
||||
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 algorithm !== 'string')
|
||||
throw new Error('algorithm 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 have 3 parts')
|
||||
const importAlgorithm = this.algorithms[algorithm]
|
||||
const importAlgorithm = this.algorithms[options.algorithm]
|
||||
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 payload = this._decodePayload(tokenParts[1].replace(/-/g, '+').replace(/_/g, '/'))
|
||||
const payload = this.decode(token)
|
||||
if (payload.nbf && payload.nbf >= Math.floor(Date.now() / 1000))
|
||||
return false
|
||||
if (payload.exp && payload.exp < Math.floor(Date.now() / 1000))
|
||||
return false
|
||||
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
|
||||
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 res = await crypto.subtle.sign(importAlgorithm, key, this._utf8ToUint8Array(tokenParts.slice(0, 2).join('.')))
|
||||
return Base64URL.stringify(new Uint8Array(res)) === tokenParts[2]
|
||||
}
|
||||
decode(token) {
|
||||
return this._decodePayload(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'))
|
||||
|
||||
Reference in New Issue
Block a user