forked from choffmeister/Paw-JsonWebTokenDynamicValue
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Algorithm select. Add logging.
- Loading branch information
Showing
1 changed file
with
41 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,72 @@ | ||
import jsrsasign from 'jsrsasign'; | ||
|
||
// list from | ||
// https://kjur.github.io/jsrsasign/api/symbols/KJUR.jws.JWS.html#.sign | ||
const SUPPORTED_ALGS = [ | ||
'HS256', | ||
'HS384', | ||
'HS512', | ||
'RS256', | ||
'RS384', | ||
'RS512', | ||
'ES256', | ||
'ES384', | ||
'ES512', | ||
'PS256', | ||
'PS384', | ||
'PS512' | ||
] | ||
let ALGS_CHOICES = {} | ||
SUPPORTED_ALGS.forEach(alg => { | ||
ALGS_CHOICES[alg] = alg | ||
}) | ||
|
||
@registerDynamicValueClass | ||
class JsonWebTokenDynamicValue { | ||
static identifier = 'de.choffmeister.PawExtensions.JsonWebTokenDynamicValue'; | ||
static title = 'Json Web Token'; | ||
static help = 'https://github.com/choffmeister/Paw-JsonWebTokenDynamicValue'; | ||
|
||
static title = 'Json Web Token (JWT)'; | ||
static help = 'https://github.com/luckymarmot/Paw-JsonWebTokenDynamicValue'; | ||
static inputs = [ | ||
InputField('header', 'Header', 'JSON', {persisted: true, defaultValue: '{"typ": "JWT", "alg": "HS256"}'}), | ||
DynamicValueInput('payload', 'Payload', 'JSON'), | ||
DynamicValueInput('addTimeFields', 'Add Time Fields (iat & exp)', 'Checkbox', {defaultValue: true}), | ||
DynamicValueInput('signatureSecret', 'Secret', 'SecureValue'), | ||
DynamicValueInput('signatureSecretIsBase64', 'Secret is Base64', 'Checkbox') | ||
InputField('alg', 'Algorithm', 'Select', {choices: ALGS_CHOICES, defaultValue: 'HS256'}), | ||
InputField('header', 'Header', 'JSON', {defaultValue: '{}'}), | ||
InputField('payload', 'Payload', 'JSON'), | ||
InputField('addTimeFields', 'Add Time Fields (iat & exp)', 'Checkbox', {defaultValue: true}), | ||
InputField('signatureSecret', 'Secret', 'SecureValue'), | ||
InputField('signatureSecretIsBase64', 'Secret is Base64', 'Checkbox') | ||
]; | ||
|
||
title() { | ||
return 'JSON Web Token' | ||
} | ||
|
||
evaluate() { | ||
const now = Math.floor((new Date()).getTime() / 1000); | ||
|
||
const header = { | ||
typ: "JWT", | ||
alg: "HS256", | ||
...this.header | ||
} | ||
|
||
let payload | ||
if(this.addTimeFields) { | ||
payload = { | ||
iat: now, | ||
exp: now + 60, | ||
...this.payload, | ||
} | ||
} | ||
else { | ||
payload = this.payload | ||
} else { | ||
payload = { ...this.payload } | ||
} | ||
|
||
const secret = this.signatureSecretIsBase64 | ||
? {b64: jsrsasign.b64utob64(this.signatureSecret)} | ||
: {rstr: this.signatureSecret}; | ||
|
||
console.log(`Sign JWT: Header: ${JSON.stringify(header)} Payload: ${JSON.stringify(payload)}`) | ||
if (SUPPORTED_ALGS.indexOf(header.alg) < 0) { | ||
console.error(`Unsupported algorithm '${header.alg}' (supports ${SUPPORTED_ALGS.join(', ')})`) | ||
} | ||
|
||
return jsrsasign.jws.JWS.sign(null, header, payload, secret); | ||
} | ||
} |