Skip to content

Commit ce937f8

Browse files
authored
Merge pull request #182 from descarteslabs/feat/namespace-helper
Shared namespace helper for all services and extensions
2 parents 78ce0eb + a391416 commit ce937f8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

descarteslabs/auth/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import random
2626
import six
2727
import stat
28+
from hashlib import sha1
2829

2930
from descarteslabs.exceptions import AuthError, OauthError
3031

@@ -73,6 +74,7 @@ def __init__(self, domain="https://iam.descarteslabs.com",
7374
self.client_id = client_id
7475
self.client_secret = client_secret
7576
self._token = jwt_token
77+
self._namespace = None
7678

7779
self.domain = domain
7880
self.scope = scope
@@ -192,6 +194,12 @@ def _get_token(self, timeout=100):
192194

193195
os.chmod(self.token_info_path, stat.S_IRUSR | stat.S_IWUSR)
194196

197+
@property
198+
def namespace(self):
199+
if self._namespace is None:
200+
self._namespace = sha1(self.payload['sub'].encode('utf-8')).hexdigest()
201+
return self._namespace
202+
195203

196204
if __name__ == '__main__':
197205
auth = Auth.from_environment_or_token_json()

descarteslabs/tests/test_auth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def test_get_token(self):
3131
r = requests.post(url, data=json.dumps(params), headers=headers)
3232
self.assertEqual(200, r.status_code)
3333

34+
def test_get_namespace(self):
35+
auth = Auth.from_environment_or_token_json()
36+
self.assertIsNotNone(auth.namespace)
37+
3438

3539
if __name__ == '__main__':
3640
unittest.main()

0 commit comments

Comments
 (0)