diff --git a/packages/bruno-app/src/components/RequestPane/Auth/AuthMode/StyledWrapper.js b/packages/bruno-app/src/components/RequestPane/Auth/AuthMode/StyledWrapper.js index 6613dd1f3..e1d0530c6 100644 --- a/packages/bruno-app/src/components/RequestPane/Auth/AuthMode/StyledWrapper.js +++ b/packages/bruno-app/src/components/RequestPane/Auth/AuthMode/StyledWrapper.js @@ -4,8 +4,7 @@ const Wrapper = styled.div` font-size: 0.8125rem; .auth-mode-selector { - background: ${(props) => props.theme.requestTabPanel.bodyModeSelect.color}; - border-radius: 3px; + background: transparent; .dropdown-item { padding: 0.2rem 0.6rem !important; diff --git a/packages/bruno-app/src/components/RequestPane/Auth/AuthMode/index.js b/packages/bruno-app/src/components/RequestPane/Auth/AuthMode/index.js index 3ad80ce13..f1d2e443f 100644 --- a/packages/bruno-app/src/components/RequestPane/Auth/AuthMode/index.js +++ b/packages/bruno-app/src/components/RequestPane/Auth/AuthMode/index.js @@ -15,8 +15,8 @@ const AuthMode = ({ item, collection }) => { const Icon = forwardRef((props, ref) => { return ( -
- {humanizeRequestAuthMode(authMode)} +
+ {humanizeRequestAuthMode(authMode)}
); }); diff --git a/packages/bruno-app/src/components/RequestPane/Auth/BearerAuth/StyledWrapper.js b/packages/bruno-app/src/components/RequestPane/Auth/BearerAuth/StyledWrapper.js new file mode 100644 index 000000000..71d3bdf1d --- /dev/null +++ b/packages/bruno-app/src/components/RequestPane/Auth/BearerAuth/StyledWrapper.js @@ -0,0 +1,10 @@ +import styled from 'styled-components'; + +const Wrapper = styled.div` + .single-line-editor-wrapper { + padding: 0.15rem 0.4rem; + border: ${(props) => props.theme.sidebar.search.border}; + } +`; + +export default Wrapper; diff --git a/packages/bruno-app/src/components/RequestPane/Auth/BearerAuth/index.js b/packages/bruno-app/src/components/RequestPane/Auth/BearerAuth/index.js new file mode 100644 index 000000000..90347dc5a --- /dev/null +++ b/packages/bruno-app/src/components/RequestPane/Auth/BearerAuth/index.js @@ -0,0 +1,52 @@ +import React, { useState } from 'react'; +import get from 'lodash/get'; +import { useTheme } from 'providers/Theme'; +import { useDispatch } from 'react-redux'; +import SingleLineEditor from 'components/SingleLineEditor'; +import { updateBearerToken } from 'providers/ReduxStore/slices/collections'; +import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions'; +import StyledWrapper from './StyledWrapper'; + +const BearerAuth = ({ onTokenChange, item, collection }) => { + const dispatch = useDispatch(); + const { storedTheme } = useTheme(); + + const bearerToken = item.draft + ? get(item, 'draft.request.auth.bearer.token') + : get(item, 'request.auth.bearer.token'); + + const handleRun = () => dispatch(sendRequest(item, collection.uid)); + const handleSave = () => dispatch(saveRequest(item.uid, collection.uid)); + + const handleTokenChange = (token) => { + dispatch( + updateBearerToken({ + collectionUid: collection.uid, + itemUid: item.uid, + content: { + token: token + } + }) + ); + }; + + return ( + + +
+ handleTokenChange(val)} + onRun={handleRun} + collection={collection} + /> +
+
+ ); +}; + +export default BearerAuth; diff --git a/packages/bruno-app/src/components/RequestPane/Auth/index.js b/packages/bruno-app/src/components/RequestPane/Auth/index.js index e2f345d23..1210b0251 100644 --- a/packages/bruno-app/src/components/RequestPane/Auth/index.js +++ b/packages/bruno-app/src/components/RequestPane/Auth/index.js @@ -1,32 +1,30 @@ import React from 'react'; import get from 'lodash/get'; -import { useDispatch } from 'react-redux'; -import { updateRequestBody } from 'providers/ReduxStore/slices/collections'; -import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions'; +import AuthMode from './AuthMode'; +import BearerAuth from './BearerAuth'; import StyledWrapper from './StyledWrapper'; -const RequestBody = ({ item, collection }) => { - const dispatch = useDispatch(); +const Auth = ({ item, collection }) => { const authMode = item.draft ? get(item, 'draft.request.auth.mode') : get(item, 'request.auth.mode'); - const onEdit = (value) => { - // dispatch( - // updateRequestBody({ - // content: value, - // itemUid: item.uid, - // collectionUid: collection.uid - // }) - // ); + const getAuthView = () => { + switch (authMode) { + case 'basic': { + return
Basic Auth
; + } + case 'bearer': { + return ; + } + } }; - if (authMode === 'basic') { - return
Basic Auth
; - } - - if (authMode === 'bearer') { - return
Bearer Token
; - } - - return No Auth; + return ( + +
+ +
+ {getAuthView()} +
+ ); }; -export default RequestBody; +export default Auth; diff --git a/packages/bruno-app/src/components/RequestPane/HttpRequestPane/index.js b/packages/bruno-app/src/components/RequestPane/HttpRequestPane/index.js index 652414a62..ec20514fe 100644 --- a/packages/bruno-app/src/components/RequestPane/HttpRequestPane/index.js +++ b/packages/bruno-app/src/components/RequestPane/HttpRequestPane/index.js @@ -108,13 +108,10 @@ const HttpRequestPane = ({ item, collection, leftPaneWidth }) => {
) : null} - {focusedTab.requestPaneTab === 'auth' ? ( -
- -
- ) : null} -
+
{getTabPanel(focusedTab.requestPaneTab)}
diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index 0c6945ae9..ccdd7fe1a 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -12,7 +12,6 @@ import { getItemsToResequence, moveCollectionItemToRootOfCollection, findCollectionByUid, - recursivelyGetAllItemUids, transformRequestToSaveToFilesystem, findParentItemInCollection, findEnvironmentInCollection, diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index 176926e19..84762aa54 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -322,6 +322,26 @@ export const collectionsSlice = createSlice({ } } }, + updateBearerToken: (state, action) => { + const collection = findCollectionByUid(state.collections, action.payload.collectionUid); + + if (collection) { + const item = findItemInCollection(collection, action.payload.itemUid); + + if (item && isItemARequest(item)) { + if (!item.draft) { + item.draft = cloneDeep(item); + } + + item.draft.request.auth = item.draft.request.auth || {}; + switch (item.draft.request.auth.mode) { + case 'bearer': + item.draft.request.auth.bearer = action.payload.content; + break; + } + } + } + }, addQueryParam: (state, action) => { const collection = findCollectionByUid(state.collections, action.payload.collectionUid); @@ -1188,6 +1208,7 @@ export const { collectionClicked, collectionFolderClicked, requestUrlChanged, + updateBearerToken, addQueryParam, updateQueryParam, deleteQueryParam, diff --git a/packages/bruno-app/src/utils/collections/index.js b/packages/bruno-app/src/utils/collections/index.js index a1688890a..a2b63966b 100644 --- a/packages/bruno-app/src/utils/collections/index.js +++ b/packages/bruno-app/src/utils/collections/index.js @@ -355,6 +355,7 @@ export const transformRequestToSaveToFilesystem = (item) => { url: _item.request.url, params: [], headers: [], + auth: _item.request.auth, body: _item.request.body, script: _item.request.script, vars: _item.request.vars, diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index f07331c55..5a8512910 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -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')}`; + } + } + if (request.body.mode === 'json') { if (!contentTypeDefined) { axiosRequest.headers['content-type'] = 'application/json'; diff --git a/packages/bruno-lang/v2/src/jsonToBru.js b/packages/bruno-lang/v2/src/jsonToBru.js index b9a5779fa..8ef44d7ad 100644 --- a/packages/bruno-lang/v2/src/jsonToBru.js +++ b/packages/bruno-lang/v2/src/jsonToBru.js @@ -34,6 +34,11 @@ const jsonToBru = (json) => { body: ${http.body}`; } + if (http.auth && http.auth.length) { + bru += ` + auth: ${http.auth}`; + } + bru += ` } diff --git a/packages/bruno-lang/v2/tests/fixtures/request.bru b/packages/bruno-lang/v2/tests/fixtures/request.bru index f340bb9a4..c4ae4b058 100644 --- a/packages/bruno-lang/v2/tests/fixtures/request.bru +++ b/packages/bruno-lang/v2/tests/fixtures/request.bru @@ -7,6 +7,7 @@ meta { get { url: https://api.textlocal.in/send body: json + auth: bearer } query { diff --git a/packages/bruno-lang/v2/tests/fixtures/request.json b/packages/bruno-lang/v2/tests/fixtures/request.json index 47995832d..7a00f5bb3 100644 --- a/packages/bruno-lang/v2/tests/fixtures/request.json +++ b/packages/bruno-lang/v2/tests/fixtures/request.json @@ -7,7 +7,8 @@ "http": { "method": "get", "url": "https://api.textlocal.in/send", - "body": "json" + "body": "json", + "auth": "bearer" }, "query": [ { diff --git a/packages/bruno-lang/v2/tests/index.spec.js b/packages/bruno-lang/v2/tests/index.spec.js index ecc970670..e753ee962 100644 --- a/packages/bruno-lang/v2/tests/index.spec.js +++ b/packages/bruno-lang/v2/tests/index.spec.js @@ -14,7 +14,7 @@ describe('bruToJson', () => { }); describe('jsonToBru', () => { - it('should parse the bru file', () => { + it('should parse the json file', () => { const input = require('./fixtures/request.json'); const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'request.bru'), 'utf8'); const output = jsonToBru(input);