oauth3.js/oauth3.node.storage.js

135 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-03-21 05:29:03 +00:00
'use strict';
2017-03-26 07:43:06 +00:00
var PromiseA = require('bluebird');
var fs = PromiseA.promisifyAll(require('fs'));
2017-03-21 05:29:03 +00:00
var path = require('path');
2017-03-26 07:43:06 +00:00
//var oauth3dir = process.cwd();
var oauth3dir = path.join(require('os').homedir(), '.oauth3', 'v1');
var sessionsdir = path.join(oauth3dir, 'sessions');
var directivesdir = path.join(oauth3dir, 'directives');
var metadir = path.join(oauth3dir, 'meta');
if (!fs.existsSync(oauth3dir)) {
fs.mkdirSync(oauth3dir);
}
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
module.exports = {
directives: {
2017-03-26 07:43:06 +00:00
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) {
2017-03-21 05:29:03 +00:00
// TODO make safe
try {
2017-03-26 07:43:06 +00:00
return require(path.join(directivesdir, providerUri + '.json'));
2017-03-21 05:29:03 +00:00
} catch(e) {
return null;
}
}
, set: function (providerUri, directives) {
2017-03-26 07:43:06 +00:00
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 () { });
}));
});
2017-03-21 05:29:03 +00:00
}
}
2017-03-21 07:02:41 +00:00
, sessions: {
2017-03-26 07:43:06 +00:00
all: function (providerUri) {
var dirname = path.join(oauth3dir, 'sessions');
return fs.readdirAsync(dirname).then(function (nodes) {
return nodes.map(function (node) {
var result = require(path.join(dirname, node));
if (result.link) {
return null;
}
}).filter(Boolean).filter(function (result) {
if (!providerUri || providerUri === result.issuer) {
return result;
}
});
});
}
, get: function (providerUri, id) {
var result;
2017-03-21 07:02:41 +00:00
try {
if (id) {
2017-03-26 07:43:06 +00:00
return PromiseA.resolve(require(path.join(sessionsdir, providerUri + '.' + id + '.json')));
}
else {
2017-03-26 07:43:06 +00:00
result = require(path.join(sessionsdir, providerUri + '.json'));
// TODO make safer
if (result.link && '/' !== result.link[0] && !/\.\./.test(result.link)) {
result = require(path.join(oauth3dir, 'sessions', result.link));
}
}
2017-03-21 07:02:41 +00:00
} catch(e) {
2017-03-26 07:43:06 +00:00
return PromiseA.resolve(null);
2017-03-21 07:02:41 +00:00
}
2017-03-26 07:43:06 +00:00
return PromiseA.resolve(result);
2017-03-21 07:02:41 +00:00
}
, set: function (providerUri, session, id) {
2017-03-26 07:43:06 +00:00
var p;
if (id) {
2017-03-26 07:43:06 +00:00
p = fs.writeFileAsync(path.join(sessionsdir, providerUri + '.' + id + '.json'), JSON.stringify(session, null, 2));
}
else {
2017-03-26 07:43:06 +00:00
p = fs.writeFileAsync(path.join(sessionsdir, providerUri + '.json'), JSON.stringify(session, null, 2));
}
2017-03-26 07:43:06 +00:00
return p.then(function () {
return session;
});
}
, clear: function () {
var dirname = path.join(oauth3dir, 'sessions');
return fs.readdirAsync(dirname).then(function (nodes) {
return PromiseA.all(nodes.map(function (node) {
return fs.unlinkAsync(path.join(dirname, node));
}));
});
}
}
, meta: {
get: function (key) {
// TODO make safe
try {
return PromiseA.resolve(require(path.join(metadir, key + '.json')));
} catch(e) {
return PromiseA.resolve(null);
}
}
, set: function (key, value) {
return fs.writeFileAsync(path.join(metadir, key + '.json'), JSON.stringify(value, null, 2)).then(function () {
return value;
});
2017-03-21 07:02:41 +00:00
}
}
2017-03-21 05:29:03 +00:00
};