Saturday, 1 December 2018

Open Authorization code flow popup, get token and use it on same button click

I am working on a ServiceNow integration with a third party system. Using this integration a user can get the data from a third party system and use it to create a Service Request ticket. For this integration, we have created a widget on the Service Portal. This integration uses the authorization code flow for the authentication.

The widget is part of a catalog item which a user can request from the Service Portal. So the flow is like:

  1. User logins to Service Portal and selects the item.

  2. User selects a record in a reference field on the item. This item also has a button, on which when a user clicks he is presented with an authorization popup, where third party login UI appears. He enters his credentials for the third party system. If the authentication is successful, popup closes and tokens are saved in the 'oauth_credential' ServiceNow table.

What happens behind the scene is I am using the ServiceNow 'Get OAuth Token' functionality from the ServiceNow 'REST message' module. See below:

HTML

 <button type="button" ng-click="checkAccess()" class="btn btn-default">Check Access</button>

Client Script

$scope.authorize = function() {
  glideUserSession.loadCurrentUser().then(function(currentUser) {
    var oauthRequestorSysId = currentUser.userID;
    var oauthRequestorContext = 'sys_user';
    var oauthProfileId = '59f05d7ddbf563003ca7da11cf961962';
    var redirect = "https://dev000.service-now.com/oauth_redirect.do";

    var oauth_initiator_url = '/oauth_initiator.do' +
    '?oauth_requestor_context=' + oauthRequestorContext +
    '&oauth_requestor=' + oauthRequestorSysId +
    '&oauth_provider_profile=' + oauthProfileId +
    '&response_type=code';

     window.open(oauth_initiator_url, "", "height=500,width=800");

     // additional code to use token once popup closes
  });
}

I am able to achieve this till now. This popup saves the tokens into the ServiceNow.

Issue:

Once the authorization happens and popup up closes, the page refreshes. I don't want the page to refresh because the earlier selection made by user in the reference field is lost on page refresh. I have more code in the same authorize function, where I will use the token generated by the authorization code pop up. But the issue is, if I add the code after popup, the popup opens up, but then it pauses and the next line of code gets executed. It seems like popup is asynchronous. Additional code functionality uses the token generated by popup to get data from the third party system. I can't create two buttons, one for authorize and other one to use the tokens. I have to do this in a single button click.

Can I stop the page refresh once popup is closed? Can I add some logic after popup window line and this logic should be executed only once popup is closed.



from Open Authorization code flow popup, get token and use it on same button click

No comments:

Post a Comment