Skip to content

Commit

Permalink
Aligning cache duration to token duration (#38)
Browse files Browse the repository at this point in the history
* Update Backend OAuth2 Authentication With Cache.policy.xml

Remove token cache duration name value parameter and derive the value from the token received from token provider. Token duration and cache duration are now linked.

* Update Backend OAuth2 Authentication With Cache.policy.xml

Fixing typo.

* Update Backend OAuth2 Authentication With Cache.policy.xml

Strong typing tokenDurationSeconds

* Moved body result to variable.

Seems like the body result is in a stream so you can only read it once (after that you will probably be at the end of the stream). So trying to read twice for access_token and expires_in generated an error (not found on the second read). Moving the Body to a JObject variable solved the issue.
  • Loading branch information
Wagner Silveira authored and tomkerkhove committed Oct 28, 2019
1 parent 5660ddb commit dfc9c20
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/Backend OAuth2 Authentication With Cache.policy.xml
Expand Up @@ -7,7 +7,6 @@
<!-- Parameters: scope - a URI encoded scope value -->
<!-- Parameters: clientId - an id obtained during app registration -->
<!-- Parameters: clientSecret - a URL encoded secret, obtained during app registration -->
<!-- Parameters: tokenExpiresSeconds - a value indicating the time in seconds the token should be cached -->


<!-- Copy the following snippet into the inbound section. -->
Expand All @@ -27,8 +26,10 @@
return "client_id={{clientId}}&scope={{scope}}&client_secret={{clientSecret}}&grant_type=client_credentials";
}</set-body>
</send-request>
<set-variable name="bearerToken" value="@((String)((IResponse)context.Variables["accessToken"]).Body.As<JObject>()["access_token"])" />
<cache-store-value key="bearerToken" value="@((string)context.Variables["bearerToken"])" duration="{{tokenExpiresSeconds}}" />
<set-variable name="accessToken" value="@(((IResponse)context.Variables["accessTokenResult"]).Body.As<JObject>())" />
<set-variable name="bearerToken" value="@((string)((JObject)context.Variables["accessToken"])["access_token"])" />
<set-variable name="tokenDurationSeconds" value="@((int)((JObject)context.Variables["accessToken"])["expires_in"])" />
<cache-store-value key="bearerToken" value="@((string)context.Variables["bearerToken"])" duration="@((int)context.Variables["tokenDurationSeconds"])" />
</when>
</choose>
<set-header name="Authorization" exists-action="override">
Expand Down

0 comments on commit dfc9c20

Please sign in to comment.