Skip to content

Commit

Permalink
Use undici in basic-auth-request
Browse files Browse the repository at this point in the history
  • Loading branch information
danbev committed Oct 10, 2022
1 parent 4217a9f commit a6d1f4c
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 92 deletions.
61 changes: 47 additions & 14 deletions lib/basic-auth-request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const request = require('request');
const { Client } = require('undici');
const { getAuthUrlFromOCP } = require('./authorization-server-request');

const buildError = (requestError) => {
Expand All @@ -13,23 +13,23 @@ const buildError = (requestError) => {
// This will return a User Openshift Object
async function getUserFromAuthToken (settings) {
return new Promise((resolve, reject) => {
const req = {
const client = new Client(settings.url, {
connect: {
strictSSL: 'insecureSkipTlsVerify' in settings ? !settings.insecureSkipTlsVerify : true
}
});
const requestOptions = {
path: 'apis/user.openshift.io/v1/users/~',
method: 'GET',
url: new URL('/apis/user.openshift.io/v1/users/~', settings.url).toString(),
auth: {
bearer: settings.token
},
strictSSL: 'insecureSkipTlsVerify' in settings ? !settings.insecureSkipTlsVerify : true
headers: { Authorization: `Bearer ${settings.token}` }
};

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

if (resp.statusCode === 401) {
client.request(requestOptions).then(async (responseData) => {
if (responseData.statusCode === 401) {
return reject(new Error(`401 Unable to authenticate with token ${settings.token}`));
}

return resolve(JSON.parse(body));
return resolve(await responseData.body.json());
}).catch((err) => {
return reject(buildError(err));
});
});
}
Expand All @@ -41,6 +41,19 @@ async function getTokenFromBasicAuth (settings) {
const credentials = `${settings.user}:${settings.password}`;
const auth = `Basic ${Buffer.from(credentials).toString('base64')}`;

const client = new Client(authUrl, {
connect: {
strictSSL: 'insecureSkipTlsVerify' in settings ? !settings.insecureSkipTlsVerify : true
}
});

const requestOptions = {
path: authUrl,
method: 'GET',
headers: { Authorization: auth }
};

/*
const req = {
method: 'GET',
url: authUrl,
Expand All @@ -49,7 +62,26 @@ async function getTokenFromBasicAuth (settings) {
},
strictSSL: 'insecureSkipTlsVerify' in settings ? !settings.insecureSkipTlsVerify : true
};
*/
client.request(requestOptions).then(async (responseData) => {
if (responseData.statusCode === 401) {
return reject(new Error(`401 Unable to authenticate user ${settings.user}`));
}
const body = await responseData.body.json();
const hash = body.uri.hash;
if (hash) {
const startIndex = hash.indexOf('=') + 1;
const stopIndex = hash.indexOf('&');
const accessToken = hash.slice(startIndex, stopIndex);
return resolve(accessToken);
} else {
return reject(new Error(`Unable to authenticate user ${settings.user} to ${settings.url}. Cannot obtain access token from response.`));
}
}).catch((err) => {
return reject(buildError(err));
});

/*
request(req, (err, resp, body) => {
if (err) return reject(buildError(err));
Expand All @@ -67,6 +99,7 @@ async function getTokenFromBasicAuth (settings) {
return reject(new Error(`Unable to authenticate user ${settings.user} to ${resp.request.uri.host}. Cannot obtain access token from response.`));
}
});
*/
});
}

Expand Down
Loading

0 comments on commit a6d1f4c

Please sign in to comment.