-
-
Save n8agrin/72295c9ad29283b6e02908b5d6837529 to your computer and use it in GitHub Desktop.
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 'securerandom' | |
| module OmniEmbedSdk | |
| def self.dashboard_url(content_id:, external_id:, domain: 'embed-omniapp.co', name:, nonce:nil, org_name:, port: nil, secret:, user_attributes:nil) | |
| login_url = "https://#{org_name}.#{domain}#{port ? ":#{port}" : ''}/embed/login" | |
| content_path = "/embed/dashboards/#{content_id}" | |
| nonce ||= SecureRandom.alphanumeric(32) | |
| signature = sign(login_url: login_url, content_path: content_path, external_id: external_id, name: name, nonce: nonce, secret: secret, user_attributes: user_attributes) | |
| query_params = { | |
| :contentPath => content_path, | |
| :externalId => external_id, | |
| :name => name, | |
| :nonce => nonce, | |
| :signature => signature | |
| } | |
| query_params[:userAttributes] = JSON.generate(user_attributes) if user_attributes | |
| URI::HTTPS.build( | |
| :host => "#{org_name}.#{domain}", | |
| :port => port, | |
| :path => "/embed/login", | |
| :query => query_params.to_query | |
| ) | |
| end | |
| def self.sign(login_url:, content_path:, external_id:, name:, nonce:, secret:, user_attributes:nil) | |
| user_attributes = user_attributes ? JSON.generate(user_attributes) : '' | |
| signing_str = <<~HEREDOC | |
| #{login_url} | |
| #{content_path} | |
| #{external_id} | |
| #{name} | |
| #{nonce} | |
| #{user_attributes} | |
| HEREDOC | |
| signing_str = signing_str.strip | |
| digest = OpenSSL::Digest.new('sha256') | |
| hmac = OpenSSL::HMAC.digest(digest, secret, signing_str) | |
| Base64.urlsafe_encode64(hmac, padding: false) | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Zeddox thanks!