v1.2.5: allow some storage configuration for node
This commit is contained in:
parent
df6b77daf5
commit
667fbbab3a
|
@ -38,7 +38,6 @@ OAUTH3._base64.atob = function (base64) {
|
|||
OAUTH3._base64.btoa = function (text) {
|
||||
return new Buffer(text, 'utf8').toString('base64');
|
||||
};
|
||||
OAUTH3._defaultStorage = require('./oauth3.node.storage');
|
||||
|
||||
OAUTH3._node = {};
|
||||
OAUTH3._node.discover = function(providerUri/*, opts*/) {
|
||||
|
@ -107,5 +106,13 @@ OAUTH3._node.randomState = function () {
|
|||
return crypto.randomBytes(16).toString('hex');
|
||||
};
|
||||
OAUTH3.randomState = OAUTH3._node.randomState;
|
||||
OAUTH3._nodeCreate = OAUTH3.create;
|
||||
OAUTH3.create = function (loc, opts) {
|
||||
if (!loc) {
|
||||
loc = {};
|
||||
}
|
||||
OAUTH3._defaultStorage = require('./oauth3.node.storage').create(loc, opts);
|
||||
return OAUTH3._nodeCreate.apply(OAUTH3, arguments);
|
||||
};
|
||||
|
||||
module.exports = OAUTH3;
|
||||
|
|
|
@ -1,137 +1,159 @@
|
|||
'use strict';
|
||||
|
||||
var PromiseA = require('bluebird');
|
||||
var fs = PromiseA.promisifyAll(require('fs'));
|
||||
var PromiseA;
|
||||
try {
|
||||
PromiseA = require('bluebird');
|
||||
} catch(e) {
|
||||
PromiseA = global.Promise;
|
||||
}
|
||||
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');
|
||||
//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');
|
||||
|
||||
// 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, '..'));
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
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);
|
||||
});
|
||||
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');
|
||||
}
|
||||
, 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 () { });
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
var sessionsdir = path.join(oauth3dir, 'sessions');
|
||||
var directivesdir = path.join(oauth3dir, 'directives');
|
||||
var metadir = path.join(oauth3dir, 'meta');
|
||||
|
||||
, 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;
|
||||
}
|
||||
});
|
||||
});
|
||||
// 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, '..'));
|
||||
}
|
||||
, get: function (providerUri, id) {
|
||||
var result;
|
||||
try {
|
||||
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);
|
||||
}
|
||||
|
||||
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')));
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
return PromiseA.resolve(null);
|
||||
}
|
||||
return PromiseA.resolve(result);
|
||||
}
|
||||
, set: function (providerUri, session, id) {
|
||||
var p;
|
||||
|
||||
if (id) {
|
||||
return PromiseA.resolve(require(path.join(sessionsdir, providerUri + '.' + id + '.json')));
|
||||
p = fs.writeFileAsync(path.join(sessionsdir, providerUri + '.' + id + '.json'), JSON.stringify(session, null, 2));
|
||||
}
|
||||
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));
|
||||
}
|
||||
p = fs.writeFileAsync(path.join(sessionsdir, providerUri + '.json'), JSON.stringify(session, null, 2));
|
||||
}
|
||||
} catch(e) {
|
||||
return PromiseA.resolve(null);
|
||||
return p.then(function () {
|
||||
return session;
|
||||
});
|
||||
}
|
||||
return PromiseA.resolve(result);
|
||||
}
|
||||
, set: function (providerUri, session, id) {
|
||||
var p;
|
||||
, clear: function () {
|
||||
return fs.readdirAsync(sessionsdir).then(function (nodes) {
|
||||
return PromiseA.all(nodes.map(function (node) {
|
||||
return fs.unlinkAsync(path.join(sessionsdir, node));
|
||||
}));
|
||||
});
|
||||
}
|
||||
};
|
||||
OAUTH3.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;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (id) {
|
||||
p = fs.writeFileAsync(path.join(sessionsdir, providerUri + '.' + id + '.json'), JSON.stringify(session, null, 2));
|
||||
}
|
||||
else {
|
||||
p = fs.writeFileAsync(path.join(sessionsdir, providerUri + '.json'), JSON.stringify(session, null, 2));
|
||||
}
|
||||
return p.then(function () {
|
||||
return session;
|
||||
});
|
||||
}
|
||||
, clear: function () {
|
||||
return fs.readdirAsync(sessionsdir).then(function (nodes) {
|
||||
return PromiseA.all(nodes.map(function (node) {
|
||||
return fs.unlinkAsync(path.join(sessionsdir, 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;
|
||||
});
|
||||
}
|
||||
return OAUTH3;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "oauth3.js",
|
||||
"version": "1.2.4",
|
||||
"version": "1.2.5",
|
||||
"description": "The world's smallest, fastest, and most secure OAuth3 (and OAuth2) JavaScript implementation.",
|
||||
"main": "oauth3.node.js",
|
||||
"scripts": {
|
||||
|
|
Loading…
Reference in New Issue