Skip to content

Commit

Permalink
feat: add basic authentication option when the token is not provided …
Browse files Browse the repository at this point in the history
…in user config (#92)

fixes #89
  • Loading branch information
tremes authored and lholmquist committed Nov 29, 2018
1 parent e154ef6 commit f5778b9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,31 @@ You can pass in a custom config object by using the `settings` object
});
});

If you don't have an OpenShift access token, you can use basic authentication by providing username and password as follows:

const openshiftRestClient = require('openshift-rest-client');
const settings = {};

const customConfig = {
apiVersion: 'v1',
context:
{ cluster: '192-168-99-100:8443',
namespace: 'for-node-client-testing',
user: 'developer/192-168-99-100:8443' },
user: {
username: yourUsername,
password: youPassword
},
cluster: 'https://192.168.99.100:8443' }
};

settings.config = customConfig;

openshiftRestClient(settings).then((client) => {
// Use the client object to find a list of projects, for example
client.projects.findAll().then((projects) => {
console.log(projects);
});
});


63 changes: 53 additions & 10 deletions lib/common-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,29 @@ const privates = require('./private-map');
const request = require('request');

module.exports = function doRequest (client, options) {
const clientConfig = privates.get(client).config;
if (clientConfig.user.token !== undefined) {
return doRequestWithToken(client, options, clientConfig.user.token);
} else {
return doRequestWithBasicAuth(client, options).then((accessToken) => {
return doRequestWithToken(client, options, accessToken);
});
}
};

const buildError = (body) => {
const err = new Error(body.message);
err.statusCode = body.code;
return err;
};

function doRequestWithToken (client, options = {}, userToken) {
return new Promise((resolve, reject) => {
options = options || {};
const requestSettings = (client.settings && client.settings.request) ? client.settings.request : {};
const clientConfig = privates.get(client).config;

const baseOptions = {
auth: {
bearer: clientConfig.user.token || '',
bearer: userToken || '',
ca: clientConfig.user.ca
},
json: true
Expand All @@ -45,12 +60,6 @@ module.exports = function doRequest (client, options) {
// request settings will be request specific stuff that is overriden during the initial creation of the client
const req = Object.assign({}, baseOptions, options, requestSettings);

const buildError = (body) => {
const err = new Error(body.message);
err.statusCode = body.code;
return err;
};

request(req, (err, resp, body) => {
if (err) return reject(buildError(err));

Expand All @@ -69,4 +78,38 @@ module.exports = function doRequest (client, options) {
return resolve(body);
});
});
};
}

function doRequestWithBasicAuth (client, options = {}) {
return new Promise((resolve, reject) => {
const requestSettings = (client.settings && client.settings.request) ? client.settings.request : {};
const clientConfig = privates.get(client).config;
// basic auth header
const credentials = `${clientConfig.user.username}:${clientConfig.user.password}`;
const auth = `Basic ${Buffer.from(credentials).toString('base64')}`;
// basic authentication request options
const headersDef = {
headers: {
'X-CSRF-Token': 1,
'Authorization': auth
}
};

const baseOptions = {
method: 'GET',
url: `${client.authUrl}`
};

const req = Object.assign({}, headersDef, options, baseOptions, requestSettings);
request(req, (err, resp) => {
if (err) return reject(buildError(err));

// extract access_token value from hash header
const hash = resp.request.uri.hash;
const startIndex = hash.indexOf('=') + 1;
const stopIndex = hash.indexOf('&');
const accessToken = hash.slice(startIndex, stopIndex);
return resolve(accessToken);
});
});
}
2 changes: 1 addition & 1 deletion lib/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function loadConfig (client) {
return load(client.settings).then((config) => {
client.apiUrl = `${config.cluster}/oapi/${config.apiVersion}`;
client.kubeUrl = `${config.cluster}/api/${config.apiVersion}`;

client.authUrl = `${config.cluster}/oauth/authorize?response_type=token&client_id=openshift-challenging-client`;
client.apis = {
oapi: {
version: config.apiVersion,
Expand Down

0 comments on commit f5778b9

Please sign in to comment.