Skip to content

Instantly share code, notes, and snippets.

@stefanneculai
Last active December 19, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanneculai/5890109 to your computer and use it in GitHub Desktop.
Save stefanneculai/5890109 to your computer and use it in GitHub Desktop.
Generate Disqus SSO payload using Ruby
require 'rubygems'
require 'base64'
require 'cgi'
require 'openssl'
require "json"
DISQUS_SECRET_KEY = '<YOUR_SECRET_KEY>'
DISQUS_PUBLIC_KEY = '<YOUR_PUBLIC_KEY>'
def get disqus_sso(user)
# create a JSON packet of our data attributes
data = {
'id' => user['id'],
'username' => user['username'],
'email' => user['email']
}.to_json
# encode the data to base64
message = Base64.encode64(data).gsub("\n", "")
# generate a timestamp for signing the message
timestamp = Time.now.to_i
# generate our hmac signature
sig = OpenSSL::HMAC.hexdigest('sha1', DISQUS_SECRET_KEY, '%s %s' % [message, timestamp])
# return a script tag to insert the sso message
return "<script type=\"text/javascript\">
var disqus_config = function() {
this.page.remote_auth_s3 = \"#{message} #{sig} #{timestamp}\";
this.page.api_key = \"#{DISQUS_PUBLIC_KEY}\";
}
</script>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment