playing around with node.js

This commit is contained in:
AJ ONeal 2017-03-20 23:29:03 -06:00
parent 1008d0f6a3
commit afb021af9b
4 changed files with 74 additions and 30 deletions

43
bin/oauth3.js Normal file
View File

@ -0,0 +1,43 @@
'use strict';
// process.stdout.isTTY
var form = require('terminal-forms.js').create(process.stdin, process.stdout);
var OAUTH3 = require('../oauth3.node.js');
OAUTH3.hooks.directives._get = require('../oauth3.node.storage.js').directives._get;
OAUTH3.hooks.directives._set = require('../oauth3.node.storage.js').directives._set;
var url = require('url');
console.log('stdin tty', process.stdin.isTTY);
console.log('stdout tty', process.stdout.isTTY);
form.ask({ label: "What's your OAuth3 Provider URL? ", type: 'url' }).then(function (urlResult) {
var urlObj = url.parse(urlResult.input);
// TODO get unique client id for bootstrapping app
var oauth3 = OAUTH3.create(urlObj);
var providerPromise = oauth3.setProvider(urlObj.host + urlObj.pathname);
function getCurrentUserEmail() {
return form.ask({ label: "What's your email (or cloud mail) address? ", type: 'email' }).then(function (emailResult) {
// TODO lookup uuid locally before performing loginMeta
// TODO lookup token locally before performing loginMeta / otp
return providerPromise.then(function () {
return OAUTH3.authn.loginMeta(oauth3._providerDirectives, { email: emailResult.input }).then(function (result) {
// TODO require hashcash to create user account
console.log(result);
return form.PromiseA.reject(new Error("not implemented"));
});
});
});
}
getCurrentUserEmail().then(function (email) {
// TODO skip if token exists locally
return OAUTH3.authn.otp(oauth3._providerDirectives, { email: email }).then(function (otpResult) {
return form.ask({
label: "What's your login code?"
, help: "(it was sent to '" + urlResult.input + "' and looks like 1234-5678-9012)"
}).then(function () {
console.log(otpResult);
});
});
});
});

View File

@ -4,7 +4,7 @@
var OAUTH3 = exports.OAUTH3 = {
clientUri: function (location) {
return OAUTH3.uri.normalize(location.host + location.pathname);
return OAUTH3.uri.normalize(location.host + (location.pathname || ''));
}
, error: {
parse: function (providerUri, params) {
@ -1024,6 +1024,7 @@
}
if (this._providerUri) {
p = OAUTH3.discover(this._providerUri, { client_id: this._clientUri }).then(function (/*directives*/) {
console.error("BUG: there's some jquery in oauth3.core.js that needs to be removed and tested");
$('.js-signin').removeAttr('disabled');
});
}

View File

@ -10,8 +10,9 @@ var PromiseA = require('bluebird');
var requestAsync = PromiseA.promisify(require('request'));
var crypto = require('crypto');
OAUTH3.PromiseA = PromiseA;
OAUTH3._discoverHelper = function(providerUri, opts) {
return OAUTH3._browser.discover(providerUri, opts);
return OAUTH3._node.discover(providerUri, opts);
};
OAUTH3._requestHelper = function (preq, opts) {
/*
@ -75,38 +76,16 @@ OAUTH3._node._parseJson = function (resp) {
return PromiseA.reject(err);
}
return json;
resp.data = json;
return resp;
};
OAUTH3._logoutHelper = function(directives, opts) {
var logoutReq = OAUTH3.urls.logout(
directives
, { client_id: (opts.client_id || opts.client_uri || OAUTH3.clientUri(OAUTH3._browser.window.location))
, windowType: 'popup' // we'll figure out background later
, broker: opts.broker
//, state: opts._state
, debug: opts.debug
}
);
return OAUTH3._browser.frameRequest(
OAUTH3.url.resolve(directives.issuer, logoutReq.url)
, logoutReq.state // state should recycle params
, { windowType: 'popup'
, reuseWindow: opts.broker && '-broker'
, debug: opts.debug
}
).then(function (params) {
OAUTH3._browser.closeFrame(params.state || opts._state, opts);
if (params.error) {
// TODO directives.audience
return OAUTH3.PromiseA.reject(OAUTH3.error.parse(directives.issuer /*providerUri*/, params));
}
return params;
});
// TODO allow prompting of which account
return OAUTH3.PromiseA.reject(new Error("logout not yet implemented for node.js"));
};
OAUTH3._node.randomState = function () {
return crypto.randomBytes(16).toString('hex');
};
OAUTH3.randomState = OAUTH3._node.randomState;
module.exports = OAUTH3;

21
oauth3.node.storage.js Normal file
View File

@ -0,0 +1,21 @@
'use strict';
var fs = require('fs');
var path = require('path');
module.exports = {
directives: {
_get: function (providerUri) {
// TODO make safe
try {
return require(path.join(process.cwd(), providerUri + '.directives.json'));
} catch(e) {
return null;
}
}
, _set: function (providerUri, directives) {
fs.writeFileSync(path.join(process.cwd(), providerUri + '.directives.json'), JSON.stringify(directives, null, 2));
return directives;
}
}
};