Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWT Reworking for Kibana API requests #63

Merged
merged 7 commits into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ define(function (require) {
require('elasticsearch');
require('angular-route');
require('angular-bindonce');

require('restangular');

var configFile = JSON.parse(require('text!config'));
Expand Down Expand Up @@ -49,16 +49,34 @@ define(function (require) {
'Pragma': 'no-cache',
'Expires': 0
});
var fullUrl = window.location;
var searchUrl = window.location.search;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this and above line

var hrefUrl = window.location.href;

// Look for 'token=', then capture all characters
// after (non-greedy) until either end of substring
// or the next ampersand.
Copy link

@KjellKod KjellKod Sep 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great regexp comment!

var jwtPattern = /jwt=(.*?)(&|$)/i;
var jwt = String(hrefUrl).match(jwtPattern);
localStorage.setItem('token', jwt[1]);
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
// TODO: standardized error handling - right now we soft fail errors to empty data

// Redirect API login failure response to login page
if (data.status !== 'success' && data.message && data.message.match(/you must be logged in/gi)) {
location.href = '/login.php';
}

return data && data.status === 'success' ? data.data : {error: true, message: data.message};
});
RestangularProvider.addFullRequestInterceptor(function() {
const config = { headers: {} };
const token = localStorage.getItem('token');
if (token) {
config.headers['Authorization'] = token;
}
return config;
});
})
.config(['ngClipProvider', function (ngClipProvider) {
ngClipProvider.setPath('bower_components/zeroclipboard/dist/ZeroClipboard.swf');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
define(function (require) {

var _ = require('lodash');
var app = require('modules').get('netmon/download');
app.factory('fileDownloader', function(Restangular, $timeout) {
return {
deDupeFiles : function(fileList) {

var countOccurences = function(fileList) {
var dupes = false;
var itemFreq = {};
Expand All @@ -20,7 +20,7 @@ define(function (require) {
if (dupes) return itemFreq;
return false;
};

var generateNamesForDupes = function(fileList) {
var dupes = countOccurences(fileList);
if (dupes){
Expand All @@ -45,11 +45,11 @@ define(function (require) {
}
return fileList;
};

return generateNamesForDupes(fileList);
},


formatFileNameForModal : function(fileName) {
var lengthToTruncate = 35;
if (fileName.length > lengthToTruncate){
Expand All @@ -58,23 +58,23 @@ define(function (require) {
return fileName;
},
get: function(settings, type, sessionID){


var self = this;
var iframe = null;
var downloadID = null;
self.route = type + '/';
self.type = type;
self.postData = {file : settings.fileList};

settings.checkInterval = 500;
settings.fileUrl = '/data/api/' + self.route + '?action=download&downloadID=';

// sessionID needed for File Recon only (NOT pcap recon)
if (sessionID) {
self.postData.sessionID = sessionID;
}

var cancelDownload = function(downloadID) {
Restangular
.one(self.route)
Expand All @@ -83,12 +83,12 @@ define(function (require) {

settings.failCallback(
{
header: 'Canceled',
header: 'Canceled',
body: 'Download canceled.',
class: 'modal-header alert alert-warning'
}, 'Canceled');
};


var getiframeDocument = function(iframe) {
var iframeDoc = iframe.contentWindow || iframe.contentDocument;
Expand Down Expand Up @@ -127,7 +127,7 @@ define(function (require) {
header: 'Failure',
body: 'Download interrupted.',
class: 'modal-header alert alert-danger'
}, 'failure');
}, 'failure');

}
statusList = JSON.parse(statusList);
Expand All @@ -141,8 +141,8 @@ define(function (require) {
header: 'Failure',
body: 'One or more of the downloads failed.',
class: 'modal-header alert alert-danger'
}, bulkStatus.bulkStatus);
}
}, bulkStatus.bulkStatus);
}
iframe.contentWindow.stop();
iframe.parentNode.removeChild(iframe);
} else {
Expand All @@ -167,10 +167,15 @@ define(function (require) {
header: 'Failure',
body: res.data.message,
class: 'alert alert-danger'
}, 'Error');
}, 'Error');
} else {
downloadID = res.downloadID;
settings.fileUrl += downloadID;
const jwt = localStorage.getItem('token');
// If this fails, pcap download will be rejected.
if (jwt) {
settings.fileUrl += "&jwt=" + jwt;
}
settings.prepareCallback(settings.fileUrl);
//create a temporary iframe that is used to request the fileUrl as a GET request
iframe = document.createElement('iframe');
Expand Down