Repository: auth0/node-auth0 (ts)

Issue:
impersonate call in v2 doesn't seem to work
I tried to call the impersonate call in the v2 branch and I always get a 401.  Looking at the code and at the documentation, it looks like it is missing the Bearer header.


Pull request description:
Fix impersonation to send AT in the params.
Fixes #59

Patch:
diff --git a/src/auth/UsersManager.js b/src/auth/UsersManager.js
index 6307d1287..cbd417e59 100644
--- a/src/auth/UsersManager.js
+++ b/src/auth/UsersManager.js
@@ -119,13 +119,14 @@ UsersManager.prototype.getInfo = function (accessToken, cb) {
  * @param   {Object}    settings                  Impersonation settings.
  * @param   {String}    settings.impersonator_id  Impersonator user ID.
  * @param   {String}    settings.protocol         The authentication protocol.
+ * @param   {String}    settings.token            API v1 token obtained for impersonation
+ * @param   {String}    [settings.clientId]       Client id used for impersonation. Uses the one supplied in the constructor by default.
  * @param   {Function}  [cb]]                     Callback function.
  *
  * @return  {Promise|undefined}
  */
 UsersManager.prototype.impersonate = function (userId, settings, cb) {
   var url = this.baseUrl + '/users/' + userId + '/impersonate';
-  var data = extend({ client_id: this.clientId }, settings);
 
   if (userId === null || userId === undefined) {
     throw new ArgumentError('You must specify a user ID');
@@ -149,10 +150,17 @@ UsersManager.prototype.impersonate = function (userId, settings, cb) {
     throw new ArgumentError('protocol field is required');
   }
 
+  if (typeof settings.token !== 'string'
+      || settings.token.trim().length === 0) {
+    throw new ArgumentError('token field is required');
+  }
+
+  var data = extend({ client_id: settings.clientId || this.clientId }, settings);
+  var headers = extend({'Authorization': `Bearer ${settings.token}`}, this.headers);
   // Perform the request.
   var promise = getRequestPromise({
     method: 'POST',
-    headers: this.headers,
+    headers: headers,
     data: data,
     url: url
   });


Test patch:
diff --git a/test/auth/users-manager.tests.js b/test/auth/users-manager.tests.js
index 59af3137c..c2fafde1e 100644
--- a/test/auth/users-manager.tests.js
+++ b/test/auth/users-manager.tests.js
@@ -166,6 +166,7 @@ describe('UsersManager', function () {
 
   describe('#impersonate', function () {
     var USER_ID = encodeURIComponent('github|12345');
+    var token = 'API V1 TOKEN';
     var manager = new UsersManager(options);
     var path = '/users/' + USER_ID + '/impersonate';
 
@@ -207,6 +208,16 @@ describe('UsersManager', function () {
         .to.throw(ArgumentError, 'impersonator_id field is required');
     });
 
+    it('should require a token', function () {
+      var settings = {
+        impersonator_id: 'auth0|12345',
+        protocol: 'oauth2'
+      };
+      var impersonate = manager.impersonate.bind(manager, USER_ID, settings);
+
+      expect(impersonate)
+        .to.throw(ArgumentError, 'token field is required');
+    });
 
     it('should require a protocol', function () {
       var settings = {
@@ -222,7 +233,8 @@ describe('UsersManager', function () {
     it('should accept a callback', function (done) {
       var settings = {
         impersonator_id: 'auth0|12345',
-        protocol: 'oauth2'
+        protocol: 'oauth2',
+        token: token
       };
 
       manager.impersonate(USER_ID, settings, done.bind(null, null));
@@ -232,7 +244,8 @@ describe('UsersManager', function () {
     it('should not return a promise when a callback is provided', function () {
       var settings = {
         impersonator_id: 'auth0|12345',
-        protocol: 'oauth2'
+        protocol: 'oauth2',
+        token: token
       };
       var returnValue = manager.impersonate(
         USER_ID,
@@ -248,7 +261,8 @@ describe('UsersManager', function () {
     it('should return a promise when no callback is provided', function () {
       var settings = {
         impersonator_id: 'auth0|12345',
-        protocol: 'oauth2'
+        protocol: 'oauth2',
+        token: token
       };
       var returnValue = manager.impersonate(USER_ID, settings);
 
@@ -261,7 +275,8 @@ describe('UsersManager', function () {
       var request = this.request;
       var settings = {
         impersonator_id: 'auth0|12345',
-        protocol: 'oauth2'
+        protocol: 'oauth2',
+        token: token
       };
 
       manager
@@ -281,7 +296,8 @@ describe('UsersManager', function () {
 
       var settings = {
         impersonator_id: 'auth0|12345',
-        protocol: 'oauth2'
+        protocol: 'oauth2',
+        token: token
       };
       var request = nock(BASE_URL)
         .post(path, function (body) {
@@ -306,7 +322,8 @@ describe('UsersManager', function () {
       var settings = {
         impersonator_id: 'auth0|12345',
         protocol: 'oauth2',
-        client_id: 'OVERRIDEN_CLIENT_ID'
+        clientId: 'OVERRIDEN_CLIENT_ID',
+        token: token
       };
       var request = nock(BASE_URL)
         .post(path, function (body) {
@@ -331,6 +348,7 @@ describe('UsersManager', function () {
       var settings = {
         impersonator_id: 'auth0|12345',
         protocol: 'oauth2',
+        token: token
       };
       var request = nock(BASE_URL)
         .post(path)
@@ -347,6 +365,28 @@ describe('UsersManager', function () {
         });
     });
 
+    it('should use the authorization header', function (done) {
+      nock.cleanAll();
+
+      var settings = {
+        impersonator_id: 'auth0|12345',
+        protocol: 'oauth2',
+        token: token
+      };
+      var request = nock(BASE_URL)
+        .post(path)
+        .matchHeader('Authorization', `Bearer ${token}`)
+        .reply(200);
+
+      manager
+        .impersonate(USER_ID, settings)
+        .then(function () {
+          expect(request.isDone())
+            .to.be.true;
+
+          done();
+        });
+    });
 
     it('should allow the user to add additional parameters', function (done) {
       nock.cleanAll();
@@ -356,7 +396,8 @@ describe('UsersManager', function () {
         protocol: 'oauth2',
         additionalParameters: {
           response_type: 'code'
-        }
+        },
+        token: token
       };
       var request = nock(BASE_URL)
         .post(path, function (body) {