Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arcseldon/e14fcb3a5b65c8e6042ce7a6445c2aa8 to your computer and use it in GitHub Desktop.
Save arcseldon/e14fcb3a5b65c8e6042ce7a6445c2aa8 to your computer and use it in GitHub Desktop.
var async = require('async');
var request = require("request");
var _ = require("underscore");
module.exports = function(ctx, cb) {
const api_url = 'https://TENANT.auth0.com/api/v2/device-credentials';
var access_token;
var user_id = ctx.body.user_id;
async.waterfall([
// STEP 1: obtain access_token to call Management API v2
function get_access_token(done){
console.log('Getting access token...');
var options = { method: 'POST',
url: 'https://TENANT.auth0.com/oauth/token',
json: {
client_id: context.secrets.CLIENT_ID,
client_secret: context.secrets.CLIENT_SECRET,
audience: 'https://TENANT.auth0.com/api/v2/',
grant_type: 'client_credentials'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
access_token = JSON.parse(body).access_token;
done(null, access_token);
});
},
// STEP 2: GET device_credentials via Management API v2
function get_device_credentials(access_token, done){
console.log('Getting device credentials...');
var options = {
url: api_url,
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + access_token
},
qs: {
type: 'refresh_token',
user_id: user_id
}
};
request(options, function(error, response, body){
if (error) throw new Error(error);
done(null, body);
});
},
function delete_all_device_credentials(body, done){
console.log('Deleting refresh tokens...');
_.each(JSON.parse(body), function(device_credential){
console.log('deleting device credential ', device_credential.id);
var options = {
method: 'DELETE',
url: api_url + '/' + device_credential.id,
headers: {
'Authorization': 'Bearer ' + access_token
}
}
request(options, function(error, response, body){
if(error) throw new Error(error);
console.log('REFRESH TOKEN DELETED ', device_credential.id);
});
});
cb(null, { result: 'all refresh tokens deleted'});
}
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment