global variables value not passed to functions.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2023 02:02 AM
I have this oauth script. I have declared (this.authCode and this.idToken) as global variables in the initialize function to be used in other functions in this script. Now the problem is that when the "saveToken" function is ran, the value of this.idToken is being read correctly but the this.authCode is empty. What am i missing out here?
var OAuthUtil_Pvt_Jwt_Custom = Class.create();
OAuthUtil_Pvt_Jwt_Custom.prototype = {
initialize: function(oauthContext) {
this.oauthContext = oauthContext;
this.authCode = "";
this.idToken = "";
},
interceptRequestParameters: function(requestParamMap) {
// Add/Modify request parameters if needed
this.preprocessAccessToken(requestParamMap);
},
parseTokenResponse: function(accessTokenResponse) {
this.postprocessAccessToken(accessTokenResponse);
},
preprocessAuthCode: function(requestParamMap) {
// ******** Add Custom Parameters for Authorization URL ***********
// Example for adding a sample nonce param.
//this.oauthContext.addQueryParameter("nonce", "2");
},
preprocessAccessToken: function(requestParamMap) {
var jwtAPI = new sn_auth.GlideJWTAPI();
// sys_id of the JWT Provider record.
var jwtToken = jwtAPI.generateJWT("1234", "{}", "{}");
gs.info("###JWT_Token - " + jwtToken);
var profGr = this.oauthContext.getOAuthProfile();
var redirectURL = profGr.oauth_entity.redirect_url;
requestParamMap.put("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
requestParamMap.put("client_assertion", jwtToken);
requestParamMap.put("redirect_uri", redirectURL);
requestParamMap.put("grant_type", "authorization_code");
gs.info('this is the auth code: ' + requestParamMap.get("code"));
requestParamMap.put("code", requestParamMap.get("code"));
//remove params not sent to the token endpoint here
requestParamMap.remove('client_id');
requestParamMap.remove('client_secret');
//save the authorisation code to global variable authCode
this.authCode = requestParamMap.get("code");
gs.info("authcode in preprocessAccessToken : " + this.authCode);
var strKeys = '' + requestParamMap.getKeys();
if (strKeys) {
var keys = strKeys.split(',');
for (var i = 0; i < keys.length; i++) {
gs.info('request param: ' + keys[i] + ': ' + requestParamMap.get(keys[i]));
}
}
},
postprocessAccessToken: function(accessTokenResponse) {
var contentType = accessTokenResponse.getContentType();
var contentBody = accessTokenResponse.getBody();
gs.info("contentBody: " + contentBody);
gs.info("contentType: " + contentType);
if (contentType && contentType.indexOf('application/json') != -1) {
var tokenResponse = (new global.JSON()).decode(accessTokenResponse.getBody());
var paramMap = accessTokenResponse.getparameters();
for (param in tokenResponse) {
gs.info("response param : " + param + " : " + tokenResponse[param].toString());
paramMap.put(param, tokenResponse[param].toString());
}
// for (param in tokenResponse) {
// if (param == 'id_token') {
// paramMap.put("access_token", tokenResponse[param].toString());
// } else {
// paramMap.put(param, tokenResponse[param].toString());
// }
// }
var strKeys = '' + paramMap.getKeys();
if (strKeys) {
var keys = strKeys.split(',');
for (var i = 0; i < keys.length; i++) {
gs.info('response param: ' + keys[i] + ': ' + paramMap.get(keys[i]));
}
}
//save the id_token to global variable idToken
this.idToken = paramMap.get("id_token");
//extract the global variable idToken and authCode in saveToken function
this.saveToken();
}
},
saveToken: function() {
gs.info("running saveToken");
gs.info("id_token is " + this.idToken);
gs.info("code is " + this.authCode);
},
type: 'OAuthUtil_Pvt_Jwt_Custom'
};
5 REPLIES 5

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2023 07:27 AM
Apologies, I thought we where talking about a script include, therefore I was wondering how you where calling the script include.
I think you're overriding yourself with this line:
requestParamMap.put("code", requestParamMap.get("code"));
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.