Create node-red-contrib-micropub

This commit is contained in:
Peter Stuifzand 2019-02-06 15:14:39 +01:00
commit b28708192b
5 changed files with 216 additions and 0 deletions

48
indieauth.html Normal file
View File

@ -0,0 +1,48 @@
<script type="text/javascript">
RED.nodes.registerType('indieauth', {
category: 'config',
defaults: {
client_id: {value: ""},
me: {value: ""}
},
credentials: {
micropub_endpoint: {value: "", required: false},
token_endpoint: {value: "", required: false},
auth_token: {value: "", required: false}
},
label: function () {
return this.me || 'indieauth';
},
exportable: false,
oneditprepare: function () {
var id = this.id;
$('#node-config-start-auth').mousedown(function () {
var callback = 'https://localhost:1881/indieauth/auth/callback';
var clientId = $("#node-config-input-client_id").val();
var me = $("#node-config-input-me").val();
var url = 'indieauth/auth?id=' + id + '&client_id=' + clientId + "&me=" + me + "&callback=" + encodeURIComponent(callback);
$(this).attr("href", url);
});
}
})
</script>
<script type="text/x-red" data-template-name="indieauth">
<div class="form-row">
<label for="node-config-input-client_id"><i class="icon-bookmark"></i> Client ID</label>
<input type="text" id="node-config-input-client_id">
</div>
<div class="form-row">
<label for="node-config-input-me"><i class="icon-bookmark"></i> me</label>
<input type="text" id="node-config-input-me">
</div>
<div class="form-row">
<label>&nbsp;</label>
<a class="btn" id="node-config-start-auth" href="#" target="_blank"><span>Web Sign In</span></a>
</div>
</script>

80
indieauth.js Normal file
View File

@ -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);
});
});
};

47
micropub-create.html Normal file
View File

@ -0,0 +1,47 @@
<script type="text/javascript">
(function () {
RED.nodes.registerType('micropub-create', {
category: 'output',
color: '#a6bbcf',
defaults: {
name: {value: ""},
endpoint: {value: "", type: "indieauth"}
},
inputs: 1,
outputs: 1,
icon: "bridge.png",
label: function () {
return this.name || "micropub";
}
})
})();
</script>
<script type="text/x-red" data-template-name="micropub-create">
<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-endpoint"><i class="icon-tag"></i> Endpoint</label>
<input type="text" id="node-input-endpoint" placeholder="Endpoint">
</div>
</script>
<script type="text/x-red" data-help-name="micropub-create">
<p>Posts to Micropub endpoints</p>
<p>Connect to your Micropub powered blog and post messages.</p>
<p>Input Parameters:</p>
<p><code>msg.type</code> - Default: 'json'
<p><code>msg.payload</code> - Should be an Micropub object using MF2.</p>
<p><code>msg.payload.type</code> - Should be an array with the type.</p>
<p><code>msg.payload.properties</code> - Should be on object with MF2 properties.</p>
<p><code>msg.payload.properties.name</code> - Should be an array with a string.</p>
<p><code>msg.payload.properties.content.html</code> - Should be a string.</p>
<p><code>msg.payload.properties.content.value</code> - Should be a string.</p>
<p>Output Values:</p>
<p>This message will be passed on unchanged, but the <code>location</code> of the post will be added.</p>
<p><code>msg.location</code> - When succesful this will contain the location of this post.</p>
</script>

28
micropub-create.js Normal file
View File

@ -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);
}

13
package.json Normal file
View File

@ -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"
}
}
}