2017-03-20 23:55:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-08-03 21:05:30 +00:00
|
|
|
var PromiseA;
|
|
|
|
try {
|
|
|
|
PromiseA = require('bluebird');
|
|
|
|
} catch(e) {
|
|
|
|
PromiseA = global.Promise;
|
|
|
|
}
|
|
|
|
var promisify = PromiseA.promisify || require('util').promisify;
|
|
|
|
|
2017-03-20 23:55:27 +00:00
|
|
|
//var OAUTH3 = require('./oauth3.core.js').OAUTH3;
|
|
|
|
var OAUTH3 = require('./oauth3.issuer.js').OAUTH3;
|
|
|
|
// used for OAUTH3.urls.resourcePasswordOwner
|
|
|
|
// used for OAUTH3.authn.loginMeta
|
|
|
|
// used for OAUTH3.authn.otp
|
|
|
|
// used for OAUTH3.authn.resourcePasswordOwner
|
2018-08-03 21:05:30 +00:00
|
|
|
|
|
|
|
var requestAsync = promisify(require('@coolaj86/urequest'));
|
2017-03-20 23:55:27 +00:00
|
|
|
var crypto = require('crypto');
|
|
|
|
|
2017-12-01 22:11:06 +00:00
|
|
|
OAUTH3.crypto = OAUTH3.crypto || {};
|
|
|
|
OAUTH3.crypto.core = require('./oauth3.node.crypto.js');
|
2017-03-21 05:29:03 +00:00
|
|
|
OAUTH3.PromiseA = PromiseA;
|
2017-03-20 23:55:27 +00:00
|
|
|
OAUTH3._discoverHelper = function(providerUri, opts) {
|
2017-03-21 05:29:03 +00:00
|
|
|
return OAUTH3._node.discover(providerUri, opts);
|
2017-03-20 23:55:27 +00:00
|
|
|
};
|
|
|
|
OAUTH3._requestHelper = function (preq, opts) {
|
|
|
|
/*
|
|
|
|
if (opts && opts.directives) {
|
|
|
|
preq.url = OAUTH3.url.resolve(opts.directives.issuer, preq.url);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return OAUTH3._node.request(preq, opts);
|
|
|
|
};
|
2017-03-21 07:02:41 +00:00
|
|
|
OAUTH3._base64.atob = function (base64) {
|
|
|
|
return new Buffer(base64, 'base64').toString('utf8');
|
|
|
|
};
|
|
|
|
OAUTH3._base64.btoa = function (text) {
|
|
|
|
return new Buffer(text, 'utf8').toString('base64');
|
|
|
|
};
|
2017-07-31 22:42:22 +00:00
|
|
|
OAUTH3._defaultStorage = require('./oauth3.node.storage');
|
2017-03-21 07:02:41 +00:00
|
|
|
|
2017-03-20 23:55:27 +00:00
|
|
|
OAUTH3._node = {};
|
|
|
|
OAUTH3._node.discover = function(providerUri/*, opts*/) {
|
|
|
|
return OAUTH3.request({
|
|
|
|
method: 'GET'
|
|
|
|
, url: OAUTH3.url.normalize(providerUri) + '/.well-known/oauth3/directives.json'
|
|
|
|
}).then(function (resp) {
|
|
|
|
return resp.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
OAUTH3._node.request = function(preq/*, _sys*/) {
|
|
|
|
var data = {
|
|
|
|
method: preq.method
|
|
|
|
, url: preq.url || preq.uri
|
|
|
|
, headers: preq.headers
|
2017-10-25 20:12:41 +00:00
|
|
|
, timeout: preq.timeout || undefined
|
2017-03-20 23:55:27 +00:00
|
|
|
, json: preq.data || preq.body || preq.json || undefined // TODO which to use?
|
|
|
|
, formData: preq.formData || undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
//console.log('DEBUG request');
|
|
|
|
//console.log(preq.url || preq.uri);
|
|
|
|
//console.log(data.json);
|
|
|
|
|
|
|
|
return requestAsync(data).then(OAUTH3._node._parseJson);
|
|
|
|
};
|
|
|
|
OAUTH3._node._parseJson = function (resp) {
|
|
|
|
var err;
|
|
|
|
var json = resp.body;
|
|
|
|
|
|
|
|
// TODO toCamelCase
|
|
|
|
if (!(resp.statusCode >= 200 && resp.statusCode < 400)) {
|
|
|
|
err = new Error("bad response code: " + resp.statusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
//console.log('resp.body', typeof resp.body);
|
|
|
|
if ('string' === typeof json) {
|
|
|
|
try {
|
|
|
|
json = JSON.parse(json);
|
|
|
|
} catch(e) {
|
2017-09-28 19:50:52 +00:00
|
|
|
err = err || (new Error('response not parsable: ' + resp.body));
|
2017-03-20 23:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle both Oauth2- and node-style errors
|
2017-09-28 19:50:52 +00:00
|
|
|
if (json && json.error) {
|
|
|
|
err = new Error(json.error.message || json.error_description || JSON.stringify(json.error));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
2017-03-20 23:55:27 +00:00
|
|
|
err.result = json;
|
|
|
|
return PromiseA.reject(err);
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:02:41 +00:00
|
|
|
return {
|
|
|
|
headers: resp.headers
|
|
|
|
, status: resp.statusCode
|
|
|
|
, data: json
|
|
|
|
};
|
2017-03-20 23:55:27 +00:00
|
|
|
};
|
2017-03-21 07:02:41 +00:00
|
|
|
OAUTH3._logoutHelper = function(/*directives, opts*/) {
|
2017-03-21 05:29:03 +00:00
|
|
|
// TODO allow prompting of which account
|
|
|
|
return OAUTH3.PromiseA.reject(new Error("logout not yet implemented for node.js"));
|
2017-03-20 23:55:27 +00:00
|
|
|
};
|
|
|
|
OAUTH3._node.randomState = function () {
|
|
|
|
return crypto.randomBytes(16).toString('hex');
|
|
|
|
};
|
|
|
|
OAUTH3.randomState = OAUTH3._node.randomState;
|
2017-03-21 05:29:03 +00:00
|
|
|
|
|
|
|
module.exports = OAUTH3;
|