Skip to content

Commit

Permalink
feat: Replace request with undici (#322)
Browse files Browse the repository at this point in the history
* Replace request dependencies with undici

* Use undici in authorization-server-request.js

* Use undici in basic-auth-request
  • Loading branch information
danbev authored Oct 13, 2022
1 parent 0fa1283 commit b4d606b
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 201 deletions.
29 changes: 16 additions & 13 deletions lib/authorization-server-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 buildError = (requestError) => {
const err = new Error(requestError.message);
Expand All @@ -15,25 +15,28 @@ const buildError = (requestError) => {
* @param {boolean} insecureSkipTlsVerify validate ssl
*/
const getAuthUrlFromOCP = async (url, insecureSkipTlsVerify = true) => {
const req = {
method: 'GET',
url: new URL('/.well-known/oauth-authorization-server', url).toString(),
strictSSL: insecureSkipTlsVerify
};

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

if (resp.statusCode === 404) {
const client = new Client(url, {
connect: {
strictSSL: insecureSkipTlsVerify
}
});
const requestOptions = {
path: '.well-known/oauth-authorization-server',
method: 'GET'
};
client.request(requestOptions).then(async (responseData) => {
if (responseData.statusCode === 404) {
return reject(new Error('404 Unable to get the auth url'));
}
const bodyJSON = JSON.parse(body);
const bodyJSON = await responseData.body.json();
if (bodyJSON.authorization_endpoint) {
return resolve(`${bodyJSON.authorization_endpoint}?response_type=token&client_id=openshift-challenging-client`);
} else {
return reject(new Error(`Unable to retrieve the token_endpoint for ${resp.request.uri.host}. Cannot obtain token_endpoint from response.`));
return reject(new Error(`Unable to retrieve the token_endpoint for ${url}. Cannot obtain token_endpoint from response.`));
}
}).catch((err) => {
return reject(buildError(err));
});
});
};
Expand Down
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
53 changes: 52 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"license": "Apache-2.0",
"dependencies": {
"kubernetes-client": "9.0.0",
"request": "~2.88.2"
"undici": "^5.11.0"
},
"devDependencies": {
"coveralls": "~3.1.1",
Expand Down
Loading

0 comments on commit b4d606b

Please sign in to comment.