Skip to content

Instantly share code, notes, and snippets.

@n8agrin
Created November 1, 2023 23:32
Show Gist options
  • Save n8agrin/72295c9ad29283b6e02908b5d6837529 to your computer and use it in GitHub Desktop.
Save n8agrin/72295c9ad29283b6e02908b5d6837529 to your computer and use it in GitHub Desktop.
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
p signing_str
digest = OpenSSL::Digest.new('sha256')
hmac = OpenSSL::HMAC.digest(digest, secret, signing_str)
Base64.urlsafe_encode64(hmac, padding: false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment