Skip to content

Commit

Permalink
feat(#119): bru lang support for basic and bearer auth
Browse files Browse the repository at this point in the history
  • Loading branch information
helloanoop committed Sep 28, 2023
1 parent a6b1960 commit 51ee37c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
43 changes: 41 additions & 2 deletions packages/bruno-lang/v2/src/bruToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const { outdentString } = require('../../v1/src/utils');
*
*/
const grammar = ohm.grammar(`Bru {
BruFile = (meta | http | query | headers | bodies | varsandassert | script | tests | docs)*
BruFile = (meta | http | query | headers | auths | bodies | varsandassert | script | tests | docs)*
auths = authbasic | authbearer
bodies = bodyjson | bodytext | bodyxml | bodygraphql | bodygraphqlvars | bodyforms | body
bodyforms = bodyformurlencoded | bodymultipart
Expand Down Expand Up @@ -75,6 +76,9 @@ const grammar = ohm.grammar(`Bru {
varsres = "vars:post-response" dictionary
assert = "assert" assertdictionary
authbasic = "auth:basic" dictionary
authbearer = "auth:bearer" dictionary
body = "body" st* "{" nl* textblock tagend
bodyjson = "body:json" st* "{" nl* textblock tagend
bodytext = "body:text" st* "{" nl* textblock tagend
Expand All @@ -92,13 +96,21 @@ const grammar = ohm.grammar(`Bru {
docs = "docs" st* "{" nl* textblock tagend
}`);

const mapPairListToKeyValPairs = (pairList = []) => {
const mapPairListToKeyValPairs = (pairList = [], parseEnabled = true) => {
if (!pairList.length) {
return [];
}
return _.map(pairList[0], (pair) => {
let name = _.keys(pair)[0];
let value = pair[name];

if (!parseEnabled) {
return {
name,
value
};
}

let enabled = true;
if (name && name.length && name.charAt(0) === '~') {
name = name.slice(1);
Expand Down Expand Up @@ -282,6 +294,33 @@ const sem = grammar.createSemantics().addAttribute('ast', {
headers: mapPairListToKeyValPairs(dictionary.ast)
};
},
authbasic(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const usernameKey = _.find(auth, { name: 'username' });
const passwordKey = _.find(auth, { name: 'password' });
const username = usernameKey ? usernameKey.value : '';
const password = passwordKey ? passwordKey.value : '';
return {
auth: {
basic: {
username,
password
}
}
};
},
authbearer(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const tokenKey = _.find(auth, { name: 'token' });
const token = tokenKey ? tokenKey.value : '';
return {
auth: {
bearer: {
token
}
}
};
},
bodyformurlencoded(_1, dictionary) {
return {
body: {
Expand Down
19 changes: 18 additions & 1 deletion packages/bruno-lang/v2/src/jsonToBru.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const stripLastLine = (text) => {
};

const jsonToBru = (json) => {
const { meta, http, query, headers, body, script, tests, vars, assertions, docs } = json;
const { meta, http, query, headers, auth, body, script, tests, vars, assertions, docs } = json;

let bru = '';

Expand Down Expand Up @@ -82,6 +82,23 @@ const jsonToBru = (json) => {
bru += '\n}\n\n';
}

if (auth && auth.basic) {
bru += `auth:basic {
${indentString(`username: ${auth.basic.username}`)}
${indentString(`password: ${auth.basic.password}`)}
}
`;
}

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

if (body && body.json && body.json.length) {
bru += `body:json {
${indentString(body.json)}
Expand Down
9 changes: 9 additions & 0 deletions packages/bruno-lang/v2/tests/fixtures/request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ headers {
~transaction-id: {{transactionId}}
}

auth:basic {
username: john
password: secret
}

auth:bearer {
token: 123
}

body:json {
{
"hello": "world"
Expand Down
9 changes: 9 additions & 0 deletions packages/bruno-lang/v2/tests/fixtures/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
"enabled": false
}
],
"auth": {
"basic": {
"username": "john",
"password": "secret"
},
"bearer": {
"token": "123"
}
},
"body": {
"json": "{\n \"hello\": \"world\"\n}",
"text": "This is a text body",
Expand Down

0 comments on commit 51ee37c

Please sign in to comment.