Skip to content

Commit

Permalink
feat: add revokeRefreshToken (#579)
Browse files Browse the repository at this point in the history
* feat: add revokeRefreshToken

* feat: add optional options to revokeRefreshToken
  • Loading branch information
andreafspeziale authored Feb 2, 2021
1 parent 3e83451 commit 6be7d2c
Show file tree
Hide file tree
Showing 3 changed files with 375 additions and 11 deletions.
92 changes: 87 additions & 5 deletions docs/auth_TokensManager.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ <h1 class="page-title">auth/TokensManager.js</h1>
* @constructor
* @memberOf module:auth
*
* @param {Object} options Manager options.
* @param {String} options.baseUrl The auth0 account URL.
* @param {String} [options.headers] Default request headers.
* @param {String} [options.clientId] Default client ID.
* @param {Object} options Manager options.
* @param {String} options.baseUrl The auth0 account URL.
* @param {String} [options.headers] Default request headers.
* @param {String} [options.clientId] Default client ID.
* @param {String} [options.clientSecret] Default client Secret.
*/
var TokensManager = function(options) {
if (typeof options !== 'object') {
Expand All @@ -67,6 +68,7 @@ <h1 class="page-title">auth/TokensManager.js</h1>
this.baseUrl = options.baseUrl;
this.headers = options.headers || {};
this.clientId = options.clientId || '';
this.clientSecret = options.clientSecret || '';
};

/**
Expand Down Expand Up @@ -213,6 +215,86 @@ <h1 class="page-title">auth/TokensManager.js</h1>
return promise;
};

/**
* Proactively revoke an issued refresh token.
*
* @method
* @memberOf module:auth.TokensManager.prototype
*
* @example &lt;caption>
* Given an existing refresh token, this endpoint will revoke it in order
* to prevent unauthorized silently user authentication tokens refresh.
* Find more information in the &lt;a href="https://auth0.com/docs/api/authentication#revoke-refresh-token">API Docs&lt;/a>.
* &lt;/caption>
*
* * var data = {
* token: '{REFRESH_TOKEN}'
* };
*
* auth0.tokens.revokeRefreshToken(data, function (err, _) {
* if (err) {
* // Handle error.
* }
*
* // Do stuff.
* });
*
* @param {Object} data Token data object.
* @param {String} data.token User refresh token.
* @param {String} [data.client_id] Target client ID.
* @param {String} [data.client_secret] Target client secret.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
TokensManager.prototype.revokeRefreshToken = function(data, cb) {
if (!data) {
throw new ArgumentError('Missing token data object');
}

var hasToken = typeof data.token === 'string' &amp;&amp; data.token.trim().length !== 0;

if (!hasToken) {
throw new ArgumentError('token property is required');
}

var hasClientId =
(data.client_id &amp;&amp; typeof data.client_id === 'string' &amp;&amp; data.client_id.trim().length !== 0) ||
this.clientId !== '';

if (!hasClientId) {
throw new ArgumentError(
'Neither token data client_id property or constructor clientId property has been set'
);
}

var body = extend(
{
client_id: this.clientId,
client_secret: this.clientSecret
},
data
);

var headers = this.headers;

// Perform the request.
var promise = axios({
method: 'POST',
url: this.baseUrl + '/oauth/revoke',
data: body,
headers: headers
}).then(({ data }) => data);

// Use callback if given.
if (cb instanceof Function) {
promise.then(cb.bind(null, null)).catch(cb);
return;
}

return promise;
};

module.exports = TokensManager;
</code></pre>
</article>
Expand All @@ -226,7 +308,7 @@ <h1 class="page-title">auth/TokensManager.js</h1>
<br class="clear">

<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a> on Thu Jan 21 2021 12:34:05 GMT-0800 (Pacific Standard Time) using the Minami theme.
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a> on Mon Feb 01 2021 15:09:19 GMT+0100 (Ora standard dell’Europa centrale) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
90 changes: 86 additions & 4 deletions src/auth/TokensManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ var ArgumentError = require('rest-facade').ArgumentError;
* @constructor
* @memberOf module:auth
*
* @param {Object} options Manager options.
* @param {String} options.baseUrl The auth0 account URL.
* @param {String} [options.headers] Default request headers.
* @param {String} [options.clientId] Default client ID.
* @param {Object} options Manager options.
* @param {String} options.baseUrl The auth0 account URL.
* @param {String} [options.headers] Default request headers.
* @param {String} [options.clientId] Default client ID.
* @param {String} [options.clientSecret] Default client Secret.
*/
var TokensManager = function(options) {
if (typeof options !== 'object') {
Expand All @@ -26,6 +27,7 @@ var TokensManager = function(options) {
this.baseUrl = options.baseUrl;
this.headers = options.headers || {};
this.clientId = options.clientId || '';
this.clientSecret = options.clientSecret || '';
};

/**
Expand Down Expand Up @@ -172,4 +174,84 @@ TokensManager.prototype.getDelegationToken = function(data, cb) {
return promise;
};

/**
* Proactively revoke an issued refresh token.
*
* @method
* @memberOf module:auth.TokensManager.prototype
*
* @example <caption>
* Given an existing refresh token, this endpoint will revoke it in order
* to prevent unauthorized silently user authentication tokens refresh.
* Find more information in the <a href="https://auth0.com/docs/api/authentication#revoke-refresh-token">API Docs</a>.
* </caption>
*
* * var data = {
* token: '{REFRESH_TOKEN}'
* };
*
* auth0.tokens.revokeRefreshToken(data, function (err, _) {
* if (err) {
* // Handle error.
* }
*
* // Do stuff.
* });
*
* @param {Object} data Token data object.
* @param {String} data.token User refresh token.
* @param {String} [data.client_id] Target client ID.
* @param {String} [data.client_secret] Target client secret.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
TokensManager.prototype.revokeRefreshToken = function(data, cb) {
if (!data) {
throw new ArgumentError('Missing token data object');
}

var hasToken = typeof data.token === 'string' && data.token.trim().length !== 0;

if (!hasToken) {
throw new ArgumentError('token property is required');
}

var hasClientId =
(data.client_id && typeof data.client_id === 'string' && data.client_id.trim().length !== 0) ||
this.clientId !== '';

if (!hasClientId) {
throw new ArgumentError(
'Neither token data client_id property or constructor clientId property has been set'
);
}

var body = extend(
{
client_id: this.clientId,
client_secret: this.clientSecret
},
data
);

var headers = this.headers;

// Perform the request.
var promise = axios({
method: 'POST',
url: this.baseUrl + '/oauth/revoke',
data: body,
headers: headers
}).then(({ data }) => data);

// Use callback if given.
if (cb instanceof Function) {
promise.then(cb.bind(null, null)).catch(cb);
return;
}

return promise;
};

module.exports = TokensManager;
Loading

0 comments on commit 6be7d2c

Please sign in to comment.