Skip to content

Instantly share code, notes, and snippets.

@shivabhusal
Last active November 19, 2015 03:01
Show Gist options
  • Save shivabhusal/679f3c786e51275f5e4b to your computer and use it in GitHub Desktop.
Save shivabhusal/679f3c786e51275f5e4b to your computer and use it in GitHub Desktop.
A OpenSource Object Oriented Ruby Library to implement SingleSign On into FreshDesk. This basically gets you the redirection_url you require to implement singlesignon

How to use it

It is very easy to use this library. Just use the get_redirection_url method

def help_desk_url
    options = {
        username: user_name,
        email: email,
        role: role
    }
    SsoService.init(options).get_redirection_url
    # or
    # SsoService.new(options).get_redirection_url
end
class SsoService
CONFIG = {
child: {redirection_url: 'https://url-to-your-portal1.com', landing_page: '/support/tickets'},
parent: {redirection_url: 'https://url-to-your-portal2.com', landing_page: '/support/tickets'}
}
def initialize(params)
@role = params[:role].to_sym rescue :parent
@username = params[:username] rescue ''
@email = params[:email] rescue ''
@sso_secret = ENV['FRESHDESK_SSO_SECRET_KEY'] rescue ''
end
# A factory to generate objects
# @return [SsoService]
def self.init(params)
self.new(params)
end
# Gets URL along with params required to authenticate to FreshDesk
# @return [String]
def get_redirection_url
sso_domain + "/login/sso?name=" + username +
"&email=" + email +
"&timestamp=" + time_in_utc +
"&hash=" + gen_hash_from_params_hash +
"&redirect_to=" + get_landing_url
end
private
attr_reader :username, :role, :sso_secret
# Sanitizes the email if it has any `+` like characters
# @return [String]
def email
@email.gsub('+', '')
end
# URL to which the USER is redirected
# in order to get logged in to the FreshDesk portal
# @return [String]
def sso_domain
CONFIG[role][:redirection_url] rescue CONFIG[:parent][:redirection_url]
end
# URL to which the USER is redirected
# after logged into the FreshDesk portal
# @return [String]
def get_landing_url
CONFIG[role][:landing_page] rescue CONFIG[:parent][:landing_page]
end
# Gets MD5 HASH from some unique keys to be passed to FreshDesk
# @return [String] A Digest
def gen_hash_from_params_hash
digest = OpenSSL::Digest::Digest.new('MD5')
OpenSSL::HMAC.hexdigest(digest, sso_secret, url_params)
end
# Gets URL params in GET format
# @return [String]
def url_params
"#{username}#{email}#{time_in_utc}"
end
# Gets time in UTC for FreshDesk to compare with
# @return [Time]
def time_in_utc
Time.now.getutc.to_i.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment