Created
April 24, 2013 07:56
-
-
Save tfoldi/5450418 to your computer and use it in GitHub Desktop.
This ruby class can logon and invoke REST API URLs in Tableau server. Authentication is done by using RSA crpyto stuff.
This file contains 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 '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 |
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
http://community.tableausoftware.com/groups/dev-community/blog/2013/04/24/using-the-undocumented-rest-api-authentication-and-invocation-of-tableau-server