Setting a variable in Oauth from a javascript policy?

Not applicable

I found this issue while creating a condition in an Oauth proxy. I needed to set the token to expire based on scope. I created a javascript policy and built the condition

 var ExpirationTimeScope = context.getVariable("request.formparam.scope");  //captures the name of the scope 
 var ExpirationTimeSet = 3600000;  ///default scope setting 
 if (ExpirationTimeScope === 'mobile') { 
     ExpirationTimeSet = -1; //token never expires 
 }
context.setVariable('flow.expirationTime', ExpirationTimeSet
);

I made reference to this code within the Oauth Policy

  <Operation>GenerateAccessToken</Operation>
    <Scope>request.formparam.scope</Scope>
    <ExpiresIn ref="flow.expirationTime">3600000</ExpiresIn>

The code would complete but the scope wouldn't pick up. Can someone assist? I started receiving this message within the trace
"java.lang.Integer cannot be cast to java.lang.String"

1 1 226
1 REPLY 1

In your javascript, convert the number to a string before setting as a variable. Use the .toString() method on the variable.

context.setVariable('flow.expirationTime', ExpirationTimeSet.toString());

or use .toFixed(0) to get a string that looks like an integer

context.setVariable('flow.expirationTime', ExpirationTimeSet.toFixed(0));