Skip to content

Instantly share code, notes, and snippets.

@tfoldi
Created April 24, 2013 07:56
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
This ruby class can logon and invoke REST API URLs in Tableau server. Authentication is done by using RSA crpyto stuff.
require 'httpclient'
module TableauLDAPSync
class Tableau
def initialize(server_url)
proxy = ENV['HTTP_PROXY']
@http_client = HTTPClient.new(proxy)
@http_client.set_cookie_store("cookie.dat")
@http_client.debug_dev = STDOUT if $DEBUG
@server_url = server_url
end
def login(user, pass)
key = OpenSSL::PKey::RSA.new
# invoke /auth.xml on server
response = @http_client.get( tableau_url_with '/auth.xml' )
# parse returned XML
doc = REXML::Document.new( response.body )
# read RSA key + authenticity token
authinfo = doc.elements[1, 'authinfo']
modulus = authinfo.elements[1, 'modulus']
exponent = authinfo.elements[1, 'exponent']
authenticity_token = authinfo.elements[1, 'authenticity_token']
# fill RSA key information
key.n = modulus.text.to_i(16)
key.e = exponent.text.to_i(16)
# logon to server with encrypted password
response = @http_client.post( tableau_url_with('/auth/login.xml'),
{ 'authenticity_token' => authenticity_token.text,
'crypted' => assymmetric_encrypt(pass,key),
'username' => user
}
)
@http_client.save_cookie_store
end
def tableau_url_with(path)
URI.join(@server_url, path)
end
# Encrypt test with RSA public key and pack as %.0x hex numbers
def assymmetric_encrypt(val, public_key)
crypt_binary = public_key.public_encrypt(val)
crypt_binary.unpack("H*")
end
def get(path, params = {} )
@http_client.get( tableau_url_with(path), params)
end
def post(path, params = {} )
@http_client.post( tableau_url_with(path), params)
end
end
end
@WarFox
Copy link

WarFox commented Feb 17, 2014

Hey thanks for the script.

I had to use

require 'rexml/document'

to get it working though.

Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment