forked from looker/looker_embed_sso_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_example.py
120 lines (96 loc) · 4.2 KB
/
python_example.py
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
import urllib
import base64
import json
import time
import binascii
import os
from hashlib import sha1
import six
import six.moves.urllib as urllib
import hmac
def to_ascii(s):
"""Compatibility function for converting between Python 2.7 and 3 calls"""
if isinstance(s, six.text_type):
return s
elif isinstance(s, six.binary_type):
return "".join(map(chr, map(ord, s.decode(encoding='UTF-8'))))
return s
class Looker:
def __init__(self, host, secret):
self.secret = secret
self.host = host
class User:
def __init__(self, id=id, first_name=None, last_name=None,
permissions=[], models=[], group_ids=[], external_group_id=None,
user_attributes={}, access_filters={}):
self.external_user_id = json.dumps(id)
self.first_name = json.dumps(first_name)
self.last_name = json.dumps(last_name)
self.permissions = json.dumps(permissions)
self.models = json.dumps(models)
self.access_filters = json.dumps(access_filters)
self.user_attributes = json.dumps(user_attributes)
self.group_ids = json.dumps(group_ids)
self.external_group_id = json.dumps(external_group_id)
class URL:
def __init__(self, looker, user, session_length, embed_url, force_logout_login=False):
self.looker = looker
self.user = user
self.path = '/login/embed/' + urllib.parse.quote_plus(embed_url)
self.session_length = json.dumps(session_length)
self.force_logout_login = json.dumps(force_logout_login)
def set_time(self):
self.time = json.dumps(int(time.time()))
def set_nonce(self):
self.nonce = json.dumps(to_ascii(binascii.hexlify(os.urandom(16))))
def sign(self):
# Do not change the order of these
string_to_sign = "\n".join([self.looker.host,
self.path,
self.nonce,
self.time,
self.session_length,
self.user.external_user_id,
self.user.permissions,
self.user.models,
self.user.group_ids,
self.user.external_group_id,
self.user.user_attributes,
self.user.access_filters])
signer = hmac.new(bytearray(self.looker.secret, 'UTF-8'), string_to_sign.encode('UTF-8'), sha1)
self.signature = base64.b64encode(signer.digest())
def to_string(self):
self.set_time()
self.set_nonce()
self.sign()
params = {'nonce': self.nonce,
'time': self.time,
'session_length': self.session_length,
'external_user_id': self.user.external_user_id,
'permissions': self.user.permissions,
'models': self.user.models,
'group_ids': self.user.group_ids,
'external_group_id': self.user.external_group_id,
'user_attributes': self.user.user_attributes,
'access_filters': self.user.access_filters,
'signature': self.signature,
'first_name': self.user.first_name,
'last_name': self.user.last_name,
'force_logout_login': self.force_logout_login}
query_string = '&'.join(["%s=%s" % (key, urllib.parse.quote_plus(val)) for key, val in params.items()])
return "%s%s?%s" % (self.looker.host, self.path, query_string)
def test():
looker = Looker('your_company.looker.com', 'b167c4998c1faadf5bf7742675aa6018aaa2e296f2eec5442b208d2cb6fdf1bd')
user = User(57,
first_name='Embed Wil',
last_name='Krouse',
permissions=['see_lookml_dashboards', 'access_data'],
models=['thelook'],
group_ids=[5,4],
external_group_id='awesome_engineers',
user_attributes={"an_attribute_name": "my_attribute_value", "my_number_attribute": "42"},
access_filters={'fake_model': {'id': 1}})
fifteen_minutes = 15 * 60
url = URL(looker, user, fifteen_minutes, "/embed/dashboards/3", force_logout_login=True)
print("https://" + url.to_string())
test()