Kong / insomnia

The open-source, cross-platform API client for GraphQL, REST, WebSockets, SSE and gRPC. With Cloud, Local and Git storage.
https://insomnia.rest
Apache License 2.0
34.31k stars 1.93k forks source link

Using nested variable in base64 encode function #2703

Open TjorbenWridt opened 3 years ago

TjorbenWridt commented 3 years ago

Describe the bug I have some request in our application which need a double authentication, due to thrid party software which is in use. Therefore I tried to use the base64 encode function of insomnia, but the function can only work with one parameter. So I introduced another variable which stores the concat of the username + : + password. This variable is added to the base64 function. The return value is stored in another variable, which is then used in the customHeader in the request.

In the environment view the value is correct base64 encoded. But if I use the variable with the base64 encoded value in the request view, the generated value is not correct. If you decode the value with another online tool you will see that the decoded value will show {{ application.user.username }} + {{ application.user.password }} .

If the concatValue and the base64 variable are moved to the first level in the JSON it works. Only if it is nested it is not working.

To Reproduce Steps to reproduce the behavior:

Here is an example how the environment looks like:

Base environment:

{
  "application": {
    "utils": {
       "concatValue": "{{ application.credentials.user.username }} : {{ application.credentials.user.password }}",
       "base64EncodedValue": "base64({{ application.utils.concatValue }})"
     }
  }
} 

Subenvironment:

{
  "application": {
    "credentials": {
     "user": {
        "username": "test",
        "password": "test"
      }
    }
  }
}

Add the application.utils.base64EncodedValue to the request header value.

Expected behavior The nested value should be base64 encoded and not the variable names.

Screenshots No screenshot as the preview value is not captured. Sorry

Desktop (please complete the following information):

develohpanda commented 3 years ago

Hi @TjorbenWridt!

How is "base64EncodedValue": "base64({{ application.utils.concatValue }})" working for you? That just resolves to base64(test : test) for me...

Regardless, there is a way to achieve this using Nunjucks templating.

Firstly, when you create a new sub environment, it isn't automatically activated, so your base environment concatValue doesn't resolve correctly (and thus resolves to the variable names themselves). If you open the environment drop-down again, and select your environment, they should resolve correctly.

image

Secondly, in order to use base64 encoding you need to use Nunjucks. Try and use the following base environment:

{
  "application": {
    "utils": {
      "concatValue": "{{ application.credentials.user.username }} : {{ application.credentials.user.password }}",
      "base64EncodedValue": "{% base64 'encode', 'normal', application.utils.concatValue %}"
    }
  }
}

The above environment will use encode application.utils.concatValue, and render with the Base64 => Encode tag like this: image

Adding it manually To add the `Base64` tag manually, hit `ctrl + space` or `cmd + space` and search for it. Then click on the tag, and select `Environment Variable` from the cog next to the value input in the modal that opens: ![image](https://user-images.githubusercontent.com/4312346/96657046-a4c37c00-139d-11eb-88e9-3f5063dddf26.png)

After that, you can use application.utils.base64EncodedValue in the request header, with the correct encoded value.

Let me know if this solves your concern!

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

bbbco commented 1 year ago

Likely related to https://github.com/Kong/insomnia/issues/3919