fleet/frontend/kolide/base.js
John Murphy 019191c36b SSO Login and Configuration Support (#1506)
Closes issue #1456 This PR adds a single sign on option to the login form, exposes single sign on to the end user, and allows an admin user to set single sign on configuration options.
2017-05-17 10:58:40 -05:00

59 lines
1.5 KiB
JavaScript

import local from 'utilities/local';
import Request from 'kolide/request';
class Base {
constructor () {
const { origin } = global.window.location;
this.baseURL = `${origin}/api`;
this.bearerToken = local.getItem('auth_token');
}
static post (endpoint, body = {}, headers = {}) {
const { POST } = Request.REQUEST_METHODS;
return Base._request(POST, endpoint, body, headers);
}
static get (endpoint, overrideHeaders = {}) {
const { GET } = Request.REQUEST_METHODS;
return Base._request(GET, endpoint, {}, overrideHeaders);
}
static _deleteRequest (endpoint, headers) {
const { DELETE: method } = Request.REQUEST_METHODS;
const request = new Request({ endpoint, method, headers });
return request.send();
}
static _request (method, endpoint, body, headers) {
const { GET } = Request.REQUEST_METHODS;
const requestAttrs = method === GET
? { endpoint, method, headers }
: { endpoint, method, body, headers };
const request = new Request(requestAttrs);
return request.send();
}
_authenticatedHeaders = (headers) => {
return {
...headers,
Authorization: `Bearer ${this.bearerToken}`,
};
}
_authenticatedRequest(method, endpoint, body, overrideHeaders) {
const headers = this._authenticatedHeaders(overrideHeaders);
return Base._request(method, endpoint, body, headers);
}
_endpoint (pathname) {
return this.baseURL + pathname;
}
}
export default Base;