Context
In Gravitee 4.2, I'm configuring a V2 API for which I'm extracting the user id from an OAuth2 access token from the incoming request and add an Authorization Basic header to this request with the user:password encoded in base 64 before forwarding it to my backend.
Extracting the user id from the token is working fine using the JSON Web Tokens policy.
To add the Authorization header to the request I'm using the Transform Headers policy. But I cannot manage writing a working Gravitee Expression Language to add the header with the "user:password" part base 64 encoded. And I find it really hard to debug the EL. I didn't find built-in solution to base 64 encode a string.
The documentation states that GEL extends Spring EL but I couldn't find "advanced" example.
I'm trying to use the java.util.Base64 class for the encoding.
This EL doesn't work:
{T(java.util.Base64).getEncoder().encodeToString({T(java.lang.String).valueOf('test:test').getBytes()})}
But this subpart is working. It get the bytes array from the provided String :
{T(java.lang.String).valueOf('test:test').getBytes()}
Questions
- Is there another, simpler, way to add (dynamically) an authorization header to the requests ?
- Is it possible to write an EL to encode a String in base 64 ?
java.util.Base64andjava.util.Base64$Encoder).With gravitee.yml On the
gravitee.ymlfile of the gateway, you can configure the expression language whitelist in the dedicated section:With environment variables
You can configure your gateway using those environment variables:
About the expression you used
You can simplify it a bit like that (you do not need to use
String.valueOf):Edit
If the syntax
java.util.Base64$Encoderused in the whitelist doesn't work, replacing the$by a.might work instead (especially in case of environment variable declaration in a docker compose yaml file for example):java.util.Base64.EncoderHave a good day!