Created
August 31, 2017 14:35
-
-
Save bryandh/c72e61200d4a4c1283f6722624f4a314 to your computer and use it in GitHub Desktop.
Auth0 login inter-tenant-migration-script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use this script in a connection within the Auth0 Tenant that you want to copy the user TO | |
// Use the variables defined in the login functions to specify the 'other' Auth0 Tenant that you want to copy the users FROM | |
function login (email, password, callback) { | |
// Require and set up all the Auth0 settings | |
var request = require('request'), | |
domain = 'OTHER TENANTS DOMAIN', | |
domain_uri = 'https://' + domain, | |
audience = 'OTHER TENANTS API AUDIENCE', | |
scope = 'openid profile email', | |
client_id = 'OTHER TENANTS CLIENT ID', | |
client_secret = 'OTHER TENANTS CLIENT SECRET', | |
connection = 'OTHER TENANTS CLIENT CONNECTION'; | |
// Request an access token for the given email/password credentials | |
request({ | |
method: 'POST', | |
uri: domain_uri + '/oauth/token', | |
json: { | |
grant_type: 'password', | |
username: email, | |
password: password, | |
audience: audience, | |
scope: scope, | |
client_id: client_id, | |
client_secret: client_secret, | |
connection: connection | |
} | |
}, function (err, response, body) { | |
if (err) return callback(err); | |
if (response.statusCode === 401 || response.statusCode === 403 || response.statusCode === 500) | |
return callback(new Error('statusCode: ' + response.statusCode + '\nerror: ' + body.error + '\nerror_description: ' + body.error_description)); | |
// Request the profile for the requested access token | |
request({ | |
method: 'GET', | |
uri: domain_uri + '/userinfo', | |
auth: { | |
'bearer': body.access_token | |
} | |
}, function(error, response, body) { | |
if (err) return callback(err); | |
if (response.statusCode === 401 || response.statusCode === 403 || response.statusCode === 500) | |
return callback(new Error('statusCode: ' + response.statusCode + ' error: ' + body.error + ' error_description: ' + body.error_description)); | |
var profile = JSON.parse(body); | |
// The automatic insert will prepend the database type before the user_id, therefore the user_id will result in 'databaseType|user_id' after insertion into Auth0 | |
// This means we need to strip off the type and only pass the user_id part | |
profile.user_id = profile.sub.split('|')[1]; | |
// Set the username to the profile's nickname or the username won't be copied over properly | |
profile.username = profile.nickname; | |
// Return the profile to the callback, effectively copying the profile to this Auth0 Connection | |
return callback(null, profile); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment