fix for OAuth OPTIONS firing on XHR 302

This commit is contained in:
Stephen Birarda 2017-06-28 14:13:09 -07:00
parent 5617860fe4
commit cf92144748
2 changed files with 47 additions and 24 deletions

View file

@ -2,11 +2,11 @@ $(document).ready(function(){
// setup the underscore templates // setup the underscore templates
var nodeTemplate = _.template($('#nodes-template').html()); var nodeTemplate = _.template($('#nodes-template').html());
var queuedTemplate = _.template($('#queued-template').html()); var queuedTemplate = _.template($('#queued-template').html());
// setup a function to grab the assignments // setup a function to grab the assignments
function getNodesAndAssignments() { function getNodesAndAssignments() {
$.getJSON("nodes.json", function(json){ $.getJSON("nodes.json", function(json){
json.nodes.sort(function(a, b){ json.nodes.sort(function(a, b){
if (a.type === b.type) { if (a.type === b.type) {
if (a.uptime < b.uptime) { if (a.uptime < b.uptime) {
@ -16,36 +16,50 @@ $(document).ready(function(){
} else { } else {
return 0; return 0;
} }
} }
if (a.type === "agent" && b.type !== "agent") { if (a.type === "agent" && b.type !== "agent") {
return 1; return 1;
} else if (b.type === "agent" && a.type !== "agent") { } else if (b.type === "agent" && a.type !== "agent") {
return -1; return -1;
} }
if (a.type > b.type) { if (a.type > b.type) {
return 1; return 1;
} }
if (a.type < b.type) { if (a.type < b.type) {
return -1; return -1;
} }
}); });
$('#nodes-table tbody').html(nodeTemplate(json)); $('#nodes-table tbody').html(nodeTemplate(json));
}).fail(function(jqXHR, textStatus, errorThrown) {
// we assume a 401 means the DS has restarted
// and no longer has our OAuth produced uuid
// so just reload and re-auth
if (jqXHR.status == 401) {
location.reload();
}
}); });
$.getJSON("assignments.json", function(json){ $.getJSON("assignments.json", function(json){
$('#assignments-table tbody').html(queuedTemplate(json)); $('#assignments-table tbody').html(queuedTemplate(json));
}).fail(function(jqXHR, textStatus, errorThrown) {
// we assume a 401 means the DS has restarted
// and no longer has our OAuth produced uuid
// so just reload and re-auth
if (jqXHR.status == 401) {
location.reload();
}
}); });
} }
// do the first GET on page load // do the first GET on page load
getNodesAndAssignments(); getNodesAndAssignments();
// grab the new assignments JSON every two seconds // grab the new assignments JSON every two seconds
var getNodesAndAssignmentsInterval = setInterval(getNodesAndAssignments, 2000); var getNodesAndAssignmentsInterval = setInterval(getNodesAndAssignments, 2000);
// hook the node delete to the X button // hook the node delete to the X button
$(document.body).on('click', '.glyphicon-remove', function(){ $(document.body).on('click', '.glyphicon-remove', function(){
// fire off a delete for this node // fire off a delete for this node
@ -57,10 +71,10 @@ $(document).ready(function(){
} }
}); });
}); });
$(document.body).on('click', '#kill-all-btn', function() { $(document.body).on('click', '#kill-all-btn', function() {
var confirmed_kill = confirm("Are you sure?"); var confirmed_kill = confirm("Are you sure?");
if (confirmed_kill == true) { if (confirmed_kill == true) {
$.ajax({ $.ajax({
url: "/nodes/", url: "/nodes/",

View file

@ -2091,22 +2091,31 @@ bool DomainServer::isAuthenticatedRequest(HTTPConnection* connection, const QUrl
// the user does not have allowed username or role, return 401 // the user does not have allowed username or role, return 401
return false; return false;
} else { } else {
// re-direct this user to OAuth page static const QByteArray REQUESTED_WITH_HEADER = "X-Requested-With";
static const QString XML_REQUESTED_WITH = "XMLHttpRequest";
// generate a random state UUID to use if (connection->requestHeaders().value(REQUESTED_WITH_HEADER) == XML_REQUESTED_WITH) {
QUuid stateUUID = QUuid::createUuid(); // unauthorized XHR requests get a 401 and not a 302, since there isn't an XHR
// path to OAuth authorize
connection->respond(HTTPConnection::StatusCode401, UNAUTHENTICATED_BODY);
} else {
// re-direct this user to OAuth page
// add it to the set so we can handle the callback from the OAuth provider // generate a random state UUID to use
_webAuthenticationStateSet.insert(stateUUID); QUuid stateUUID = QUuid::createUuid();
QUrl authURL = oauthAuthorizationURL(stateUUID); // add it to the set so we can handle the callback from the OAuth provider
_webAuthenticationStateSet.insert(stateUUID);
Headers redirectHeaders; QUrl authURL = oauthAuthorizationURL(stateUUID);
redirectHeaders.insert("Location", authURL.toEncoded()); Headers redirectHeaders;
connection->respond(HTTPConnection::StatusCode302, redirectHeaders.insert("Location", authURL.toEncoded());
QByteArray(), HTTPConnection::DefaultContentType, redirectHeaders);
connection->respond(HTTPConnection::StatusCode302,
QByteArray(), HTTPConnection::DefaultContentType, redirectHeaders);
}
// we don't know about this user yet, so they are not yet authenticated // we don't know about this user yet, so they are not yet authenticated
return false; return false;