Skip to content

Commit

Permalink
fix: add authorization token to generate code
Browse files Browse the repository at this point in the history
Fixes #1791
  • Loading branch information
Leonardo Ferreira Lima committed Apr 27, 2024
1 parent c17e4ef commit 5d73ff1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CopyToClipboard } from 'react-copy-to-clipboard';
import toast from 'react-hot-toast';
import { IconCopy } from '@tabler/icons';
import { findCollectionByItemUid } from '../../../../../../../utils/collections/index';
import { getAuthHeaders } from '../../../../../../../utils/codegenerator/auth';

const CodeView = ({ language, item }) => {
const { displayedTheme } = useTheme();
Expand All @@ -20,10 +21,16 @@ const CodeView = ({ language, item }) => {
item.uid
);

const headers = [...(collection?.root?.request?.headers || []), ...(requestHeaders || [])];
const collectionRootAuth = collection?.root?.request?.auth;
const requestAuth = item.draft ? get(item, 'draft.request.auth') : get(item, 'request.auth');

let snippet = '';
const headers = [
...getAuthHeaders(collectionRootAuth, requestAuth),
...(collection?.root?.request?.headers || []),
...(requestHeaders || [])
];

let snippet = '';
try {
snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers })).convert(target, client);
} catch (e) {
Expand Down
30 changes: 30 additions & 0 deletions packages/bruno-app/src/utils/codegenerator/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import get from 'lodash/get';

export const getAuthHeaders = (collectionRootAuth, requestAuth) => {
const auth = collectionRootAuth && ['inherit', 'none'].includes(requestAuth.mode) ? collectionRootAuth : requestAuth;

switch (auth.mode) {
case 'basic':
const username = get(auth, 'basic.username');
const password = get(auth, 'basic.password');
const basicToken = Buffer.from(`${username}:${password}`).toString('base64');

return [
{
enabled: true,
name: 'Authorization',
value: `Basic ${basicToken}`
}
];
case 'bearer':
return [
{
enabled: true,
name: 'Authorization',
value: `Bearer ${get(auth, 'bearer.token')}`
}
];
default:
return [];
}
};

0 comments on commit 5d73ff1

Please sign in to comment.