Skip to content

Instantly share code, notes, and snippets.

@gillevinz
Created March 11, 2020 12:56
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 gillevinz/a19add5443ba6594a4ce7d14fce2c818 to your computer and use it in GitHub Desktop.
Save gillevinz/a19add5443ba6594a4ce7d14fce2c818 to your computer and use it in GitHub Desktop.
Answers SSO ruby example
require 'openssl'
require 'base64'
def redirect_url()
user_data = {
'id': 'user_id',
'email': 'user_email',
'firstName': 'user_firstName',
'lastName': 'user_lastName',
'timestamp': (Time.now.to_f * 1000).to_i
}
answersRedirectUrl = 'redirect_url'
token = encrypt(user_data.to_json)
answersRedirectUrl + '&token=' + token + '&key=' + 'your_key'
end
def encrypt(string)
salt = OpenSSL::Random.random_bytes(16)
cipher = OpenSSL::Cipher.new('AES-128-CBC')
cipher.encrypt
key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1('your_secret', salt, 1024, cipher.key_len+cipher.iv_len)
key = key_iv[0, cipher.key_len]
iv = key_iv[cipher.key_len, cipher.iv_len]
cipher.key = key
cipher.iv = iv
encrypted = '' << cipher.update(string) << cipher.final
[encrypted, iv, salt].map {|v| ::Base64.urlsafe_encode64(v)}.join("--")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment