Merge branch 'v1.2' into merge

This commit is contained in:
AJ ONeal 2017-11-09 15:25:57 -07:00
commit 523f57944f
4 changed files with 35 additions and 15 deletions

2
.ignore Normal file
View File

@ -0,0 +1,2 @@
prefactor
.well-known

View File

@ -535,7 +535,7 @@
var providerUri = oldSession.provider_uri; var providerUri = oldSession.provider_uri;
var clientUri = oldSession.client_uri; var clientUri = oldSession.client_uri;
Object.keys(['access_token', 'token', 'client_uri', 'refresh', 'refresh_token', 'expires_in', 'provider_uri', 'scope', 'token_type']).forEach(function (key) { ['access_token', 'token', 'client_uri', 'refresh', 'refresh_token', 'expires_in', 'provider_uri', 'scope', 'token_type'].forEach(function (key) {
oldSession[key] = undefined; oldSession[key] = undefined;
}); });
Object.keys(newSession).forEach(function (key) { Object.keys(newSession).forEach(function (key) {
@ -821,7 +821,7 @@
var logoutReq = OAUTH3.urls.logout( var logoutReq = OAUTH3.urls.logout(
directives directives
, { client_id: (opts.client_id || opts.client_uri || OAUTH3.clientUri(OAUTH3._browser.window.location)) , { client_id: (opts.client_id || opts.client_uri || OAUTH3.clientUri(OAUTH3._browser.window.location))
, windowType: 'popup' // we'll figure out background later , windowType: 'popup' // TODO: figure out background later
, broker: opts.broker , broker: opts.broker
//, state: opts._state //, state: opts._state
, debug: opts.debug , debug: opts.debug
@ -932,15 +932,14 @@
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
} }
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
var data;
if (xhr.readyState !== XMLHttpRequest.DONE) { if (xhr.readyState !== XMLHttpRequest.DONE) {
// nothing to do here // nothing to do here
return; return;
} }
var data, err;
if (xhr.status !== 200) { if (xhr.status !== 200) {
reject(new Error('bad status code: ' + xhr.status)); err = new Error('bad status code: ' + xhr.status);
return;
} }
try { try {
@ -949,6 +948,18 @@
data = xhr.responseText; data = xhr.responseText;
} }
if (data.error) {
err = new Error(data.error.message || data.error_description || JSON.stringify(data.error));
Object.assign(err, data.error);
}
if (err) {
err._request = xhr;
err.status = xhr.status;
err.data = data;
reject(err);
return;
}
resolve({ resolve({
_request: xhr _request: xhr
, headers: null // TODO , headers: null // TODO
@ -956,6 +967,11 @@
, status: xhr.status , status: xhr.status
}); });
}; };
xhr.ontimeout = function () {
var err = new Error('ETIMEDOUT');
err.code = 'ETIMEDOUT';
reject(err);
};
if (preq.progress) { if (preq.progress) {
xhr.upload.onprogress = function (ev) { xhr.upload.onprogress = function (ev) {
@ -973,6 +989,9 @@
// For assets.example.com/assets // For assets.example.com/assets
xhr.withCredentials = true; xhr.withCredentials = true;
if (preq.timeout) {
xhr.timeout = preq.timeout;
}
if (preq.data) { if (preq.data) {
headers['Content-Type'] = 'application/json'; // TODO XXX TODO utf8 headers['Content-Type'] = 'application/json'; // TODO XXX TODO utf8
} }
@ -1306,7 +1325,7 @@
} }
} }
} }
, _initClient: function (location/*, opts*/) { , _initClient: function () {
var me = this; var me = this;
return OAUTH3.discover(me._clientUri, { client_id: me._clientUri }).then(function (clientDirectives) { return OAUTH3.discover(me._clientUri, { client_id: me._clientUri }).then(function (clientDirectives) {
me._clientDirectives = clientDirectives; me._clientDirectives = clientDirectives;

View File

@ -44,6 +44,7 @@ OAUTH3._node.request = function(preq/*, _sys*/) {
method: preq.method method: preq.method
, url: preq.url || preq.uri , url: preq.url || preq.uri
, headers: preq.headers , headers: preq.headers
, timeout: preq.timeout || undefined
, json: preq.data || preq.body || preq.json || undefined // TODO which to use? , json: preq.data || preq.body || preq.json || undefined // TODO which to use?
, formData: preq.formData || undefined , formData: preq.formData || undefined
}; };
@ -60,10 +61,7 @@ OAUTH3._node._parseJson = function (resp) {
// TODO toCamelCase // TODO toCamelCase
if (!(resp.statusCode >= 200 && resp.statusCode < 400)) { if (!(resp.statusCode >= 200 && resp.statusCode < 400)) {
// console.log('[A3] DEBUG', resp.body);
err = new Error("bad response code: " + resp.statusCode); err = new Error("bad response code: " + resp.statusCode);
err.result = resp.body;
return PromiseA.reject(err);
} }
//console.log('resp.body', typeof resp.body); //console.log('resp.body', typeof resp.body);
@ -71,15 +69,16 @@ OAUTH3._node._parseJson = function (resp) {
try { try {
json = JSON.parse(json); json = JSON.parse(json);
} catch(e) { } catch(e) {
err = new Error('response not parsable:' + resp.body); err = err || (new Error('response not parsable: ' + resp.body));
err.result = resp.body;
return PromiseA.reject(err);
} }
} }
// handle both Oauth2- and node-style errors // handle both Oauth2- and node-style errors
if (json.error) { if (json && json.error) {
err = new Error(json.error && json.error.message || json.error_description || json.error); err = new Error(json.error.message || json.error_description || JSON.stringify(json.error));
}
if (err) {
err.result = json; err.result = json;
return PromiseA.reject(err); return PromiseA.reject(err);
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "oauth3.js", "name": "oauth3.js",
"version": "1.0.10", "version": "1.2.2",
"description": "The world's smallest, fastest, and most secure OAuth3 (and OAuth2) JavaScript implementation.", "description": "The world's smallest, fastest, and most secure OAuth3 (and OAuth2) JavaScript implementation.",
"main": "oauth3.node.js", "main": "oauth3.node.js",
"scripts": { "scripts": {