How to attach policy in javascript?

Not applicable

Hi,

My requirement is to attach OAUTH policy according to content of request payload.

request payload is:

{

"Url" : "abc.xyz.com",

"data" : { }

}

So I have created a javascript and attached with preflow.

my javaScript is as below

var getPayload = context.getVariable('request.content');
print("getPayload::::" +getPayload);
var str = context.getVariable("getPayload.Url");
var n = false;
if(str !== null){
 var n = str.includes("xyz");
}
if(n)
print("n::::" +  n); // I want to call/attach OAUTH policy here
else{
print("n::::"+n); // No authentication
}
Solved Solved
0 2 58
1 ACCEPTED SOLUTION

Hi @neha ,

You cannot attach or detach policies using javascript. But, you can achieve the same result using the combination of javascript & policy conditions.

var getPayload = context.getVariable('request.content');
print("getPayload::::" +getPayload);
var str = context.getVariable("getPayload.Url");
var n = false;
if(str !== null){
 var n = str.includes("xyz");
}
if(n)
  context.setVariable("attachOAuth", true); // I want to call/attach OAUTH policy here
else{
  context.setVariable("attachOAuth", false);
print("n::::"+n); // No authentication
}

In OAuth Policy step, add condition,

<Step>
  <Condition>attachOAuth = true</Condition>
  <Name>OAuth</Name>
</Step>

For more info, please refer docs here. Keep us posted if any.

View solution in original post

2 REPLIES 2

Hi @neha ,

You cannot attach or detach policies using javascript. But, you can achieve the same result using the combination of javascript & policy conditions.

var getPayload = context.getVariable('request.content');
print("getPayload::::" +getPayload);
var str = context.getVariable("getPayload.Url");
var n = false;
if(str !== null){
 var n = str.includes("xyz");
}
if(n)
  context.setVariable("attachOAuth", true); // I want to call/attach OAUTH policy here
else{
  context.setVariable("attachOAuth", false);
print("n::::"+n); // No authentication
}

In OAuth Policy step, add condition,

<Step>
  <Condition>attachOAuth = true</Condition>
  <Name>OAuth</Name>
</Step>

For more info, please refer docs here. Keep us posted if any.

Also, I think you need to do a var jsonPayload = JSON.parse(getPayload) ; var str = jsonPayload.Url; ... since getPayload.Url is not a context variable but just part of the json payload in your example.