ms-rest-azure.interactiveLogin callback always returns 'authorization_pending' error (in React Electron)
See original GitHub issueIssue:
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.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Update: A newer version of “ms-rest-azure”: “2.3.5” has been published which takes an explicit dependency on async version 2.5.0 and has the retry logic baked into it. Hope this should resolve the issue. Let us know if you find more issues.
Ah I see. We will investigate further by bumping the version of async. If everything passes then we will take a dependency on newer version of async. Btw, We are working towards creating an isomorphic js library that works in node.js environment and browser too. We have created a new library called ms-rest-nodeauth that focuses on node.js based authentication. This library does not use async library. It is written in typescript with latest ES6 and ES7 features (async/await, promises, etc.). It also supports callback for backwards compatibility. Give it a try and let us know if you have any issues with that.