Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secret base64 encoded support #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ lib-cov
# Coverage directory used by tools like istanbul
coverage

# Intellij idea project configuration directory
.idea

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

Expand All @@ -25,4 +28,4 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
/nbproject/private/
/nbproject/private/
10 changes: 10 additions & 0 deletions jwt.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ <h3>References</h3>
alg: { value: ["HS256"] },
jwkurl: { value: "" },
secret: { value: "" },
secb64enc: { value: "false" },
key: { value: "" },
signvar: { value: "payload" },
storetoken: { value: "payload" }
Expand Down Expand Up @@ -166,6 +167,13 @@ <h3>References</h3>
<label for="node-input-secret"><i class="fa fa-lock"></i> Secret</label>
<input type="password" id="node-input-secret" placeholder="Secret">
</div>
<div class="form-row">
<label for="node-input-secb64enc"><i class="fa fa-lock"></i> SecB64Enc.</label>
<select id="node-input-secb64enc">
<option value="false" selected>False</option>
<option value="true">True</option>
</select>
</div>
<div class="form-row">
<label for="node-input-key"><i class="fa fa-file"></i> Key File</label>
<input type="text" id="node-input-key" placeholder="Key File">
Expand Down Expand Up @@ -234,6 +242,8 @@ <h3>Details</h3>

<p>Algorithms RS256, RS384, RS512, ES256, ES384, ES512 use a PEM encoded public key file loaded from the <code>Key File</code> property, or string containing the key from <code>NODE_RED_NODE_JWT_PUBLIC_KEY</code> environment variable.</p>

<p>Select <code>SecB64Enc.</code> as True if the secret base64 encoded</p>

<p>Algorithms HS256, HS384, HS512 use the <code>secret</code> property or the <code>NODE_RED_NODE_JWT_SECRET</code> environment variable.</p>

<h3>References</h3>
Expand Down
31 changes: 18 additions & 13 deletions jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (RED) {
// changed to load key on deploy level and not on runtime level why fs.readFileSync is sync.
if (node.alg === 'RS256' ||
node.alg === 'RS384' ||
node.alg === 'RS512' ||
node.alg === 'RS512' ||
node.alg === 'ES256' ||
node.alg === 'ES384' ||
node.alg === 'ES512') {
Expand All @@ -45,16 +45,16 @@ module.exports = function (RED) {
node.secret = key.key.toPrivateKeyPEM();
}
jwt.sign(msg[node.signvar],
node.secret,
{algorithm: node.alg, expiresIn: node.exp, keyid: node.jwkkid}, function (err, token) {
if (err) {
done(err);
} else {
msg[node.storetoken] = token;
send(msg);
done();
}
});
node.secret,
{algorithm: node.alg, expiresIn: node.exp, keyid: node.jwkkid}, function (err, token) {
if (err) {
done(err);
} else {
msg[node.storetoken] = token;
send(msg);
done();
}
});
} catch (err) {
node.error(err.message);
}
Expand All @@ -78,6 +78,7 @@ module.exports = function (RED) {
this.alg = n.alg;
this.jwkurl = n.jwkurl;
this.secret = n.secret;
this.secb64enc = n.secb64enc;
this.key = n.key;
this.signvar = n.signvar;
this.storetoken = n.storetoken;
Expand All @@ -102,7 +103,7 @@ module.exports = function (RED) {
var authz = msg.req.get('authorization').split(' ');
if(authz.length == 2 && (authz[0] === 'Bearer' || (msg.prefix !== undefined && authz[0] === msg.prefix))){
msg.bearer = authz[1];
}
}
} else if (msg.req.query.access_token !== undefined) {
msg.bearer = msg.req.query.access_token;
} else if (msg.req.body !== undefined && msg.req.body.access_token !== undefined) {
Expand All @@ -123,11 +124,15 @@ module.exports = function (RED) {
//...otherwise use first key in set
key = node.jwk.keys[0];
}

node.alg = header.alg;
node.secret = key.key.toPublicKeyPEM();
}

if (node.secb64enc === "true"){
node.secret = Buffer.from(node.secret, 'base64');
}

jwt.verify(msg[node.signvar], node.secret, {algorithms: node.alg}, function (err, decoded) {
if (err) {
msg['payload'] = err;
Expand Down