From afb021af9b45e68931ea325a3ffd1afa12de89fd Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 20 Mar 2017 23:29:03 -0600 Subject: [PATCH] playing around with node.js --- bin/oauth3.js | 43 ++++++++++++++++++++++++++++++++++++++++++ oauth3.core.js | 3 ++- oauth3.node.js | 37 ++++++++---------------------------- oauth3.node.storage.js | 21 +++++++++++++++++++++ 4 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 bin/oauth3.js create mode 100644 oauth3.node.storage.js diff --git a/bin/oauth3.js b/bin/oauth3.js new file mode 100644 index 0000000..b7bbe8f --- /dev/null +++ b/bin/oauth3.js @@ -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); + }); + }); + }); +}); diff --git a/oauth3.core.js b/oauth3.core.js index 2295f9e..d9fbaf3 100644 --- a/oauth3.core.js +++ b/oauth3.core.js @@ -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'); }); } diff --git a/oauth3.node.js b/oauth3.node.js index 156cd5e..fff8733 100644 --- a/oauth3.node.js +++ b/oauth3.node.js @@ -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; diff --git a/oauth3.node.storage.js b/oauth3.node.storage.js new file mode 100644 index 0000000..1e0b81a --- /dev/null +++ b/oauth3.node.storage.js @@ -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; + } + } +};