forked from looker/looker_embed_sso_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csharp_example.cs
149 lines (133 loc) · 4.94 KB
/
csharp_example.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace SSOTest
{
class Program
{
static void Main(string[] args)
{
var user_attributes = new Dictionary<string, string>();
user_attributes["an_attribute_name"] = "my_attribute_value";
user_attributes["my_number_attribute"] = "1000.232";
var config = new LookerEmbedConfiguration()
{
HostName = "your-hostname.looker.com",
//HostPort = 9999,
Secret = "--- your secret here ---",
ExternalUserId = "57",
UserFirstName = "Embed",
UserLastName = "User",
Permissions = new string[] {"explore", "see_user_dashboards", "see_lookml_dashboards","access_data","see_looks", "download_with_limit"},
Models = new string[] { "imdb" },
GroupIds = new int[] {4, 2},
ExternalGroupId = "awesome_engineers",
UserAttributeMapping = user_attributes
};
var url = GetLookerEmbedUrl("/embed/dashboards/1", config);
Console.WriteLine(url.AbsoluteUri);
Console.ReadLine();
}
public class LookerEmbedConfiguration
{
// AccessFilters holds a JSON serialized object tree describing the access control filters
// {"model_name":{"view_name.field_name": "'Your Value'"}}"
public string AccessFilters { get; set; }
public string ExternalUserId { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public bool ForceLogoutLogin { get; set; }
public string[] Models { get; set; }
public int[] GroupIds { get; set; }
public string ExternalGroupId { get; set; }
public string[] Permissions { get; set; }
public Dictionary<string, string> UserAttributeMapping { get; set; }
public string Secret { get; set; }
public TimeSpan SessionLength { get; set; }
public string HostName { get; set; }
public int HostPort { get; set; }
public string Nonce { get; set; }
public LookerEmbedConfiguration()
{
ForceLogoutLogin = true;
SessionLength = TimeSpan.FromMinutes(15);
Nonce = DateTime.Now.Ticks.ToString();
AccessFilters = "{}";
}
}
public static Uri GetLookerEmbedUrl(string targetPath, LookerEmbedConfiguration config)
{
var builder = new UriBuilder
{
Scheme = "https",
Host = config.HostName,
Port = config.HostPort,
Path = "/login/embed/" + System.Net.WebUtility.UrlEncode(targetPath)
};
var unixTime = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
var time = unixTime.ToString();
var json_nonce = JsonConvert.SerializeObject(config.Nonce);
var json_external_user_id = JsonConvert.SerializeObject(config.ExternalUserId);
var json_permissions = JsonConvert.SerializeObject(config.Permissions);
var json_group_ids = JsonConvert.SerializeObject(config.GroupIds);
var json_external_group_id = JsonConvert.SerializeObject(config.ExternalGroupId);
var json_user_attribute_values = JsonConvert.SerializeObject(config.UserAttributeMapping);
var json_models = JsonConvert.SerializeObject(config.Models);
var json_session_length = String.Format("{0:N0}", (long)config.SessionLength.TotalSeconds);
// order of elements is important
var stringToSign = String.Join("\n", new string[] {
builder.Uri.Authority,
builder.Path,
json_nonce,
time,
json_session_length,
json_external_user_id,
json_permissions,
json_models,
json_group_ids,
json_external_group_id,
json_user_attribute_values,
config.AccessFilters
});
var signature = EncodeString(stringToSign, config.Secret);
var json_first_name = JsonConvert.SerializeObject(config.UserFirstName);
var json_last_name = JsonConvert.SerializeObject(config.UserLastName);
var json_force_logout_login = JsonConvert.SerializeObject(config.ForceLogoutLogin);
var qparams = new Dictionary<string, string>()
{
{ "nonce", json_nonce },
{ "time", time },
{ "session_length", json_session_length },
{ "external_user_id", json_external_user_id },
{ "permissions", json_permissions },
{ "models", json_models },
{ "group_ids", json_group_ids },
{ "external_group_id", json_external_group_id },
{ "user_attributes", json_user_attribute_values },
{ "access_filters", config.AccessFilters},
{ "first_name", json_first_name },
{ "last_name", json_last_name },
{ "force_logout_login", json_force_logout_login },
{ "signature", signature }
};
builder.Query = String.Join("&", qparams.Select(kvp => kvp.Key + "=" + System.Net.WebUtility.UrlEncode(kvp.Value)));
return builder.Uri;
}
private static string EncodeString(string urlToSign, string secret)
{
var bytes = Encoding.UTF8.GetBytes(secret);
var stringToEncode = Encoding.UTF8.GetBytes(urlToSign);
using (HMACSHA1 hmac = new HMACSHA1(bytes))
{
var rawHmac = hmac.ComputeHash(stringToEncode);
return Convert.ToBase64String(rawHmac);
}
}
}
}