Skip to content

Instantly share code, notes, and snippets.

@loganvolkers
Last active September 8, 2016 04:40
Show Gist options
  • Save loganvolkers/83db8b04049b7eb9bc649d14647da5dc to your computer and use it in GitHub Desktop.
Save loganvolkers/83db8b04049b7eb9bc649d14647da5dc to your computer and use it in GitHub Desktop.
Auth0 Custom Connection Script
function doAuth(access_token, ctx, callback) {
var opts = {
'headers': {
'Authorization': 'OAuth ' + access_token,
'Accept': 'application/json',
'User-Agent': 'Auth0'
}
};
request.get('https://login.mailchimp.com/oauth2/metadata', opts, function(e, r, b) {
if (e) {
return callback(new Error('Failed Mailchimp Metadata Lookup' + e));
}
if (r.statusCode !== 200) {
return callback(new Error('Failed Mailchimp Metadata Lookup StatusCode:' + r.statusCode));
}
try {
var meta = JSON.parse(b);
}
catch (e) {
return callback(new Error('Failed Mailchimp Metadata parsing JSON' + e));
}
request.get(meta.api_endpoint + '/3.0', opts, function(e, r, b) {
if (e) {
return callback(new Error('Failed Mailchimp User/Account Lookup' + e));
}
if (r.statusCode !== 200) {
return callback(new Error('Failed Mailchimp User/Account Lookup StatusCode:' + r.statusCode));
}
try {
var profile = JSON.parse(b);
}
catch (e) {
return callback(new Error('Failed Mailchimp parsing profile JSON' + e + 'body: ' + b));
}
// See: https://auth0.com/docs/user-profile/normalized
var auth0Normalized = {
// "email": profile.email,
// "email_verified": true,
// "family_name": "Foo",
// "gender": "male",
// "given_name": "John",
// "identities": [{
// "provider": "Mailchimp",
// "user_id": profile.account_id,
// "connection": "MailchimpDev",
// "isSocial": true
// }],
// "locale": "en",
"name": profile.account_name,
"nickname": profile.username,
// "picture": "https://lh4.googleusercontent.com/-OdsbOXom9qE/AAAAAAAAAAI/AAAAAAAAADU/_j8SzYTOJ4I/photo.jpg",
"user_id": profile.account_id
};
var final = Object.assign({},profile,auth0Normalized);
callback(null, final);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment