added auth node to verify/decode idToken JWT sent from clients

This commit is contained in:
Peter Svensson
2020-08-09 14:03:15 +02:00
parent 79cbbddfe1
commit 05ccc64d14
7 changed files with 2123 additions and 6 deletions

View File

@ -194,9 +194,22 @@ input:
}
}
output: An array of headers returned by the operation if all went well. These are mostly useless but at least a confirmation of success.
output: An array of headers returned by the operation if all went well. These are mostly useless but at least a confirmation of success. Dcoumentation and problem-solving hints can be found here; (https://firebase.google.com/docs/auth/admin/verify-id-tokens). Note that you need to explicitly get the correct id token in the client and send it to the back-end.
# Auth nodes
TBD
## verify-idtoken
Decrypts a firebase client SDK JWT idToken into a user object.
input:
{
"payload": "eyJhbGciOiJSUz....fMrAUdK"
}
output:
{
"payload": { "name": "Foo Foobarson", "picture": "https://...", ..}
}

56
auth/verify-idtoken.html Normal file
View File

@ -0,0 +1,56 @@
<script type="text/javascript">
RED.nodes.registerType('verify-idtoken',{
category: 'firebase-admin',
color: '#a6bbcf',
defaults: {
name: {value:""},
cred: {value: "", type: 'firebase-config'}
},
inputs:1,
outputs:1,
icon: "firebase-auth.png",
label: function() {
return this.name||"verify-idtoken";
},
oneditsave: function() {
let type = $('#apitype-select').val()
console.log('type is set to '+type)
},
oneditprepare: function() {
$('#apitype-select').change(function () {
$("#node-input-apitype").val($(this).find('option:selected').val())
});
$("#apitype-select").val($("#node-input-apitype").val())
$('#apitype-select').trigger('change');
}
});
</script>
<script type="text/x-red" data-template-name="verify-idtoken">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-cred"><i class="icon-tag"></i> Credentials</label>
<input type="text" id="node-input-cred" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="verify-idtoken">
<p>A node that wraps the verify-idtoken API from the firebase-admin SDK</p>
Reoslved a given JWT token in payload as a user object for the current project (that the configured credentials is pointing to)
<p>
input: {"payload": "......JWT......"}
<p>
output: The decoded User object for the given JWT idToken
</script>

41
auth/verify-idtoken.js Normal file
View File

@ -0,0 +1,41 @@
// "eyJhbGciOiJSUzI1NiIsImtpZCI6Ijc0NGY2MGU5ZmI1MTVhMmEwMWMxMWViZWIyMjg3MTI4NjA1NDA3MTEiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXpwIjoiOTczNDA4MDM4MzUwLWI2aGNoN3NvamliNmkxNjZtN3BtOWtmcDNvaHJoMzRrLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiOTczNDA4MDM4MzUwLWI2aGNoN3NvamliNmkxNjZtN3BtOWtmcDNvaHJoMzRrLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTA5ODI2MDk5NjU1NTU2Mjk4ODI0IiwiZW1haWwiOiJwc3ZlbnNzb25AZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiJmVkpiSWRzMFFuVlBWbm50VUtoSmFnIiwiaWF0IjoxNTk2OTY1OTMxLCJleHAiOjE1OTY5Njk1MzF9.Rq8bsj-RlcMdVjk2UuJrUykyJcRcaTRohQD788lCigIVxLedxIJ5rZRndRVzSTUmX2n_sgCdtiFLN2w16KHy5v2053fNXvlyEuR4t1fctt_LU-OygLGW4qeSPEWonu7zBmhy2aaJBHnx0NzqRbCYewm0zVrI_BEgju-OvJAlH5pud9Ycs7o7d6NaJsJp0JW3bEcz0bgETA2jWlYIR3ZeQSRxYcjwTZ2OhbocMEWYVFVoTF8n0PVlFe21cZs3RfA2KWfuVvwvrB2BGku6rkVANoT8UJKkhLP81NipgPzIowui72O5pbCCm5senj5D_VMh74NmptHSLAKTGHv36EzzPA"
module.exports = function (RED) {
function FirebaseAdmin(config) {
RED.nodes.createNode(this, config);
var node = this;
if (config.cred) {
let c = RED.nodes.getNode(config.cred)
this.admin = c.admin
}
node.on('input', function (msg) {
if (msg && msg.payload) {
console.log('verify-idtoken got input')
console.dir(msg)
const idtoken = msg.payload.idtoken
try {
this.admin.auth().verifyIdToken(idtoken)
.then(function (decodedToken) {
console.log('verify-idtoken got result:')
console.dir(decodedToken)
this.status({ fill: "green", shape: "ring", text: '' });
node.send({ payload: decodedToken })
}.bind(this)).catch(function (error) {
console.log('verify-idtoken caught an exception!')
console.dir(error)
this.status({ fill: "red", shape: "ring", text: error.errorInfo.message });
}.bind(this));
} catch (ex) {
this.status({ fill: "red", shape: "ring", text: ex.code });
}
}
}.bind(this));
}
RED.nodes.registerType("verify-idtoken", FirebaseAdmin);
}

View File

@ -20,7 +20,7 @@
</div>
<div class="form-row">
<label for="node-config-input-cred"><i class="icon-bookmark"></i> Firebase Service Account Credentials</label>
<input type="text" id="node-config-input-cred">
<input type="textarea" rows="10" id="node-config-input-cred">
</div>
<div class="form-row">
<label for="node-config-input-dburl"><i class="icon-bookmark"></i> Firebase Database URL</label>

BIN
icons/firebase-auth.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

2005
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "node-red-contrib-firebase-admin",
"version": "1.1.23",
"version": "1.1.24",
"description": "A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.",
"main": "index.js",
"scripts": {
@ -16,7 +16,8 @@
"node-red",
"firebase",
"firestore",
"cloud storage"
"cloud storage",
"auth"
],
"bugs": {
"url": "https://github.com/psvensson/node-red-contrib-firebase-admin/issues"
@ -40,7 +41,8 @@
"storage-read": "storage/storage-read.js",
"storage-write": "storage/storage-write.js",
"storage-list": "storage/storage-list.js",
"storage-delete": "storage/storage-delete.js"
"storage-delete": "storage/storage-delete.js",
"verify-idtoken": "auth/verify-idtoken.js"
}
},
"dependencies": {