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

Unable to set either env or collection variables via collection level Pre Request script #2266

Open
2 tasks done
XDIJK opened this issue May 8, 2024 · 8 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@XDIJK
Copy link

XDIJK commented May 8, 2024

I have checked the following:

  • I use the newest version of bruno.
  • I've searched existing issues and found nothing related to my issue.

Describe the bug

OS & Version

OS: Ubuntu 22.04.4 LTS
Bruno version: V1.17.0 deb pkg

I've been attempting to set any form of variable via a collection level Pre Request script and am seemingly unable to do so. unsure if I am missing something obvious or it is simply not working.

The variable neither can be interpolated in any request nor is it visibly set in the Bruno variables tab.
This was tested both on 1.16.x & on 1.17.0

Similar issue can be found here however none of the work arounds seemed to have worked for me: #1019

Likewise I found some odd behaviour where the request level Post Request script finishes before the Pre Request script does most likely related to some odd async axios behaviour.

Furthermore I have also tried to write the script without async functions and simply awaiting the response of the POST request which simply does not execute the POST not sure what is going on there.

I've also tried creating or removing the variables and letting the script instantiate them but no combination of any of it seems to work.

Pre Request script

const axios = require('axios');

var url = "https://cognito-idp.eu-central-1.amazonaws.com/";
var clientId = bru.getEnvVar("cognitoClientId");
var username = bru.getEnvVar("cognitoUserName");
var password = bru.getEnvVar("cognitoUserPassword");

async function getBearer() {
    const response = await axios.post(url, {
        "AuthParameters": {
            "USERNAME": username,
            "PASSWORD": password
        },
        "AuthFlow": "USER_PASSWORD_AUTH",
        "ClientId": clientId
    }, {
        headers: {
            'X-Amz-Target': 'AWSCognitoIdentityProviderService.InitiateAuth',
            'Content-Type': 'application/x-amz-json-1.1'
        },
    })
    return response.data
}

async function main() {
   const bearerRes = await getBearer()
    bru.setVar("cognitoAccessToken", bearerRes.AuthenticationResult.AccessToken);
    
    bru.setVar("cognitoIdToken", bearerRes.AuthenticationResult.IdToken);
  
  console.log(bru.getVar("cognitoAccessToken")) //Logs successfully using either using .getEnvVar or .getVar
}

main()

Pre Request script without functions as in documentation:

const axios = require('axios');

var url = "https://cognito-idp.eu-central-1.amazonaws.com/";
var clientId = bru.getEnvVar("cognitoClientId");
var username = bru.getEnvVar("cognitoUserName");
var password = bru.getEnvVar("cognitoUserPassword");

console.log("Start") //executes
    const response = await axios.post(url, {
        "AuthParameters": {
            "USERNAME": username,
            "PASSWORD": password
        },
        "AuthFlow": "USER_PASSWORD_AUTH",
        "ClientId": clientId
    }, {
        headers: {
            'X-Amz-Target': 'AWSCognitoIdentityProviderService.InitiateAuth',
            'Content-Type': 'application/x-amz-json-1.1'
        },
    });

console.log("Does not exec", response) //Doesn't execute
  
bru.setVar("cognitoAccessToken", response.data.bearerRes.AuthenticationResult.AccessToken);
    
    bru.setVar("cognitoIdToken", response.data.AuthenticationResult.IdToken);
  
  console.log(bru.getVar("cognitoAccessToken"))

.bru file to reproduce the bug

No response

Screenshots/Live demo link

brun-env-tab
bruno-collection-pre-request
bruno-pre-request
bruno-request-timeline

@XDIJK XDIJK added the bug Something isn't working label May 8, 2024
@jwsloan
Copy link

jwsloan commented May 8, 2024

I just got Cognito auth working myself. I ran into some strange issues for a while, and then it worked. I'm not sure what I did that finally made it successful.

The setup below also works for the schema introspection call if your API is in AppSync and authenticated via Cognito. Initially I was setting the header directly on the req object in the script, but that only worked for the actual API call. It did not work for the introspection call.

Here is the Pre-Request script I'm using:

 const axios = require('axios');

  const url = 'https://cognito-idp.us-east-1.amazonaws.com/';
  const headers = {
    'Content-Type': 'application/x-amz-json-1.1',
    'X-Amz-Target': 'AWSCognitoIdentityProviderService.InitiateAuth'
  };
  const data = {
    "AuthFlow": "USER_PASSWORD_AUTH",
    "AuthParameters": {
      "USERNAME": bru.getEnvVar("username"),
      "PASSWORD": bru.getEnvVar("password")
    },
    "ClientId": bru.getEnvVar("userPoolClientId")
  };

  response = await axios.post(url, data, { headers })

  bru.setVar("authorizationToken",response.data.AuthenticationResult.IdToken)

I think set the Collection level headers:

image

@jwsloan
Copy link

jwsloan commented May 8, 2024

@XDIJK if this works for you, I'll see about making a contribution updating some docs about authenticating with Cognito.

@XDIJK
Copy link
Author

XDIJK commented May 13, 2024

@jwsloan Thanks for the response, the actual cognito call is working just fine and as far as I can see/understand the you posted snippet above doesn't really differ from the scripts I posted.

Other than on my end await directly on the call vs via function not working at all.

However on the first script in the parent post the I am getting a valid authToken back from cognito it is the variable assignment which is failing/not happening.

@KevUp
Copy link

KevUp commented May 13, 2024

I am experiencing the same issue as @jwsloan. Running a collection Pre Request script. the script is getting an auth token, all working fine apart from when I try to set a variable with either:

bru.setEnvVar("access_token", ${access_token});
bru.setVar("access_token", access_token);

neither work!

@jwsloan
Copy link

jwsloan commented May 13, 2024

A couple of questions to consider, @KevUp @XDIJK

  1. How are you accessing the variable after setting it?
  2. Have you tried setting a header on the request directly instead?
    • This worked for me for the actual API request, but it did not work for the introspection call.

@KevUp
Copy link

KevUp commented May 14, 2024

Bruno v1.17.0
This is part of the code it getting a authentication JWT to use on API calls:

const axiosInstance = axios.create({
  baseURL: tokenUrl,
  headers: {
    "content-type": "application/json",
    "api_key": api_key,
  },
 });

const body = { 
  userName: user,
  password: password
};

axiosInstance.post('', body).then(response => {
  const access_token = response.data['id-token'];
  console.log('Authentication successful!');
  console.log('Access token:', access_token);

  // Use the access token for subsequent API requests
  bru.setEnvVar("access_token", access_token);
 
})
.catch(error => {
console.error('Authentication failed:', error);
});

and this is the response, so I know the call is good:

image

and this is how I am accessing the collection var in my subsequent calls :

image

@XDIJK
Copy link
Author

XDIJK commented May 16, 2024

@jwsloan Rather odd update it just started working without any changes made nor an update of Bruno.
Scripts are the exact same as in the original post.

So on my end it is inexplicably working right now.

To answer your question above however:
As in how I access the var the same as @KevUp above but using setVar() instead of setEnvVar()
I did not try setting it directly on the request.

@technodrome
Copy link

I have the same issue. Tokens are printed correctly in the dev console but I cannot use the variable in the Auth tab in {{token}} format at all. #2358 (comment)

Env vars seem to me broken in 1.18.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants