Skip to content

Instantly share code, notes, and snippets.

@derekseymour
Last active June 17, 2020 15:59
Show Gist options
  • Save derekseymour/26a6fe573c1274642976 to your computer and use it in GitHub Desktop.
Save derekseymour/26a6fe573c1274642976 to your computer and use it in GitHub Desktop.
Freshdesk Single Sign On URL - Node.js
var crypto = require('crypto');
/**
* Generates and returns a Freshdesk Single Sign On URL
* {@link https://gist.github.com/derekseymour/26a6fe573c1274642976 Gist}
*
* @author Derek Seymour <derek@rocketideas.com>
* @param {String} name - The name of the user logging in.
* @param {String} email - A valid email address to associate with the user.
* @param {String} [redirect_to] - An optional URL to redirect to after logging in.
* @returns {String} Freshdesk SSO URL.
*/
function getSSOUrl(name, email, redirect_to) {
var freshdesk_secret = '____Place your Single Sign On Shared Secret here____';
var freshdesk_base_url = 'http://{{your-account}}.freshdesk.com';
var timestamp = Math.floor(new Date().getTime() / 1000).toString();
var hmac = crypto.createHmac('md5', freshdesk_secret);
hmac.update(name + email + timestamp);
var hash = hmac.digest('hex');
return freshdesk_base_url + '/login/sso/' +
'?name=' + escape(name) +
'&email=' + escape(email) +
'&timestamp=' + escape(timestamp) +
'&hash=' + escape(hash) +
( typeof(redirect_to) === 'string' ? '&redirect_to=' + escape(redirect_to) : '' );
}
// Example
console.log(getSSOUrl('John Smith', 'user@example.com')); // Under express, use something like res.redirect(getSSOUrl('Name', 'email'));
@chrisrocks
Copy link

actually, the secret is missing according to the documentation:
hmac.update(name + freshdesk_secret + email + timestamp);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment