Skip to content

Instantly share code, notes, and snippets.

@woloski
Last active December 18, 2015 23:49
Show Gist options
  • Save woloski/5864366 to your computer and use it in GitHub Desktop.
Save woloski/5864366 to your computer and use it in GitHub Desktop.
Auth0 script for WCF Membership wrapper
  1. Create a new Database Connection in Auth0 Dashboard
  2. Give it an arbitrary name (myapp.com)
  3. Delete the content and paste the content of the script
  4. Replace the BASE_URL with yours
  5. Click on Try button and check if it works
  6. You can now use the Login Widget through https://YOUR_SUBDOMAIN.auth0.com/login?client=YOUR_CLIENT_ID (both values can be found on Settings)
function login (username, password, callback) {
// TODO: replace with your base url (use HTTPS on production)
var BASE_URL = 'http://myserver/services';
loginUser(function(err, user) {
if (err) return callback(err);
if (!user) return callback(); // unauthorized
getProfile(function(err, profile) {
if (err) return callback(err);
Object.keys(profile).forEach(function(k) { user[k] = profile[k]; } );
user.id = user.ProviderUserKey;
delete user.ProviderUserKey;
user.displayName = user.FirstName + user.LastName;
user.name = {
givenName: user.FirstName,
familyName: user.LastName
};
delete user.FirstName;
delete user.LastName;
user.emails = user.UpdateAndAlertEmailAddresses.split(';');
getRoles(function(err, roles) {
if (err) return callback(err);
user.roles = roles;
return callback(null, user);
});
});
});
function loginUser(callback) {
var loginRequest = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' +
'<s:Body><Login xmlns="http://asp.net/ApplicationServices/v200">' +
'<username>' + username + '</username>' +
'<password>' + password +' </password>' +
'<customCredential/>' +
'<isPersistent>true</isPersistent>' +
'</Login></s:Body>' +
'</s:Envelope>';
request.post({
url: BASE_URL + '/AuthenticationService.svc',
body: loginRequest,
headers: { 'Content-Type': 'text/xml; charset=utf-8',
'SOAPAction': 'http://asp.net/ApplicationServices/v200/AuthenticationService/Login' }
}, function (err, response, body) {
if (err) return callback(err);
// test if user/password is valid
if (/<LoginResult>true<\/LoginResult>/.test(body)) return callback(null, { name: username } );
});
}
function getRoles(callback) {
var rolesRequest = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetRolesForCurrentUser xmlns="http://asp.net/ApplicationServices/v200"/></s:Body></s:Envelope>';
request.post({
url: BASE_URL + '/RoleService.svc',
body: rolesRequest,
headers: { 'Content-Type': 'text/xml; charset=utf-8',
'SOAPAction': 'http://asp.net/ApplicationServices/v200/RoleService/GetRolesForCurrentUser' }
}, function (err, response, body) {
if (err) return callback(err);
var parser = new xmldom.DOMParser();
var doc = parser.parseFromString(body);
var roles = xpath.select("//*[local-name(.)='string']", doc).map(function(node) { return node.textContent; });
return callback(null, roles);
});
}
function getProfile(callback) {
var profileRequest = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetAllPropertiesForCurrentUser xmlns="http://asp.net/ApplicationServices/v200"><authenticatedUserOnly>true</authenticatedUserOnly></GetAllPropertiesForCurrentUser></s:Body></s:Envelope>';
request.post({
url: BASE_URL + '/ProfileService.svc',
body: profileRequest,
headers: { 'Content-Type': 'text/xml; charset=utf-8',
'SOAPAction': 'http://asp.net/ApplicationServices/v200/ProfileService/GetAllPropertiesForCurrentUser' }
}, function (err, response, body) {
if (err) return callback(err);
var parser = new xmldom.DOMParser();
var doc = parser.parseFromString(body);
var profile = { };
var props = xpath.select("//*[local-name(.)='KeyValueOfstringanyType']", doc)
.forEach(function(prop) { profile[prop.childNodes[0].textContent.split('.')[1]] = prop.childNodes[1].textContent; });
callback(null, profile);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment