Переглянути джерело

Merge branch 'sessions3' into 'master'

Sessions3

See merge request shoesandsocks/notifier!5
master
Rich Brown 1 рік тому
джерело
коміт
096c67bc6d
6 змінених файлів з 352 додано та 97 видалено
  1. +204
    -20
      package-lock.json
  2. +2
    -1
      package.json
  3. +28
    -24
      routes/authRouter.js
  4. +26
    -46
      routes/createRouter.js
  5. +50
    -3
      src/funcs.js
  6. +42
    -3
      src/index.js

+ 204
- 20
package-lock.json Переглянути файл

@@ -211,6 +211,18 @@
"resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz",
"integrity": "sha1-HjRA6RXwsSA9I3SOeO3XubW0PlY="
},
"archetype": {
"version": "0.8.8",
"resolved": "https://registry.npmjs.org/archetype/-/archetype-0.8.8.tgz",
"integrity": "sha512-isdIbFfT3zXVan34hmxIwI8A5/8lo9MaYmwXF1iYWCnJS1GvKKnZ4GrXoOUgKdUMCiB/wdguRXeStCUQhFjexg==",
"requires": {
"lodash.clonedeep": "4.x",
"lodash.set": "4.x",
"lodash.unset": "4.x",
"mpath": "0.5.1",
"standard-error": "1.1.0"
}
},
"argparse": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
@@ -373,12 +385,19 @@
"integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ=="
},
"axios": {
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
"version": "0.19.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.0.tgz",
"integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==",
"requires": {
"follow-redirects": "^1.3.0",
"is-buffer": "^1.1.5"
"follow-redirects": "1.5.10",
"is-buffer": "^2.0.2"
},
"dependencies": {
"is-buffer": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz",
"integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw=="
}
}
},
"axobject-query": {
@@ -1338,6 +1357,11 @@
"electron-to-chromium": "^1.3.47"
}
},
"bson": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.1.tgz",
"integrity": "sha512-jCGVYLoYMHDkOsbwJZBCqwMHyH4c+wzgI9hG7Z6SZJRXWr+x58pdIbm2i9a/jFGCkRJqRUr8eoI7lDWa0hTkxg=="
},
"buffer": {
"version": "4.9.1",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz",
@@ -1670,6 +1694,15 @@
"xdg-basedir": "^3.0.0"
}
},
"connect-mongodb-session": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/connect-mongodb-session/-/connect-mongodb-session-2.1.1.tgz",
"integrity": "sha512-k8NF+C32tJZuR3sSFfdz56e1NKzxejWlD/X5PVYBIZQ8/dzqkMGTKxcdGXsBreU5M48WZ+vYGrJSruSRTzFu4Q==",
"requires": {
"archetype": "0.8.x",
"mongodb": "~3.1.8"
}
},
"console-browserify": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz",
@@ -3051,11 +3084,26 @@
"dev": true
},
"follow-redirects": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.7.0.tgz",
"integrity": "sha512-m/pZQy4Gj287eNy94nivy5wchN3Kp+Q5WgUPNy5lJSZ3sgkVKSYV/ZChMAQVIgx1SqfZ2zBZtPA2YlXIWxxJOQ==",
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
"requires": {
"debug": "^3.2.6"
"debug": "=3.1.0"
},
"dependencies": {
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
}
}
},
"for-in": {
@@ -3129,7 +3177,8 @@
},
"ansi-regex": {
"version": "2.1.1",
"bundled": true
"bundled": true,
"optional": true
},
"aproba": {
"version": "1.2.0",
@@ -3147,11 +3196,13 @@
},
"balanced-match": {
"version": "1.0.0",
"bundled": true
"bundled": true,
"optional": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@@ -3164,15 +3215,18 @@
},
"code-point-at": {
"version": "1.1.0",
"bundled": true
"bundled": true,
"optional": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true
"bundled": true,
"optional": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true
"bundled": true,
"optional": true
},
"core-util-is": {
"version": "1.0.2",
@@ -3275,7 +3329,8 @@
},
"inherits": {
"version": "2.0.3",
"bundled": true
"bundled": true,
"optional": true
},
"ini": {
"version": "1.3.5",
@@ -3285,6 +3340,7 @@
"is-fullwidth-code-point": {
"version": "1.0.0",
"bundled": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@@ -3297,17 +3353,20 @@
"minimatch": {
"version": "3.0.4",
"bundled": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
},
"minimist": {
"version": "0.0.8",
"bundled": true
"bundled": true,
"optional": true
},
"minipass": {
"version": "2.3.5",
"bundled": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
@@ -3324,6 +3383,7 @@
"mkdirp": {
"version": "0.5.1",
"bundled": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@@ -3396,7 +3456,8 @@
},
"number-is-nan": {
"version": "1.0.1",
"bundled": true
"bundled": true,
"optional": true
},
"object-assign": {
"version": "4.1.1",
@@ -3406,6 +3467,7 @@
"once": {
"version": "1.4.0",
"bundled": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@@ -3481,7 +3543,8 @@
},
"safe-buffer": {
"version": "5.1.2",
"bundled": true
"bundled": true,
"optional": true
},
"safer-buffer": {
"version": "2.1.2",
@@ -3511,6 +3574,7 @@
"string-width": {
"version": "1.0.2",
"bundled": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@@ -3528,6 +3592,7 @@
"strip-ansi": {
"version": "3.0.1",
"bundled": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@@ -3566,11 +3631,13 @@
},
"wrappy": {
"version": "1.0.2",
"bundled": true
"bundled": true,
"optional": true
},
"yallist": {
"version": "3.0.3",
"bundled": true
"bundled": true,
"optional": true
}
}
},
@@ -4444,11 +4511,26 @@
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz",
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg=="
},
"lodash.clonedeep": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz",
"integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8="
},
"lodash.set": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz",
"integrity": "sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM="
},
"lodash.sortby": {
"version": "4.7.0",
"resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz",
"integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg="
},
"lodash.unset": {
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/lodash.unset/-/lodash.unset-4.5.2.tgz",
"integrity": "sha1-Nw0dPoW3Kn4bDN8tJyEhMG8j5O0="
},
"longest": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz",
@@ -4529,6 +4611,12 @@
"readable-stream": "^2.0.1"
}
},
"memory-pager": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
"optional": true
},
"merge-descriptors": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
@@ -4568,6 +4656,22 @@
"form-data": "^2.3.2",
"qs": "^6.5.2",
"rel-parser": "^1.0.2"
},
"dependencies": {
"axios": {
"version": "0.18.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.18.1.tgz",
"integrity": "sha512-0BfJq4NSfQXd+SkFdrvFbG7addhYSBA2mQwISr46pD6E5iqkWg02RAs8vyTT/j0RTnoYmeXauBuSv1qKwR179g==",
"requires": {
"follow-redirects": "1.5.10",
"is-buffer": "^2.0.2"
}
},
"is-buffer": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz",
"integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw=="
}
}
},
"miller-rabin": {
@@ -4652,6 +4756,31 @@
"minimist": "0.0.8"
}
},
"mongodb": {
"version": "3.1.13",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.13.tgz",
"integrity": "sha512-sz2dhvBZQWf3LRNDhbd30KHVzdjZx9IKC0L+kSZ/gzYquCF5zPOgGqRz6sSCqYZtKP2ekB4nfLxhGtzGHnIKxA==",
"requires": {
"mongodb-core": "3.1.11",
"safe-buffer": "^5.1.2"
}
},
"mongodb-core": {
"version": "3.1.11",
"resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.11.tgz",
"integrity": "sha512-rD2US2s5qk/ckbiiGFHeu+yKYDXdJ1G87F6CG3YdaZpzdOm5zpoAZd/EKbPmFO6cQZ+XVXBXBJ660sSI0gc6qg==",
"requires": {
"bson": "^1.1.0",
"require_optional": "^1.0.1",
"safe-buffer": "^5.1.2",
"saslprep": "^1.0.0"
}
},
"mpath": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/mpath/-/mpath-0.5.1.tgz",
"integrity": "sha512-H8OVQ+QEz82sch4wbODFOz+3YQ61FYz/z3eJ5pIdbMEaUzDqA268Wd+Vt4Paw9TJfvDgVKaayC0gBzMIw2jhsg=="
},
"ms": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
@@ -5493,6 +5622,22 @@
"axios": "^0.18.0",
"jsdom": "^11.12.0",
"li": "^1.3.0"
},
"dependencies": {
"axios": {
"version": "0.18.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.18.1.tgz",
"integrity": "sha512-0BfJq4NSfQXd+SkFdrvFbG7addhYSBA2mQwISr46pD6E5iqkWg02RAs8vyTT/j0RTnoYmeXauBuSv1qKwR179g==",
"requires": {
"follow-redirects": "1.5.10",
"is-buffer": "^2.0.2"
}
},
"is-buffer": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz",
"integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw=="
}
}
},
"remove-trailing-separator": {
@@ -5589,6 +5734,22 @@
"resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz",
"integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE="
},
"require_optional": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz",
"integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==",
"requires": {
"resolve-from": "^2.0.0",
"semver": "^5.1.0"
},
"dependencies": {
"resolve-from": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz",
"integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c="
}
}
},
"resolve": {
"version": "1.10.1",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.10.1.tgz",
@@ -5685,6 +5846,15 @@
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"saslprep": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz",
"integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==",
"optional": true,
"requires": {
"sparse-bitfield": "^3.0.3"
}
},
"sax": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
@@ -5984,6 +6154,15 @@
"resolved": "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz",
"integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM="
},
"sparse-bitfield": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
"integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=",
"optional": true,
"requires": {
"memory-pager": "^1.0.2"
}
},
"spdx-correct": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.0.tgz",
@@ -6047,6 +6226,11 @@
"resolved": "https://registry.npmjs.org/stackframe/-/stackframe-1.0.4.tgz",
"integrity": "sha512-to7oADIniaYwS3MhtCa/sQhrxidCCQiF/qp4/m5iN3ipf0Y7Xlri0f6eG29r08aL7JYl8n32AF3Q5GYBZ7K8vw=="
},
"standard-error": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/standard-error/-/standard-error-1.1.0.tgz",
"integrity": "sha1-I+UWj6HAggGJ5YEnAaeQWFENDTQ="
},
"static-extend": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/static-extend/-/static-extend-0.1.2.tgz",


+ 2
- 1
package.json Переглянути файл

@@ -13,9 +13,10 @@
"license": "ISC",
"dependencies": {
"atob": "^2.1.2",
"axios": "^0.18.0",
"axios": "^0.19.0",
"backpack-core": "^0.7.0",
"compression": "^1.7.3",
"connect-mongodb-session": "^2.1.1",
"cors": "^2.8.4",
"dotenv": "^6.0.0",
"express": "^4.16.3",


+ 28
- 24
routes/authRouter.js Переглянути файл

@@ -1,37 +1,41 @@
import express from 'express';
import Micropub from 'micropub-helper';

import { authUser, useCode } from '../src/funcs';

const authRouter = express.Router();

authRouter.get('/callback', (req, res) => {
const { code } = req.query;
// also me and state available here
return req.app.locals.mp
.getToken(code)
.then((token) => {
if (!token) {
res.status(401).json({ err: 'Token fail on callback', msg: null });
} else {
req.app.locals.token = token;
res.json({ err: null, msg: 'Token auth successful' });
}
})
.catch((erro) => {
console.log(erro);
res.status(400).json({ err: 'Token superfail on callback', msg: null });
});
const {
clientId, redirectUri, me, state, token, tokenEndpoint,
} = req.session;
const sessionInfoAboutUser = {
clientId, redirectUri, me, state, token, tokenEndpoint,
};
return useCode(sessionInfoAboutUser, code, req, res);
});

authRouter.post('/', (req, res) => {
const newMicropub = new Micropub({ ...req.body });
req.app.locals.mp = newMicropub;
return req.app.locals.mp
.getAuthUrl()
.then(url => res.json({ error: null, url }))
.catch(() => {
// console.log(err);
res.status(400).json({ error: 'Nope.', url: null });
});
const {
clientId, redirectUri, me, state,
} = req.body;
req.session.clientId = clientId;
req.session.redirectUri = redirectUri;
req.session.me = me;
req.session.state = state;
req.session.save();
const sessionInfoAboutUser = {
clientId, redirectUri, me, state,
};
return authUser(sessionInfoAboutUser, req, res);
});

authRouter.get('/cookie', (req, res) => {
const { me } = req.session;
return me
? res.json({ error: null, me })
: res.json({ error: 'no user', me: null });
});

export default authRouter;

+ 26
- 46
routes/createRouter.js Переглянути файл

@@ -4,15 +4,15 @@ import multer from 'multer';
import fs from 'fs';
import path from 'path';

import { create } from '../src/funcs';

const storage = multer.diskStorage({
destination(req, file, cb) {
cb(null, 'uploads/');
},
filename(req, file, cb) {
const { originalname } = file;
const fiver = Math.random()
.toString(36)
.substr(2, 5);
const fiver = Math.random().toString(36).substr(2, 5);
const ext = path.extname(originalname);
const rootName = originalname.replace(ext, '');
const newFilename = `${rootName}-${fiver}${ext}`;
@@ -27,41 +27,28 @@ const sanitizedFile = (file) => {
const fileExts = ['png', 'jpg', 'jpeg', 'gif'];
const isAllowedExt = fileExts.includes(file.originalname.split('.')[1].toLowerCase());
const isAllowedMimeType = file.mimetype.startsWith('image/');
return isAllowedExt && isAllowedMimeType;
return (isAllowedExt && isAllowedMimeType);
};
// this route should really be called /image
createRouter.post('/form', upload.single('file'), (req, res) => {
const {
file,
body: {
title, body, arrayOfTags, altText,
},
} = req;
const { file, body: { title, body, arrayOfTags } } = req;
if (!sanitizedFile(file)) {
return res.status(403).json({ error: 'not an allowed filetype', url: null });
}
// formData smooshed array of Tags into a string. de-smoosh:
const tags = arrayOfTags.split(',');
// sort of a jokey thing here
if (tags.includes('do not post')) {
return res.status(422).json({ error: 'Unprocessed. -do not post- found in tags', url: null });
}
return req.app.locals.mp
.create(
{
h: 'entry',
name: title,
content: body,
category: tags,
altText,
photo: [fs.createReadStream(file.path)],
},
'multipart',
)
.then(url => res.json({ error: null, url }))
.catch((err) => {
console.log(err);
res.status(400).json({ error: 'micropub-helper failed; multipart', url: null });
});
const createArray = {
h: 'entry',
name: title,
content: body,
category: tags,
photo: [fs.createReadStream(file.path)],
};
const sessionInfoAboutUser = { ...req.session };
return create(sessionInfoAboutUser, createArray, 'multipart', res);
});

createRouter.post('/', (req, res) => {
@@ -74,28 +61,21 @@ createRouter.post('/', (req, res) => {
if (!tags || tags === []) {
tags = ['micro.blog'];
}
// sort of a jokey thing here
if (tags.includes('do not post')) {
return res.status(422).json({ error: 'Unprocessed. -do not post- found in tags', url: null });
}
const sessionInfoAboutUser = { ...req.session };
if (title === '') {
return req.app.locals.mp
.create({ h: 'entry', content: text, category: tags }, 'form') // TODO: test categories/tags
.then(url => res.json({ error: null, url }))
.catch(() => res.status(400).json({ error: 'micropub-helper failed; form', url: null }));
const createArray = { h: 'entry', content: text, category: tags };
return create(sessionInfoAboutUser, createArray, 'form', res);
}
return req.app.locals.mp
.create(
{
h: 'entry',
name: title, // i thought MF syntax was 'p-name' but voxpelli's formattr turns NAME to TITLE
content: text,
category: tags,
},
'multipart',
)
.then(url => res.json({ error: null, url }))
.catch(() => res.status(400).json({ error: 'micropub-helper failed; multipart', url: null }));
const createArray = {
h: 'entry',
name: title,
content: text,
category: tags,
};
return create(sessionInfoAboutUser, createArray, 'multipart', res);
});

export default createRouter;

+ 50
- 3
src/funcs.js Переглянути файл

@@ -1,8 +1,9 @@
import atob from 'atob';
import Micropub from 'micropub-helper';

// eslint-disable-next-line import/prefer-default-export
export const isMicropubTokenAuthed = (req, res, next) => {
const { token } = req.app.locals;
const { token } = req.session;
if (!token) return res.status(401).json({ error: "no token, foo'" });

const base64Url = token.split('.')[1];
@@ -15,8 +16,54 @@ export const isMicropubTokenAuthed = (req, res, next) => {

// eslint-disable-next-line camelcase
if (now - twoHours > issued_at) {
req.app.locals.token = null;
req.session.token = null;
// TODO: destroy session?
return res.status(401).json({ error: "old token, foo'" });
}
return req.app.locals.token ? next() : res.status(403).json({ error: 'NAUGHTY' });
return req.session.token ? next() : res.status(403).json({ error: 'NAUGHTY' });
};

// mpOptions to create a new micropub, post options go into its create func
export const create = (mpOptions, postOptions, type, res) => {
// console.log(mpOptions, '<== options sent to create func');
const mp = new Micropub({ ...mpOptions });
return mp
.create(postOptions, type)
.then(url => res.json({ error: null, url }))
.catch(() => res.status(400).json({ error: 'micropub-helper failed', url: null }));
};

export const authUser = (mpOptions, req, res) => {
const mp = new Micropub({ ...mpOptions });
return mp
.getAuthUrl()
.then((url) => {
// console.log(mp.options, '<== picked up options at authUser func');
req.session.tokenEndpoint = mp.options.tokenEndpoint;
req.session.micropubEndpoint = mp.options.micropubEndpoint;
req.session.save();
return res.json({ error: null, url });
})
.catch(() => {
// console.log(err);
res.status(400).json({ error: 'Nope.', url: null });
});
};

export const useCode = (mpOptions, code, req, res) => {
const mp = new Micropub({ ...mpOptions });
return mp
.getToken(code)
.then((token) => {
if (!token) {
res.status(401).json({ err: 'Token fail on callback', msg: null });
} else {
req.session.token = token;
res.json({ err: null, msg: 'Token auth successful' });
}
})
.catch(() => {
// console.log(erro);
res.status(400).json({ err: 'Token superfail on callback', msg: null });
});
};

+ 42
- 3
src/index.js Переглянути файл

@@ -1,6 +1,8 @@
import express from 'express';
import cors from 'cors';
import compression from 'compression';
// import compression from 'compression';
import session from 'express-session';
import CMS from 'connect-mongodb-session';

import { blogRouter } from '../routes/blog-router';
import { genericRouter } from '../routes/generic-notify';
@@ -13,8 +15,45 @@ import { isMicropubTokenAuthed } from './funcs';
const port = 8086;
const app = express();

app.use(compression());
app.use(cors());
const MongoDBStore = CMS(session);
const store = new MongoDBStore({
uri: `mongodb+srv://colors_admin:${process.env.MONGO_PASSWORD}@cluster0-4udef.mongodb.net/test?retryWrites=true&w=majority`,
databaseName: 'notifierDb',
collection: 'sessions',
}, (error) => {
if (error) console.log('MongoDBStore connection error: ', error);
});

// Catch errors
store.on('error', (error) => {
console.log('store error:', error);
});

app.set('trust proxy', 1); // trust first proxy

const routesArray = ['/create', '/auth'];

app.use(routesArray, session({
secret: process.env.MONGO_PASSWORD, // dumb
store,
resave: true,
saveUninitialized: true,
cookie: { secure: false, maxAge: 1000 * 60 * 60 * 3, sameSite: false },
}));

app.use((req, res, next) => {
console.log(req.session, '<- session string');
next();
});

// app.use(compression());

const corsArrays = ['https://post.porknachos.com', 'http://localhost:3000', 'https://dashboard.porknachos.com'];
const corsOptions = {
origin: corsArrays,
credentials: true,
};
app.use(cors(corsOptions));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());



Завантаження…
Відмінити
Зберегти