Last active
December 19, 2015 03:29
-
-
Save stefanneculai/5890109 to your computer and use it in GitHub Desktop.
Generate Disqus SSO payload using Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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