diff --git a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/CodeView/index.js b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/CodeView/index.js index f0fa506b2..6771f4ced 100644 --- a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/CodeView/index.js +++ b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/CodeView/index.js @@ -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(); @@ -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'); + + 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) { diff --git a/packages/bruno-app/src/utils/codegenerator/auth.js b/packages/bruno-app/src/utils/codegenerator/auth.js new file mode 100644 index 000000000..4b3d23e64 --- /dev/null +++ b/packages/bruno-app/src/utils/codegenerator/auth.js @@ -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 []; + } +};