Last active
July 31, 2018 16:37
-
-
Save dobesv/fd70974421b891b3f277 to your computer and use it in GitHub Desktop.
Login script for custom auth0 database to auth via WordPress XML-RPC
This file contains hidden or 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 login (email, password, callback) { | |
| var xpath = require('xpath') | |
| , xmldom = require('xmldom') | |
| , xml2js = require('xml2js'); | |
| var dom = xmldom.DOMParser; | |
| var builder = new xml2js.Builder(); | |
| var reqXml = builder.buildObject({'methodCall':{ | |
| 'methodName':'wp.getProfile', | |
| 'params':[ | |
| {'param':{'value':{'i4':0}}}, | |
| {'param':{'value':email}}, | |
| {'param':{'value':password}} | |
| ] | |
| }}); | |
| request.post({ | |
| url: 'https://www.menoraway.com/xmlrpc.php', | |
| body: reqXml, | |
| encoding: 'utf8', | |
| method: 'POST', | |
| headers: { 'Content-Type' : 'application/xml' } | |
| }, function (err, response, body) { | |
| if (err) return callback(err); | |
| var parser = new xml2js.Parser(); | |
| return parser.parseString(body, function(err, doc) { | |
| if(err) return callback(err); | |
| //throw new Error(JSON.stringify(doc)); | |
| var mr = doc.methodResponse; | |
| if('fault' in mr) { | |
| if(mr.fault[0].value[0].struct[0].member[0].value[0].int[0] === '403') { | |
| return callback(new WrongUsernameOrPasswordError(email, mr.fault[0].value[0].struct[0].member[1].value[0])); | |
| } else { | |
| return callback(new Error(mr.fault[0].value[0].struct[0].member[1].value[0])); | |
| } | |
| } else { | |
| var result = { | |
| user_id: null, | |
| username: null, | |
| nickname: null, | |
| email: null, | |
| email_verified: true | |
| }; | |
| mr.params[0].param[0].value[0].struct[0].member.forEach(function(prop) { | |
| if(prop.name[0] in result) { | |
| result[prop.name[0]] = prop.value[0].string[0]; | |
| } | |
| }); | |
| if(result.user_id === null || | |
| result.username === null || | |
| result.nickname === null || | |
| result.email === null) { | |
| return callback(new Error("Faield to parse login response: "+JSON.stringify(doc))); | |
| } | |
| return callback(null, result); | |
| } | |
| }); | |
| }); | |
| } |
Author
Yes, that's right. It will refer back to WordPress for any username/password user it doesn't recognize.
Author
I just fixed a problem with the script where it wasn't returning a username and changed it to use some libraries to avoid possible encode/decode issues. New and improved!
Author
Also now I added the "getByEmail" script. You'll have to provide login information for an admin user.
Where does this script go in Auth0? Rules? Apps?
What does the getByEmail script do? Is it required?
If we are blocking Brute Force XMLRPC calls, is this going to work?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you use this custom script to import your wordpress users to your Auth0 account, right?