Repository: auth0/node-auth0
Pull request #153 (2017-01-18 21:25:02)

Support refresh_token in getDelegationToken
The delegation token endpoint accepts both a refresh token and an id_token. This PR updates the getDelegationToken method to support refresh_token


Patch:
diff --git a/src/auth/TokensManager.js b/src/auth/TokensManager.js
index c77fab866..247e17cb1 100644
--- a/src/auth/TokensManager.js
+++ b/src/auth/TokensManager.js
@@ -117,12 +117,13 @@ TokensManager.prototype.getInfo = function (idToken, cb) {
  *   console.log(token);
  * });
  *
- * @param   {Object}    data              Token data object.
- * @param   {String}    data.id_token     User ID token.
- * @param   {String}    data.target       Target client ID.
- * @param   {String}    data.api_type     The API to be used (aws, auth0, etc).
- * @param   {String}    data.grant_type   Grant type (password, jwt, etc).
- * @param   {Function}  [cb]              Callback function.
+ * @param   {Object}    data                Token data object.
+ * @param   {String}    data.id_token       User ID token.
+ * @param   {String}    data.refresh_token  User refresh token.
+ * @param   {String}    data.target         Target client ID.
+ * @param   {String}    data.api_type       The API to be used (aws, auth0, etc).
+ * @param   {String}    data.grant_type     Grant type (password, jwt, etc).
+ * @param   {Function}  [cb]                Callback function.
  *
  * @return  {Promise|undefined}
  */
@@ -134,9 +135,18 @@ TokensManager.prototype.getDelegationToken = function (data, cb) {
     throw new ArgumentError('Missing token data object');
   }
 
-  if (typeof data.id_token !== 'string'
-      || data.id_token.trim().length === 0) {
-    throw new ArgumentError('id_token field is required');
+  var hasIdToken = typeof data.id_token === 'string'
+      && data.id_token.trim().length !== 0;
+
+  var hasRefreshToken = typeof data.refresh_token === 'string'
+      && data.refresh_token.trim().length !== 0;
+
+  if (!hasIdToken && !hasRefreshToken) {
+    throw new ArgumentError('one of id_token or refresh_token is required');
+  }
+
+  if (hasIdToken && hasRefreshToken) {
+    throw new ArgumentError('id_token and refresh_token fields cannot be specified simulatenously');
   }
 
   if (typeof data.target !== 'string'


Test patch:
diff --git a/test/auth/tokens-manager.tests.js b/test/auth/tokens-manager.tests.js
index bbae1b89c..9f1b298a1 100644
--- a/test/auth/tokens-manager.tests.js
+++ b/test/auth/tokens-manager.tests.js
@@ -182,12 +182,21 @@ describe('TokensManager', function () {
     });
 
 
-    it('should require an ID token' ,function () {
+    it('should require an ID token or refresh token' ,function () {
       var data = {};
       var getDelegationToken = manager.getDelegationToken.bind(manager, data);
 
       expect(getDelegationToken)
-        .to.throw(ArgumentError, 'id_token field is required');
+        .to.throw(ArgumentError, 'one of id_token or refresh_token is required');
+    });
+
+
+    it('should not accept an ID token and a refresh token simulatenously' ,function () {
+      var data = { id_token: 'foo', refresh_token: 'bar'};
+      var getDelegationToken = manager.getDelegationToken.bind(manager, data);
+
+      expect(getDelegationToken)
+        .to.throw(ArgumentError, 'id_token and refresh_token fields cannot be specified simulatenously');
     });