2017-03-21 05:29:03 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-08-04 09:58:44 +00:00
|
|
|
var PromiseA;
|
|
|
|
try {
|
|
|
|
PromiseA = require('bluebird');
|
|
|
|
} catch(e) {
|
|
|
|
PromiseA = global.Promise;
|
2017-03-26 07:43:06 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
var promisify = PromiseA.promisify || require('util').promisify;
|
|
|
|
var fs = {
|
|
|
|
existsSync: require('fs').existsSync
|
|
|
|
, mkdirSync: require('fs').mkdirSync
|
|
|
|
, readdirAsync: promisify(require('fs').readdir)
|
|
|
|
, writeFileAsync: promisify(require('fs').writeFile)
|
|
|
|
, unlinkAsync: promisify(require('fs').unlink)
|
|
|
|
};
|
|
|
|
var path = require('path');
|
2017-03-21 05:29:03 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2018-08-04 09:58:44 +00:00
|
|
|
create: function (loc/*, opts*/) {
|
|
|
|
var oauth3dir;
|
|
|
|
if (!loc.pathname) {
|
|
|
|
loc.pathname = require('os').hostname();
|
|
|
|
oauth3dir = path.join(loc.pathname, '.config/oauth3.org');
|
|
|
|
} else {
|
|
|
|
oauth3dir = path.join(loc.pathname, 'oauth3.org', 'v1');
|
2017-03-26 07:43:06 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
var sessionsdir = path.join(oauth3dir, 'sessions');
|
|
|
|
var directivesdir = path.join(oauth3dir, 'directives');
|
|
|
|
var metadir = path.join(oauth3dir, 'meta');
|
|
|
|
|
|
|
|
// We can reasonably assume the existence of the home directory, but we can't assume
|
|
|
|
// that there will already be a `.oauth3` directory or anything inside of it.
|
|
|
|
if (!fs.existsSync(path.join(oauth3dir, '..'))) {
|
|
|
|
fs.mkdirSync(path.join(oauth3dir, '..'));
|
2017-03-21 05:29:03 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
if (!fs.existsSync(oauth3dir)) {
|
|
|
|
fs.mkdirSync(oauth3dir);
|
2017-03-26 07:43:06 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
if (!fs.existsSync(directivesdir)) {
|
|
|
|
fs.mkdirSync(directivesdir);
|
|
|
|
}
|
|
|
|
if (!fs.existsSync(sessionsdir)) {
|
|
|
|
fs.mkdirSync(sessionsdir);
|
|
|
|
}
|
|
|
|
if (!fs.existsSync(metadir)) {
|
|
|
|
fs.mkdirSync(metadir);
|
2017-03-21 05:29:03 +00:00
|
|
|
}
|
2017-03-21 07:02:41 +00:00
|
|
|
|
2018-08-04 09:58:44 +00:00
|
|
|
var OAUTH3 = {};
|
|
|
|
OAUTH3.directives = {
|
|
|
|
all: function () {
|
|
|
|
return fs.readdirAsync(directivesdir).then(function (nodes) {
|
|
|
|
return nodes.map(function (node) {
|
|
|
|
try {
|
|
|
|
return require(path.join(directivesdir, node));
|
|
|
|
} catch(e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}).filter(Boolean);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
, get: function (providerUri) {
|
|
|
|
// TODO make safe
|
|
|
|
try {
|
|
|
|
return require(path.join(directivesdir, providerUri + '.json'));
|
|
|
|
} catch(e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
, set: function (providerUri, directives) {
|
|
|
|
return fs.writeFileAsync(
|
|
|
|
path.join(directivesdir, providerUri + '.json')
|
|
|
|
, JSON.stringify(directives, null, 2)
|
|
|
|
).then(function () {
|
|
|
|
return directives;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
, clear: function () {
|
|
|
|
return fs.readdirAsync(directivesdir).then(function (nodes) {
|
|
|
|
return PromiseA.all(nodes.map(function (node) {
|
|
|
|
return fs.unlinkAsync(path.join(directivesdir, node)).then(function () { }, function () { });
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
OAUTH3.sessions = {
|
|
|
|
all: function (providerUri) {
|
|
|
|
return fs.readdirAsync(sessionsdir).then(function (nodes) {
|
|
|
|
return nodes.map(function (node) {
|
|
|
|
var result = require(path.join(sessionsdir, node));
|
|
|
|
if (result.link) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}).filter(Boolean).filter(function (result) {
|
|
|
|
if (!providerUri || providerUri === result.issuer) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
, get: function (providerUri, id) {
|
|
|
|
var result;
|
|
|
|
try {
|
|
|
|
if (id) {
|
|
|
|
return PromiseA.resolve(require(path.join(sessionsdir, providerUri + '.' + id + '.json')));
|
2017-03-26 07:43:06 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
else {
|
|
|
|
result = require(path.join(sessionsdir, providerUri + '.json'));
|
|
|
|
// TODO make safer
|
|
|
|
if (result.link && '/' !== result.link[0] && !/\.\./.test(result.link)) {
|
|
|
|
result = require(path.join(sessionsdir, result.link));
|
|
|
|
}
|
2017-03-26 07:43:06 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
} catch(e) {
|
|
|
|
return PromiseA.resolve(null);
|
|
|
|
}
|
|
|
|
return PromiseA.resolve(result);
|
|
|
|
}
|
|
|
|
, set: function (providerUri, session, id) {
|
|
|
|
var p;
|
|
|
|
|
2017-03-22 15:48:04 +00:00
|
|
|
if (id) {
|
2018-08-04 09:58:44 +00:00
|
|
|
p = fs.writeFileAsync(path.join(sessionsdir, providerUri + '.' + id + '.json'), JSON.stringify(session, null, 2));
|
2017-03-22 15:48:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-08-04 09:58:44 +00:00
|
|
|
p = fs.writeFileAsync(path.join(sessionsdir, providerUri + '.json'), JSON.stringify(session, null, 2));
|
2017-03-22 15:48:04 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
return p.then(function () {
|
|
|
|
return session;
|
|
|
|
});
|
2017-03-21 07:02:41 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
, clear: function () {
|
|
|
|
return fs.readdirAsync(sessionsdir).then(function (nodes) {
|
|
|
|
return PromiseA.all(nodes.map(function (node) {
|
|
|
|
return fs.unlinkAsync(path.join(sessionsdir, node));
|
|
|
|
}));
|
|
|
|
});
|
2017-03-22 15:48:04 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
};
|
|
|
|
OAUTH3.meta = {
|
|
|
|
get: function (key) {
|
|
|
|
// TODO make safe
|
|
|
|
try {
|
|
|
|
return PromiseA.resolve(require(path.join(metadir, key + '.json')));
|
|
|
|
} catch(e) {
|
|
|
|
return PromiseA.resolve(null);
|
|
|
|
}
|
2017-03-22 15:48:04 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
, set: function (key, value) {
|
|
|
|
return fs.writeFileAsync(path.join(metadir, key + '.json'), JSON.stringify(value, null, 2)).then(function () {
|
|
|
|
return value;
|
|
|
|
});
|
2017-03-26 07:43:06 +00:00
|
|
|
}
|
2018-08-04 09:58:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return OAUTH3;
|
2017-03-21 07:02:41 +00:00
|
|
|
}
|
2017-03-21 05:29:03 +00:00
|
|
|
};
|