Skip to content

Commit

Permalink
Fix OAuth handling through iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuecklers committed Nov 28, 2016
1 parent f79adda commit 36511f8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
40 changes: 31 additions & 9 deletions connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@ function send(event) {
msg.source = event.source;

if (msg.method == 'OAUTH') {
addEventListener("storage", function handle(event) {
if (event.key == 'oauth-response') {
var response = JSON.parse(event.newValue);
receive(msg, response.status, response.headers, response.entity);
localStorage.removeItem('oauth-response');
removeEventListener("storage", handle, false);
}
}, false);
return;
return handleOAuth(msg);
}

var node = msg.method == 'GET' && document.getElementById(msg.path);
Expand Down Expand Up @@ -73,4 +65,34 @@ function getHeaders(node) {
if (token)
headers['baqend-authorization-token'] = token;
return headers;
}

var oAuthHandle, oAuthInterval;
function handleOAuth(msg) {
if (oAuthHandle)
oAuthHandle(409, {}, '{"message": "A new OAuth request was sent."}');

localStorage.removeItem('oauth-response');

var handler = function(event) {
if (event.key == 'oauth-response') {
var response = JSON.parse(event.newValue);
oAuthHandle(response.status, response.headers, response.entity);
}
};

oAuthHandle = function(status, headers, entity) {
receive(msg, status, headers, entity);
localStorage.removeItem('oauth-response');
removeEventListener("storage", handler, false);
clearTimeout(oAuthInterval);
};

addEventListener("storage", handler, false);
oAuthInterval = setInterval(function() {
var item = localStorage.getItem('oauth-response');
if (item) {
handler({key: 'oauth-response', newValue: item})
}
}, 500);
}
21 changes: 16 additions & 5 deletions lib/connector/XMLHttpConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ class XMLHttpConnector extends Connector {
*/
doSend(message, request, receive) {
if (request.method == 'OAUTH') {
addEventListener("storage", function handle(event) {
if (this.oAuthHandle)
this.oAuthHandle({status: 409, entity: '{"message": "A new OAuth request was sent."}'});

localStorage.removeItem('oauth-response');

var handler = (event) => {
if (event.key == 'oauth-response') {
receive(JSON.parse(event.newValue));
localStorage.removeItem('oauth-response');
removeEventListener("storage", handle, false);
this.oAuthHandle(JSON.parse(event.newValue));
}
}, false);
};

this.oAuthHandle = (msg) => {
receive(msg);
localStorage.removeItem('oauth-response');
removeEventListener("storage", handler, false);
};

addEventListener("storage", handler, false);
return;
}

Expand Down

0 comments on commit 36511f8

Please sign in to comment.