Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#1946): Add support for custom prefix with bearer auth #2173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
import React from 'react';
import get from 'lodash/get';
import { useTheme } from 'providers/Theme';
import { useDispatch } from 'react-redux';
import SingleLineEditor from 'components/SingleLineEditor';
import get from 'lodash/get';
import { updateCollectionAuth } from 'providers/ReduxStore/slices/collections';
import { saveCollectionRoot } from 'providers/ReduxStore/slices/collections/actions';
import { useTheme } from 'providers/Theme';
import React from 'react';
import { useDispatch } from 'react-redux';
import StyledWrapper from './StyledWrapper';

const BearerAuth = ({ collection }) => {
const dispatch = useDispatch();
const { storedTheme } = useTheme();

const bearerToken = get(collection, 'root.request.auth.bearer.token', '');
const bearer = get(collection, 'root.request.auth.bearer', {});

const handleSave = () => dispatch(saveCollectionRoot(collection.uid));

const handlePrefixChange = (prefix) => {
dispatch(
updateCollectionAuth({
mode: 'bearer',
collectionUid: collection.uid,
content: {
prefix: prefix,
token: bearer.token
}
})
);
};

const handleTokenChange = (token) => {
dispatch(
updateCollectionAuth({
mode: 'bearer',
collectionUid: collection.uid,
content: {
prefix: bearer.prefix,
token: token
}
})
Expand All @@ -29,10 +43,20 @@ const BearerAuth = ({ collection }) => {

return (
<StyledWrapper className="mt-2 w-full">
<label className="block font-medium mb-2">Prefix</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={bearer.prefix || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handlePrefixChange(val)}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Token</label>
<div className="single-line-editor-wrapper">
<SingleLineEditor
value={bearerToken}
value={bearer.token || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleTokenChange(val)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
import React from 'react';
import SingleLineEditor from 'components/SingleLineEditor';
import get from 'lodash/get';
import { updateAuth } from 'providers/ReduxStore/slices/collections';
import { saveRequest, sendRequest } from 'providers/ReduxStore/slices/collections/actions';
import { useTheme } from 'providers/Theme';
import React from 'react';
import { useDispatch } from 'react-redux';
import SingleLineEditor from 'components/SingleLineEditor';
import { updateAuth } from 'providers/ReduxStore/slices/collections';
import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions';
import StyledWrapper from './StyledWrapper';

const BearerAuth = ({ 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 bearer = item.draft ? get(item, 'draft.request.auth.bearer', {}) : get(item, 'request.auth.bearer', {});

const handleRun = () => dispatch(sendRequest(item, collection.uid));
const handleSave = () => dispatch(saveRequest(item.uid, collection.uid));

const handlePrefixChange = (prefix) => {
dispatch(
updateAuth({
mode: 'bearer',
collectionUid: collection.uid,
itemUid: item.uid,
content: {
prefix: prefix,
token: bearer.token
}
})
);
};

const handleTokenChange = (token) => {
dispatch(
updateAuth({
mode: 'bearer',
collectionUid: collection.uid,
itemUid: item.uid,
content: {
prefix: bearer.prefix,
token: token
}
})
Expand All @@ -33,10 +46,21 @@ const BearerAuth = ({ item, collection }) => {

return (
<StyledWrapper className="mt-2 w-full">
<label className="block font-medium mb-2">Prefix</label>
<div className="single-line-editor-wrapper mb-2">
<SingleLineEditor
value={bearer.prefix || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handlePrefixChange(val)}
onRun={handleRun}
collection={collection}
/>
</div>
<label className="block font-medium mb-2">Token</label>
<div className="single-line-editor-wrapper">
<SingleLineEditor
value={bearerToken}
value={bearer.token || ''}
theme={storedTheme}
onSave={handleSave}
onChange={(val) => handleTokenChange(val)}
Expand Down
12 changes: 7 additions & 5 deletions packages/bruno-app/src/utils/collections/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep';
import each from 'lodash/each';
import filter from 'lodash/filter';
import find from 'lodash/find';
import findIndex from 'lodash/findIndex';
import get from 'lodash/get';
import isEqual from 'lodash/isEqual';
import isString from 'lodash/isString';
import map from 'lodash/map';
import filter from 'lodash/filter';
import sortBy from 'lodash/sortBy';
import isEqual from 'lodash/isEqual';
import cloneDeep from 'lodash/cloneDeep';
import { uuid } from 'utils/common';
import path from 'path';
import { uuid } from 'utils/common';

const replaceTabsWithSpaces = (str, numSpaces = 2) => {
if (!str || !str.length || !isString(str)) {
Expand Down Expand Up @@ -302,6 +302,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {}
password: get(si.draft.request, 'auth.basic.password', '')
},
bearer: {
preifx: get(si.draft.request, 'auth.bearer.prefix', ''),
token: get(si.draft.request, 'auth.bearer.token', '')
}
},
Expand Down Expand Up @@ -335,6 +336,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {}
password: get(si.request, 'auth.basic.password', '')
},
bearer: {
prefix: get(si.request, 'auth.bearer.prefix', ''),
token: get(si.request, 'auth.bearer.token', '')
}
},
Expand Down
19 changes: 13 additions & 6 deletions packages/bruno-app/src/utils/exporters/postman-collection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import map from 'lodash/map';
import * as FileSaver from 'file-saver';
import map from 'lodash/map';
import { deleteSecretsInEnvs, deleteUidsInEnvs, deleteUidsInItems } from 'utils/collections/export';

export const exportCollection = (collection) => {
Expand Down Expand Up @@ -145,11 +145,18 @@ export const exportCollection = (collection) => {
case 'bearer':
return {
type: 'bearer',
bearer: {
key: 'token',
value: itemAuth.bearer.token,
type: 'string'
}
bearer: [
{
key: 'prefix',
value: itemAuth.bearer.prefix,
type: 'string'
},
{
key: 'token',
value: itemAuth.bearer.token,
type: 'string'
}
]
};
case 'basic': {
return {
Expand Down
10 changes: 8 additions & 2 deletions packages/bruno-electron/src/ipc/network/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ const setAuthHeaders = (axiosRequest, request, collectionRoot) => {
};
break;
case 'bearer':
axiosRequest.headers['Authorization'] = `Bearer ${get(collectionAuth, 'bearer.token')}`;
axiosRequest.headers['Authorization'] = `${get(collectionAuth, 'bearer.prefix')} ${get(
collectionAuth,
'bearer.token'
)}`;
break;
case 'digest':
axiosRequest.digestConfig = {
Expand Down Expand Up @@ -92,7 +95,10 @@ const setAuthHeaders = (axiosRequest, request, collectionRoot) => {
};
break;
case 'bearer':
axiosRequest.headers['Authorization'] = `Bearer ${get(request, 'auth.bearer.token')}`;
axiosRequest.headers['Authorization'] = `${get(request, 'auth.bearer.prefix')} ${get(
request,
'auth.bearer.token'
)}`;
break;
case 'digest':
axiosRequest.digestConfig = {
Expand Down
5 changes: 4 additions & 1 deletion packages/bruno-lang/v2/src/bruToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,14 @@ const sem = grammar.createSemantics().addAttribute('ast', {
authbearer(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const tokenKey = _.find(auth, { name: 'token' });
const prefixKey = _.find(auth, { name: 'prefix' });
const token = tokenKey ? tokenKey.value : '';
const prefix = prefixKey ? prefixKey.value : '';
return {
auth: {
bearer: {
token
token,
prefix
}
}
};
Expand Down
5 changes: 4 additions & 1 deletion packages/bruno-lang/v2/src/collectionBruToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,14 @@ const sem = grammar.createSemantics().addAttribute('ast', {
authbearer(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const tokenKey = _.find(auth, { name: 'token' });
const prefixKey = _.find(auth, { name: 'prefix' });
const token = tokenKey ? tokenKey.value : '';
const prefix = prefixKey ? prefixKey.value : '';
return {
auth: {
bearer: {
token
token,
prefix
}
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/bruno-lang/v2/src/jsonToBru.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ ${indentString(`password: ${auth?.basic?.password || ''}`)}

if (auth && auth.bearer) {
bru += `auth:bearer {
${indentString(`prefix: ${auth?.bearer?.prefix || ''}`)}
${indentString(`token: ${auth?.bearer?.token || ''}`)}
}

Expand Down
1 change: 1 addition & 0 deletions packages/bruno-lang/v2/src/jsonToCollectionBru.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ ${indentString(`password: ${auth.basic.password}`)}

if (auth && auth.bearer) {
bru += `auth:bearer {
${indentString(`prefix: ${auth.bearer.prefix}`)}
${indentString(`token: ${auth.bearer.token}`)}
}

Expand Down
1 change: 1 addition & 0 deletions packages/bruno-lang/v2/tests/fixtures/collection.bru
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ auth:basic {
}

auth:bearer {
prefix: Token
token: 123
}

Expand Down
1 change: 1 addition & 0 deletions packages/bruno-lang/v2/tests/fixtures/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"password": "secret"
},
"bearer": {
"prefix": "Token",
"token": "123"
},
"digest": {
Expand Down
1 change: 1 addition & 0 deletions packages/bruno-lang/v2/tests/fixtures/request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ auth:basic {
}

auth:bearer {
prefix: Token
token: 123
}

Expand Down
1 change: 1 addition & 0 deletions packages/bruno-lang/v2/tests/fixtures/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"password": "secret"
},
"bearer": {
"prefix": "Token",
"token": "123"
},
"digest": {
Expand Down
1 change: 1 addition & 0 deletions packages/bruno-schema/src/collections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const authBasicSchema = Yup.object({
.strict();

const authBearerSchema = Yup.object({
prefix: Yup.string().nullable(),
token: Yup.string().nullable()
})
.noUnknown(true)
Expand Down