global variables value not passed to functions.

cnshum
Tera Contributor

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

Peter Bodelier
Giga Sage

Hi @cnshum 

 

How are you calling these functions?


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

The sequence of execution is

 

1. preprocessAuthCode

2. preprocessAccessToken

3. postprocessAccessToken

4. Savetoken

Hi @cnshum 

 

Please share the script with which you are calling the functions.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi I don't understand what you mean by script. This is the script. So I am implementing private jet authentication using this oauth API script that I shared in the post. 

 

1. preprocessAuthCode - prepares the request parameters before it is sent to the authorization endpoint 

 

2. preprocessAccessToken - prepares the request parameters before it is send to the token endpoint to exchange for access tokens

 

3. postprocessAccessToken - retrieves the response from token endpoint ( id_tokens and access tokens are returned here)

 

4. Savetoken - saves the id_tokens and access tokens