From b28708192b311c0b37cd55c6aa4fc458c4d0ae73 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Wed, 6 Feb 2019 15:14:39 +0100 Subject: [PATCH] Create node-red-contrib-micropub --- indieauth.html | 48 ++++++++++++++++++++++++++ indieauth.js | 80 ++++++++++++++++++++++++++++++++++++++++++++ micropub-create.html | 47 ++++++++++++++++++++++++++ micropub-create.js | 28 ++++++++++++++++ package.json | 13 +++++++ 5 files changed, 216 insertions(+) create mode 100644 indieauth.html create mode 100644 indieauth.js create mode 100644 micropub-create.html create mode 100644 micropub-create.js create mode 100644 package.json diff --git a/indieauth.html b/indieauth.html new file mode 100644 index 0000000..fd8ba49 --- /dev/null +++ b/indieauth.html @@ -0,0 +1,48 @@ + + + diff --git a/indieauth.js b/indieauth.js new file mode 100644 index 0000000..49ce644 --- /dev/null +++ b/indieauth.js @@ -0,0 +1,80 @@ +var Micropub = require('micropub-helper'); + +module.exports = function (RED) { + function IndieauthNode(n) { + RED.nodes.createNode(this, n); + this.client_id = n.client_id; + this.micropub_endpoint = n.micropub_endpoint; + this.token_endpoint = n.token_endpoint; + this.authorization_endpoint = n.authorization_endpoint; + this.auth_token = n.auth_token; + this.me = n.me; + } + + RED.nodes.registerType("indieauth", IndieauthNode); + + RED.httpAdmin.get('/indieauth/auth', function (req, res) { + var node_id = req.query.id; + + const micropub = new Micropub({ + clientId: req.query.client_id, + redirectUri: 'http://localhost:1881/indieauth/auth/callback', + me: req.query.me, + state: node_id + ':1234' + }); + + var credentials = { + client_id: req.query.client_id, + me: req.query.me + }; + + micropub.getAuthUrl(req.query.me).then(url => { + credentials.micropub_endpoint = micropub.options.micropubEndpoint; + credentials.token_endpoint = micropub.options.tokenEndpoint; + res.redirect(url); + RED.nodes.addCredentials(node_id, credentials); + }).catch(err => { + RED.log.error(err); + res.send(err); + }); + }); + + RED.httpAdmin.get('/indieauth/auth/callback', function (req, res) { + RED.log.info(req.query.state); + RED.log.info(req.query.code); + + var code = req.query.code; + var state = req.query.state.split(':'); + + if (!code || !state) { + return; + } + + var node_id = state[0]; + + var credentials = RED.nodes.getCredentials(node_id); + if (!credentials) { + return; + } + + const micropub = new Micropub({ + clientId: credentials.client_id, + redirectUri: 'http://localhost:1881/indieauth/auth/callback', + tokenEndpoint: credentials.token_endpoint, + state: req.query.state, + me: credentials.me, + }); + + micropub + .getToken(code) + .then(token => { + credentials.auth_token = token; + RED.nodes.addCredentials(node_id, credentials); + res.send('auth completed'); + }).catch(err => { + RED.log.error(err); + res.send(err); + }); + }); +}; + diff --git a/micropub-create.html b/micropub-create.html new file mode 100644 index 0000000..59abb68 --- /dev/null +++ b/micropub-create.html @@ -0,0 +1,47 @@ + + + + + diff --git a/micropub-create.js b/micropub-create.js new file mode 100644 index 0000000..85983f7 --- /dev/null +++ b/micropub-create.js @@ -0,0 +1,28 @@ +var Micropub = require('micropub-helper'); + +module.exports = function (RED) { + function MicropubNode(config) { + RED.nodes.createNode(this, config); + + this.endpoint = RED.nodes.getNode(config.endpoint); + + var node = this; + + node.on('input', function (msg) { + try { + const micropub = new Micropub({ + micropubEndpoint: node.endpoint.credentials.micropub_endpoint, + token: node.endpoint.credentials.auth_token + }); + var location = micropub.postMicropub(msg.payload); + msg.location = location; + node.send(msg); + } catch (e) { + node.error(e); + } + }); + } + + RED.nodes.registerType("micropub-create", MicropubNode); +} + diff --git a/package.json b/package.json new file mode 100644 index 0000000..028a78a --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name" : "node-red-contrib-micropub", + "version": "0.1.0", + "dependencies": { + "micropub-helper": "1.5.3" + }, + "node-red" : { + "nodes": { + "indieauth": "indieauth.js", + "micropub-create": "micropub-create.js" + } + } +}