mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-28 23:24:05 +00:00
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.
This commit is contained in:
@@ -283,6 +283,7 @@ const configureRequest = async (
|
||||
break;
|
||||
}
|
||||
}
|
||||
request.credentials = credentials;
|
||||
request.headers['Authorization'] = `Bearer ${credentials.access_token}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user