2
votes

I am validating JWT token from Azure API Manager. I am looking for options to change the response body when the validation fails. As per documentation https://docs.microsoft.com/en-us/azure/api-management/api-management-access-restriction-policies.

<validate-jwt
    header-name="name of http header containing the token (use query-parameter-name attribute if the token is passed in the URL)"
    failed-validation-httpcode="401"
    failed-validation-error-message="Invalid Token"
    token-value="expression returning JWT token as a string"
    require-expiration-time="true|false"
    require-scheme="scheme"
    require-signed-tokens="true|false"
    clock-skew="allowed clock skew in seconds"
    output-token-variable-name="name of a variable to receive a JWT object representing successfully validated token">

If i set

  failed-validation-httpcode="401" and  failed-validation-error-message="Invalid Token"

the response when the validation fails would be

{
    "statusCode": 401,
    "message": "Invalid Token"
}

Now i need to change the "statusCode" in the body to "status" response to

{
    "status": 401,
    "message": "Invalid Token"
}

Is it possible in Azure API Manager?

1

1 Answers

3
votes

I have managed to fix this with the following transformation policy.

 <on-error>
     <choose>
         <when condition="@(context.Response.StatusCode == 401)">
            <find-and-replace from="statusCode" to="status" />
         </when>
    </choose> 
</on-error>