Last active
March 18, 2020 21:45
-
-
Save saltukalakus/790f62ee9a845934e020f4c263c86b18 to your computer and use it in GitHub Desktop.
Custom DB Wordpress 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
/* globals require, configuration */ | |
/** | |
* This script will be executed when the user wishes to change their password to test if the user exists. | |
* This needs a global configuration option with the following properties: | |
* {string} endpointUrl - Site URL with an empty "a0_action" parameter appended. | |
* {string} migrationToken - Migration token found in the plugin settings | |
* {string} userNamespace - Formatted site name to avoid user ID overlapping. | |
* | |
* @param {string} email - User email address, provided on login. | |
* @param {function} callback - Function to call when the script has completed. | |
*/ | |
function getByEmail(email, callback) { | |
var request = require("request"); | |
// to use Auth0 search API, first we need a management API `access_token` | |
var tools = require("auth0-extension-tools@1.3.1"); | |
tools.managementApi | |
.getClient({ | |
domain: configuration.Domain, | |
clientId: configuration.GetUser_Client_ID, | |
clientSecret: configuration.GetUser_Client_Secret | |
}) | |
.then(function(client) { | |
var params = { | |
q: | |
'email:"' + | |
email + | |
'" AND identities.connection:"' + | |
configuration.Connection + | |
'" !app_metadata.migration_complete:true' | |
}; | |
client.users.getAll(params, function(err, users) { | |
if (err) { | |
return callback(err); | |
} else if (Array.isArray(users) && users.length > 0) { | |
var profile = {}; | |
var openidProfile = users[0]; | |
profile.name = openidProfile.name || ""; | |
profile.nickname = openidProfile.nickname || ""; | |
profile.email = openidProfile.email; | |
profile.email_verified = openidProfile.email_verified || false; | |
profile.user_id = openidProfile.user_id.replace( | |
/^auth0/, | |
configuration.userNamespace | |
); | |
profile.user_metadata = openidProfile.user_metadata || {}; | |
profile.app_metadata = openidProfile.app_metadata || {}; | |
console.log("profile"); | |
return callback(null, profile); | |
} else { | |
request.post( | |
configuration.endpointUrl + "migration-ws-get-user", | |
{ | |
form: { | |
username: email, | |
access_token: configuration.migrationToken | |
} | |
}, | |
function(error, response, body) { | |
// Error encountered during HTTP request, exit. | |
if (error) { | |
return callback(error); | |
} | |
var wpUser = JSON.parse(body); | |
// Error returned from WordPress or no data, exit. | |
if (wpUser.error || !wpUser.data) { | |
return callback(null); | |
} | |
// Use WordPress profile data to populate Auth0 account. | |
var profile = { | |
user_id: configuration.userNamespace + "|" + wpUser.data.ID, | |
email: wpUser.data.user_email, | |
name: wpUser.data.display_name, | |
email_verified: true | |
}; | |
callback(null, profile); | |
} | |
); | |
} | |
}); | |
}); | |
} |
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
/* globals require, configuration */ | |
/** | |
* This script will be executed each time a user attempts to login to a custom database. | |
* This needs a global configuration option with the following properties: | |
* {string} endpointUrl - Site URL with an empty "a0_action" parameter appended. | |
* {string} migrationToken - Migration token found in the plugin settings | |
* {string} userNamespace - Formatted site name to avoid user ID overlapping. | |
* | |
* @param {string} email - User email address, provided on login. | |
* @param {string} password - User password, provided on login. | |
* @param {function} callback - Function to call when the script has completed. | |
*/ | |
function login(email, password, callback) { | |
var request = require("request"); | |
request( | |
{ | |
url: "https://" + configuration.Domain + "/oauth/token", | |
method: "POST", | |
json: { | |
grant_type: "http://auth0.com/oauth/grant-type/password-realm", | |
realm: configuration.Connection, | |
scope: "openid profile email", // todo: add name to scope | |
client_id: configuration.Login_Client_ID, | |
client_secret: configuration.Login_Client_Secret, | |
username: email, | |
password: password | |
}, | |
headers: { "content-type": "application/json" } | |
}, | |
function(error, response, body) { | |
if (error) { | |
return callback(error); | |
} | |
if (response.statusCode === 200) { | |
var profile = {}; | |
var openidProfile = jwt.decode(body.id_token); // jwt_decode | |
profile.name = openidProfile.name || ""; | |
profile.nickname = openidProfile.nickname || ""; | |
profile.email = openidProfile.email; | |
profile.email_verified = openidProfile.email_verified || false; | |
profile.user_id = openidProfile.sub.replace( | |
/^auth0/, | |
configuration.userNamespace | |
); | |
profile.user_metadata = | |
openidProfile["https://migrationapp/user_metadata"] || {}; | |
profile.app_metadata = | |
openidProfile["https://migrationapp/app_metadata"] || {}; | |
return callback(null, profile); | |
} | |
// this is where we're adding a wordpress user | |
request.post( | |
configuration.endpointUrl + "migration-ws-login", | |
{ | |
form: { | |
username: email, | |
password: password, | |
access_token: configuration.migrationToken | |
} | |
}, | |
function(error, response, body) { | |
// Error encountered during HTTP request, exit. | |
if (error) { | |
return callback(error); | |
} | |
var wpUser = JSON.parse(body); | |
// Error returned from WordPress or no data, exit. | |
if (wpUser.error || !wpUser.data) { | |
return callback(null); | |
} | |
// Use WordPress profile data to populate Auth0 account. | |
var profile = { | |
user_id: configuration.userNamespace + "|" + wpUser.data.ID, | |
email: wpUser.data.user_email, | |
name: wpUser.data.display_name, | |
email_verified: true | |
}; | |
return callback(null, profile); | |
} | |
); | |
} | |
); | |
} |
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
function (user, context, callback) { | |
if(context.clientID !== configuration.Login_Client_ID){ | |
return callback(null, user, context); | |
} | |
console.log("client name= " + context.clientName); | |
const namespace = 'https://migrationapp/'; | |
context.idToken[namespace + 'user_metadata'] = user.user_metadata; | |
context.idToken[namespace + 'app_metadata'] = user.app_metadata; | |
callback(null, user, context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment