Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.
This repository was archived by the owner on May 5, 2023. It is now read-only.

ms-rest-azure.interactiveLogin callback always returns 'authorization_pending' error (in React Electron) #2002

@christopheranderson

Description

@christopheranderson

Issue:

When I call interactiveLogin, my callback is almost immediately called and it's given an error: authorization_pending.

This seemed strange to me, because it seems like it normally waits around for a bit when I'm doing a normal Node application, but I'm doing an Electron application and this call is actually taking place in a React component. I dove down through adal-node code, etc., but it doesn't look like they handle retry/etc. I felt like maybe this method should be attempting to retry for me.

Workaround:

I decided the way of getting around this for now is to add retry, so I ended up modifying interactive login to have retry baked into the acquireTokenWithDeviceCode step. Not necessarily the cleanest code, but it works now. :)

  // will retry until a non-pending error is returned or credentials are returned.
  var tryAcquireToken = function (userCodeResponse, callback) {
      self.context.acquireTokenWithDeviceCode(self.environment.activeDirectoryResourceId, self.clientId, userCodeResponse, function (err, tokenResponse) {
        if (err) {
          if(err.error === 'authorization_pending'){
            setTimeout(()=>{
              tryAcquireToken(userCodeResponse, callback);
            }, 1000);
            return
          } else {
            return callback(err);
          }
        }
        self.username = tokenResponse.userId;
        self.authorizationScheme = tokenResponse.tokenType;
        return callback(null);
      });
  }

  async.waterfall([
    //acquire usercode
    function (callback) {
      self.context.acquireUserCode(self.environment.activeDirectoryResourceId, self.clientId, self.language, function (err, userCodeResponse) {
        if (err) return callback(err);
        if (self.userCodeResponseLogger) {
          self.userCodeResponseLogger(userCodeResponse.message);
        } else {
          console.log(userCodeResponse.message);
        }
        return callback(null, userCodeResponse);
      });
    },
    //acquire token with device code and set the username to userId received from tokenResponse.
    tryAcquireToken,

/* nothing else changed from here... */

Not entirely sure if supporting Electron is actually important for ya'll. I'll likely just create a fork or a custom library for my purposes. I also had to create a custom version of adal-node which didn't read in the package.json from a relative directory. Just thought I'd raise this here since it did take a good couple of hours out of my day.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions