From 4afcd442166af55cbc1b6dd530b0821a4d06b2ae Mon Sep 17 00:00:00 2001 From: Mateusz Pietryga Date: Sat, 13 Apr 2024 23:57:32 +0200 Subject: [PATCH] feat: OAuth2 - Include resolved authorization details in req object to be usable by scripts The new variable 'credentials' is now available in 'req' object. It is added automatically during request preparation if oauth2 method is used and is value is either evaluated or retrieved from collection oauth2 cache. --- .../bruno-electron/src/ipc/network/index.js | 1 + packages/bruno-js/src/bruno-request.js | 18 ++++++++++++------ .../src/sandbox/quickjs/shims/bruno-request.js | 2 ++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 407aeef39..001e56be5 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -283,6 +283,7 @@ const configureRequest = async ( break; } } + request.credentials = credentials; request.headers['Authorization'] = `Bearer ${credentials.access_token}`; } diff --git a/packages/bruno-js/src/bruno-request.js b/packages/bruno-js/src/bruno-request.js index b0d22b6ac..d716727fb 100644 --- a/packages/bruno-js/src/bruno-request.js +++ b/packages/bruno-js/src/bruno-request.js @@ -6,7 +6,8 @@ class BrunoRequest { * - req.headers * - req.timeout * - req.body - * + * - req.credentials + * * Above shorthands are useful for accessing the request properties directly in the scripts * It must be noted that the user cannot set these properties directly. * They should use the respective setter methods to set these properties. @@ -17,13 +18,14 @@ class BrunoRequest { this.method = req.method; this.headers = req.headers; this.timeout = req.timeout; + this.credentials = req.credentials; /** * We automatically parse the JSON body if the content type is JSON * This is to make it easier for the user to access the body directly - * + * * It must be noted that the request data is always a string and is what gets sent over the network - * If the user wants to access the raw data, they can use getBody({raw: true}) method + * If the user wants to access the raw data, they can use getBody({raw: true}) method */ const isJson = this.hasJSONContentType(this.req.headers); if (isJson) { @@ -84,6 +86,10 @@ class BrunoRequest { this.req.headers[name] = value; } + getCredentials() { + return this.credentials; + } + hasJSONContentType(headers) { const contentType = headers?.['Content-Type'] || headers?.['content-type'] || ''; return contentType.includes('json'); @@ -91,7 +97,7 @@ class BrunoRequest { /** * Get the body of the request - * + * * We automatically parse and return the JSON body if the content type is JSON * If the user wants the raw body, they can pass the raw option as true */ @@ -115,7 +121,7 @@ class BrunoRequest { * Otherwise * - We set the request data as the data itself * - We set the body property as the data itself - * + * * If the user wants to override this behavior, they can pass the raw option as true */ setBody(data, options = {}) { @@ -168,7 +174,7 @@ class BrunoRequest { __isObject(obj) { return obj !== null && typeof obj === 'object'; } - + disableParsingResponseJson() { this.req.__brunoDisableParsingResponseJson = true; diff --git a/packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js b/packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js index 1edfaaadb..0990270ac 100644 --- a/packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js +++ b/packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js @@ -8,12 +8,14 @@ const addBrunoRequestShimToContext = (vm, req) => { const headers = marshallToVm(req.getHeaders(), vm); const body = marshallToVm(req.getBody(), vm); const timeout = marshallToVm(req.getTimeout(), vm); + const credentials = marshallToVm(req.getCredentials(), vm); vm.setProp(reqObject, 'url', url); vm.setProp(reqObject, 'method', method); vm.setProp(reqObject, 'headers', headers); vm.setProp(reqObject, 'body', body); vm.setProp(reqObject, 'timeout', timeout); + vm.setProp(reqObject, 'credentials', credentials); url.dispose(); method.dispose();