-
Notifications
You must be signed in to change notification settings - Fork 4
/
TableauHTTP.cs
131 lines (117 loc) · 5.11 KB
/
TableauHTTP.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
namespace Behold_Emailer
{
class TableauHTTP
{
string tableau_server_url;
public Logger logger;
public TableauHTTP(string tableau_server_url)
{
this.tableau_server_url = tableau_server_url;
this.logger = null;
}
public void log(string l)
{
if (this.logger != null)
{
this.logger.Log(l);
}
}
public string get_trusted_ticket_for_user(string username, string site, string ip)
{
if (site == ""){ site = "default";}
this.log(String.Format("Requesting trusted ticket for {0} on site {1}", username, site));
string trusted_url = this.tableau_server_url + "/trusted";
Debug.WriteLine(trusted_url);
WebClient client = new WebClient();
byte[] response;
try
{
if (site == "default")
{
response = client.UploadValues(trusted_url, new System.Collections.Specialized.NameValueCollection()
{
{ "username", username }
}
);
}
else
{
response = client.UploadValues(trusted_url, new System.Collections.Specialized.NameValueCollection()
{
{ "username", username },
{ "target_site", site }
}
);
}
string result = System.Text.Encoding.UTF8.GetString(response);
if (result == "-1")
{
// If you don't get -1, you should have a trusted ticket, raise an exception
string error = String.Format("Trusted ticket for {0} on site {1} from server {2} returned -1, some error occurred", username, site, this.tableau_server_url);
this.log(error);
throw new ConfigurationException(error);
}
// If misconfigured, the Tableau Server returns a redirect page
else if (result.Contains("html") == true)
{
// If you don't get -1, you should have a trusted ticket, raise an exception
string error = String.Format("Trusted ticket for {0} on site {1} from server {2} returned the redirect page, some error occurred", username, site, this.tableau_server_url);
this.log(error);
throw new ConfigurationException(error);
}
this.log(String.Format("Trusted ticket for {0} on site {1} from server {2} returned {3}", username, site, this.tableau_server_url, result));
return result;
}
catch (WebException)
{
throw new ConfigurationException("Trusted tickets not working, check configuration of Tableau Server and the configuration program");
}
}
public bool redeem_trusted_ticket(string view_to_redeem, string trusted_ticket, string site){
if (site == "" || site.ToLower() == "default")
{
site = "default";
}
string trusted_view_url = String.Format("{0}/trusted/{1}", this.tableau_server_url, trusted_ticket);
if (site.ToLower() != "default")
{
trusted_view_url += String.Format("/t/{0}/views/{1}", site, view_to_redeem);
}
else
{
trusted_view_url += String.Format("/views/{0}", view_to_redeem);
}
WebClient client = new WebClient();
try
{
this.log(String.Format("Redeeming trusted ticket via {0}", trusted_view_url));
byte[] response = client.DownloadData(trusted_view_url);
this.log(String.Format("Trusted ticket redeemed succesfully"));
return true;
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
var status_code = ((HttpWebResponse)ex.Response).StatusCode;
var status_description = ((HttpWebResponse)ex.Response).StatusDescription;
this.log(String.Format("Trusted ticket redemption failed with Status Code {0} and Description {1}", status_code, status_description));
}
return false;
}
}
public bool create_trusted_ticket_session(string view_to_redeem, string username, string site, string ip)
{
string ticket = this.get_trusted_ticket_for_user(username, site, ip);
this.log(String.Format("Trusted ticket returned {0}", ticket));
bool result = this.redeem_trusted_ticket(view_to_redeem, ticket, site);
return result;
}
}
}