I am trying to decode a BASE 64 format token after getting it from object store but getting error in transform message.

Output after Object Store Retrieval. This needs to be decoded.

eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOiIxNzMzIiwiVXNlck5hbWUiOiJhY2hhbjAzMiIsIkN1bHR1cmVUYWciOm51bGwsImlhdCI6IjE1Njc2Njk3MDgiLCJleHAiOiIxNTY3NzU2MTA4IiwiaXNzIjoiVGltZVNoYXJlV2FyZSIsImF1ZCI6Imh0dHBzOi8vVGltZVNoYXJlV2FyZS5jb20vIn0.w2h6j7gbAs6CouE9WoYEDQRzBYEsNIqry70w2K4JAZg

    %dw 2.0
    import * from dw::core::Binaries
    output application/octet-stream
    ---
    fromBase64(payload as String) as Binary

ERROR MESSAGE:

    Message               : "Illegal base64 character 2e

    5| fromBase64(payload as String) as Binary
                  ^^^^^^^^^^^^^^^^^
    Trace:
      at fromBase64 (line: 5, column: 12)
      at main (line: 5, column: 1)" evaluating expression: "%dw 2.0
    import * from dw::core::Binaries
    output application/octet-stream
    ---
    fromBase64(payload as String) as Binary".
    Error type            : MULE:EXPRESSION
    Element               : authenticationflowtswFlow/processors/3 @ authenticationflowtsw:authenticationflowtsw.xml:30 (Transform Message)
    Element XML           : <ee:transform doc:name="Transform Message" doc:id="0cf3e737-e6a1-4d67-9a7c-60017db4ccf9">
    <ee:message>
    <ee:set-payload>%dw 2.0
    import * from dw::core::Binaries
    output application/octet-stream
    ---
    fromBase64(payload as String) as Binary</ee:set-payload>
    </ee:message>
    </ee:transform>

I expect to get something like-

{
"alg":"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
"typ":"JWT"}{"UserID":"1733",
"UserName":"achan032",
"CultureTag":null,
"iat":"1567669708",
"exp":"1567756108",
"iss":"TimeShareWare",
"aud":"https://TimeShareWare.com/"}0OVA`K
    "L6+@f
1

There are 1 best solutions below

0
Jason Gray On

Web Tokens use Base64Url instead of the typical Base64. They are basically the same except Base64Url are safe to pass in a URL because they use – instead of + and _ instead of / and they omit the = padding characters at the end of the string. You can do string replacement to properly convert the token then call fromBase64.

The 2e in the error message is a period "." which is not a Base64 standard character. This is because the token is a JWT and is structured like [Header].[Payload].[Signature] and each part individually Base64Url encoded. If you want to get the payload where all the claims are you will need to split the string/token by the period ".".

On the flip side if you defined a custom function called Base64Url, since no such function currently exists in Mule 4/Dataweave 2, you could build a JWT like the following: Base64Url(header) + "." + Base64Url(payload) + "." + Base64Url(signature)