Skip to content

Instantly share code, notes, and snippets.

@zpeters
Created December 16, 2021 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zpeters/d6f32fa463d88c332e8703542b984f35 to your computer and use it in GitHub Desktop.
Save zpeters/d6f32fa463d88c332e8703542b984f35 to your computer and use it in GitHub Desktop.
Azure API Policy Pax8 - Cache Auth Token
<cache-lookup-value key="@(" pax8bearerToken")" variable-name="pax8bearerToken" /><choose>
<when condition="@(!context.Variables.ContainsKey(" pax8bearerToken"))">
<send-request mode="new" response-variable-name="accessTokenResult" timeout="20" ignore-error="true">
<set-url>https://login.pax8.com/oauth/token</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body template="liquid">
{
"client_id": "{{Pax8-ClientID}}",
"client_secret": "{{Pax8-Secret}}",
"audience": "api://p8p.client",
"grant_type": "client_credentials"
}
</set-body>
</send-request>
<set-variable name="accessToken" value="@(((IResponse)context.Variables[" accessTokenResult"]).Body.As<JObject>())" />
<set-variable name="pax8bearerToken" value="@((string)((JObject)context.Variables[" accessToken"])[" access_token"])" />
<set-variable name="tokenDurationSeconds" value="@((int)((JObject)context.Variables[" accessToken"])[" expires_in"])" />
<cache-store-value key="pax8bearerToken" value="@((string)context.Variables[" pax8bearerToken"])" duration="@((int)context.Variables[" tokenDurationSeconds"])" />
</when>
</choose>
<set-header name="Authorization" exists-action="override">
<value>@("Bearer " + (string)context.Variables["pax8bearerToken"])</value>
</set-header>
<set-backend-service base-url="https://api.pax8.com/v1" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment