Skip to content

Commit

Permalink
fix(#329): fixed basic auth header issue + added cli support for basi…
Browse files Browse the repository at this point in the history
…c and bearer auth
  • Loading branch information
helloanoop committed Oct 4, 2023
1 parent 90fedc8 commit 8d89496
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/bruno-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"dependencies": {
"@usebruno/js": "0.6.0",
"@usebruno/lang": "0.4.0",
"@usebruno/lang": "0.5.0",
"axios": "^1.5.1",
"chai": "^4.3.7",
"chalk": "^3.0.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/bruno-cli/src/runner/interpolate-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
}
}

// todo: we have things happening in two places w.r.t basic auth
// need to refactor this in the future
// the request.auth (basic auth) object gets set inside the prepare-request.js file
if (request.auth) {
const username = interpolate(request.auth.username) || '';
const password = interpolate(request.auth.password) || '';

// use auth header based approach and delete the request.auth object
request.headers['authorization'] = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
delete request.auth;
}

return request;
};

Expand Down
14 changes: 14 additions & 0 deletions packages/bruno-cli/src/runner/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ const prepareRequest = (request) => {
headers: headers
};

// Authentication
if (request.auth) {
if (request.auth.mode === 'basic') {
axiosRequest.auth = {
username: get(request, 'auth.basic.username'),
password: get(request, 'auth.basic.password')
};
}

if (request.auth.mode === 'bearer') {
axiosRequest.headers['authorization'] = `Bearer ${get(request, 'auth.bearer.token')}`;
}
}

request.body = request.body || {};

if (request.body.mode === 'json') {
Expand Down
2 changes: 2 additions & 0 deletions packages/bruno-cli/src/utils/bru.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const bruToJson = (bru) => {
request: {
method: _.upperCase(_.get(json, 'http.method')),
url: _.get(json, 'http.url'),
auth: _.get(json, 'auth', {}),
params: _.get(json, 'query', []),
headers: _.get(json, 'headers', []),
body: _.get(json, 'body', {}),
Expand All @@ -49,6 +50,7 @@ const bruToJson = (bru) => {
};

transformedJson.request.body.mode = _.get(json, 'http.body', 'none');
transformedJson.request.auth.mode = _.get(json, 'http.auth', 'none');

return transformedJson;
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/bruno-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"dependencies": {
"@usebruno/js": "0.6.0",
"@usebruno/lang": "0.4.0",
"@usebruno/lang": "0.5.0",
"@usebruno/schema": "0.5.0",
"about-window": "^1.15.2",
"axios": "^1.5.1",
Expand Down
12 changes: 12 additions & 0 deletions packages/bruno-electron/src/ipc/network/interpolate-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
}
}

// todo: we have things happening in two places w.r.t basic auth
// need to refactor this in the future
// the request.auth (basic auth) object gets set inside the prepare-request.js file
if (request.auth) {
const username = interpolate(request.auth.username) || '';
const password = interpolate(request.auth.password) || '';

// use auth header based approach and delete the request.auth object
request.headers['authorization'] = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
delete request.auth;
}

return request;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/bruno-lang/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@usebruno/lang",
"version": "0.4.0",
"license" : "MIT",
"version": "0.5.0",
"license": "MIT",
"main": "src/index.js",
"files": [
"src",
Expand Down

0 comments on commit 8d89496

Please sign in to comment.