From 51ee37cf969991449aa47fe4ad2255be19a76505 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Fri, 29 Sep 2023 01:35:22 +0530 Subject: [PATCH] feat(#119): bru lang support for basic and bearer auth --- packages/bruno-lang/v2/src/bruToJson.js | 43 ++++++++++++++++++- packages/bruno-lang/v2/src/jsonToBru.js | 19 +++++++- .../bruno-lang/v2/tests/fixtures/request.bru | 9 ++++ .../bruno-lang/v2/tests/fixtures/request.json | 9 ++++ 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/packages/bruno-lang/v2/src/bruToJson.js b/packages/bruno-lang/v2/src/bruToJson.js index 2241ca554..576c58c24 100644 --- a/packages/bruno-lang/v2/src/bruToJson.js +++ b/packages/bruno-lang/v2/src/bruToJson.js @@ -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 @@ -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 @@ -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); @@ -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: { diff --git a/packages/bruno-lang/v2/src/jsonToBru.js b/packages/bruno-lang/v2/src/jsonToBru.js index 818d7c9cb..b9a5779fa 100644 --- a/packages/bruno-lang/v2/src/jsonToBru.js +++ b/packages/bruno-lang/v2/src/jsonToBru.js @@ -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 = ''; @@ -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)} diff --git a/packages/bruno-lang/v2/tests/fixtures/request.bru b/packages/bruno-lang/v2/tests/fixtures/request.bru index ae7318da5..f340bb9a4 100644 --- a/packages/bruno-lang/v2/tests/fixtures/request.bru +++ b/packages/bruno-lang/v2/tests/fixtures/request.bru @@ -21,6 +21,15 @@ headers { ~transaction-id: {{transactionId}} } +auth:basic { + username: john + password: secret +} + +auth:bearer { + token: 123 +} + body:json { { "hello": "world" diff --git a/packages/bruno-lang/v2/tests/fixtures/request.json b/packages/bruno-lang/v2/tests/fixtures/request.json index 867229de8..47995832d 100644 --- a/packages/bruno-lang/v2/tests/fixtures/request.json +++ b/packages/bruno-lang/v2/tests/fixtures/request.json @@ -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",