Skip to content

Commit 1cfafdf

Browse files
authored
feat(auth): Add scope support (#101)
Adds support for specifying `scope` when creating `Domo` SDK client. `scope` is a list of scope names such as `["data", "user", "account"]`. This allows limiting the scope of access tokens to a subset of the scopes granted on the API client. Not specifying `scope` maintains the default behavior of access tokens having the same scopes as the API client.
1 parent 5c66cc6 commit 1cfafdf

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

pydomo/Transport.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ class DomoAPITransport:
1515
serialization and deserialization of objects.
1616
"""
1717

18-
def __init__(self, client_id, client_secret, api_host, use_https, logger, request_timeout):
18+
def __init__(self, client_id, client_secret, api_host, use_https, logger, request_timeout, scope):
1919
self.apiHost = self._build_apihost(api_host, use_https)
2020
self.clientId = client_id
2121
self.clientSecret = client_secret
2222
self.logger = logger
2323
self.request_timeout = request_timeout
24+
self.scope = scope
2425
self._renew_access_token()
2526

2627
@staticmethod
@@ -84,10 +85,13 @@ def request(self, url, method, headers, params=None, body=None):
8485

8586
def _renew_access_token(self):
8687
self.logger.debug("Renewing Access Token")
88+
# scope == None means use all scopes from client
89+
scope = ' '.join(self.scope) if self.scope else None
90+
8791
request_args = {
8892
'method': HTTPMethod.POST,
8993
'url': self.apiHost + '/oauth/token',
90-
'data': {'grant_type': 'client_credentials'},
94+
'data': {'grant_type': 'client_credentials', 'scope': scope},
9195
'auth': HTTPBasicAuth(self.clientId, self.clientSecret)
9296
}
9397
if self.request_timeout:

pydomo/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ def __init__(self, client_id, client_secret, api_host='api.domo.com', **kwargs):
8383
self.logger = parent_logger
8484

8585
timeout = kwargs.get('request_timeout', None)
86+
scope = kwargs.get('scope')
8687

8788
if kwargs.get('log_level'):
8889
self.logger.setLevel(kwargs['log_level'])
8990
self.logger.debug("\n" + DOMO + "\n")
9091

91-
self.transport = DomoAPITransport(client_id, client_secret, api_host, kwargs.get('use_https', True), self.logger, request_timeout = timeout)
92+
self.transport = DomoAPITransport(client_id, client_secret, api_host, kwargs.get('use_https', True), self.logger, request_timeout = timeout, scope = scope)
9293
self.datasets = DataSetClient(self.transport, self.logger)
9394
self.groups = GroupClient(self.transport, self.logger)
9495
self.pages = PageClient(self.transport, self.logger)

0 commit comments

Comments
 (0)