-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathauth_utils.py
122 lines (106 loc) · 4.78 KB
/
auth_utils.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
121
122
# The MIT License (MIT)
#
# Copyright (c) 2019 Looker Data Sciences, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
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, external_user_id=id, first_name=None, last_name=None,
permissions=[], models=[], group_ids=[], external_group_id=None,
user_attributes={}, access_filters={}, force_logout_login=True, session_length=3000):
self.external_user_id = json.dumps(external_user_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)
self.force_logout_login = json.dumps(force_logout_login)
self.session_length = json.dumps(session_length)
class URL:
def __init__(self, looker, user, embed_url):
self.looker = looker
self.user = user
self.path = '/#/embed/' + urllib.parse.quote_plus(embed_url)
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.user.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.user.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.user.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 create_signed_url(src, user, host, secret):
looker = Looker(host, secret)
url = URL(looker, user, src)
return "https://" + url.to_string()