Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e09d69d

Browse files
sagnew-dglbalmaceda
authored andcommitted
Add grants, custom domains, rules_configs to API (auth0#177)
* Added method and unit test for Guardian SNS factor provider configuration and also added a bash script called clean.sh to clean-up any files before a commit. This helps developers like myself to clean things up before requesting a merge * Added another function and unit tests for Twileo factor provider configuration * Add class for grants * Added delete grant and unit test * Added custom_domain and unit tests * Removed Guardian changes - functionality already exists. * Fix typo in custom domain unit test * Fix typo * Added rotate secret and unit test to client * Added configure new custom domain and unit test * Added get custom domain configuration and unit test * Added verify custom domain and unit test * Added job results search and unit test * Added rulesconfigs and unit test * address comments from original PR (auth0#160) * small code review fixes * code review comments; name rules configs methods set / unset because it's a PUT, fix docstrings, remove extra_params where not needed
1 parent 62ed528 commit e09d69d

File tree

13 files changed

+385
-1
lines changed

13 files changed

+385
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ nosetests.xml
3939
coverage.xml
4040
*,cover
4141
.hypothesis/
42+
.pytest_cache
4243

4344
# Translations
4445
*.mo

auth0/v3/management/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
from .clients import Clients
44
from .client_grants import ClientGrants
55
from .connections import Connections
6+
from .custom_domains import CustomDomains
67
from .device_credentials import DeviceCredentials
78
from .emails import Emails
89
from .email_templates import EmailTemplates
10+
from .grants import Grants
911
from .guardian import Guardian
1012
from .jobs import Jobs
1113
from .logs import Logs
1214
from .resource_servers import ResourceServers
1315
from .rules import Rules
16+
from .rules_configs import RulesConfigs
1417
from .stats import Stats
1518
from .tenants import Tenants
1619
from .tickets import Tickets

auth0/v3/management/auth0.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from .grants import Grants
2+
from .custom_domains import CustomDomains
13
from .blacklists import Blacklists
24
from .clients import Clients
35
from .client_grants import ClientGrants
@@ -10,6 +12,7 @@
1012
from .logs import Logs
1113
from .resource_servers import ResourceServers
1214
from .rules import Rules
15+
from .rules_configs import RulesConfigs
1316
from .stats import Stats
1417
from .tenants import Tenants
1518
from .tickets import Tickets
@@ -31,15 +34,18 @@ def __init__(self, domain, token):
3134
self.blacklists = Blacklists(domain, token)
3235
self.clients = Clients(domain, token)
3336
self.client_grants = ClientGrants(domain, token)
37+
self.custom_domains = CustomDomains(domain, token)
3438
self.connections = Connections(domain, token)
3539
self.device_credentials = DeviceCredentials(domain, token)
3640
self.emails = Emails(domain, token)
3741
self.email_templates = EmailTemplates(domain, token)
42+
self.grants = Grants(domain.token)
3843
self.guardian = Guardian(domain, token)
3944
self.jobs = Jobs(domain, token)
4045
self.logs = Logs(domain, token)
4146
self.resource_servers = ResourceServers(domain, token)
4247
self.rules = Rules(domain, token)
48+
self.rules_configs = RulesConfigs(domain, token)
4349
self.stats = Stats(domain, token)
4450
self.tenants = Tenants(domain, token)
4551
self.tickets = Tickets(domain, token)

auth0/v3/management/clients.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,25 @@ def update(self, id, body):
111111
attributes can only be updated with the update:client_keys scope.
112112
113113
Args:
114-
id (str): Client id of the application.
114+
id (str): Client ID of the application.
115115
116116
body (dict): Attributes to modify.
117117
See: https://auth0.com/docs/api/management/v2#!/Clients/patch_clients_by_id
118118
"""
119119

120120
return self.client.patch(self._url(id), data=body)
121121

122+
def rotate_secret(self, id):
123+
"""Rotate a client secret. The generated secret is NOT base64 encoded.
124+
125+
Args:
126+
id (str): Client ID of the application.
127+
128+
body (dict): Attributes to modify.
129+
See: https://auth0.com/docs/api/management/v2#!/Clients/post_rotate_secret
130+
"""
131+
132+
params = {'id': id }
133+
134+
url = self._url('%s/rotate-secret' % id)
135+
return self.client.get(url, params=params)

auth0/v3/management/custom_domains.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from .rest import RestClient
2+
3+
4+
class CustomDomains(object):
5+
6+
"""Auth0 custom domains endpoints
7+
8+
Args:
9+
domain (str): Your Auth0 domain, e.g: 'username.auth0.com'
10+
11+
token (str): Management API v2 Token
12+
13+
telemetry (bool, optional): Enable or disable Telemetry
14+
(defaults to True)
15+
"""
16+
17+
def __init__(self, domain, token, telemetry=True):
18+
self.domain = domain
19+
self.client = RestClient(jwt=token, telemetry=telemetry)
20+
21+
def _url(self, id=None):
22+
url = 'https://%s/api/v2/custom-domains' % self.domain
23+
if id is not None:
24+
return url + '/' + id
25+
return url
26+
27+
def all(self):
28+
"""Retrieves all custom domains.
29+
30+
See: https://auth0.com/docs/api/management/v2#!/Custom_Domains/get_custom_domains
31+
"""
32+
return self.client.get(self._url())
33+
34+
def get(self, id):
35+
"""Retrieves custom domain.
36+
37+
See: https://auth0.com/docs/api/management/v2#!/Custom_Domains/get_custom_domains_by_id
38+
"""
39+
url = self._url('%s' % (id))
40+
return self.client.get(url)
41+
42+
def delete(self, id):
43+
"""Deletes a grant.
44+
45+
Args:
46+
id (str): The id of the custom domain to delete
47+
48+
49+
See: https://auth0.com/docs/api/management/v2#!/Custom_Domains/delete_custom_domains_by_id
50+
"""
51+
url = self._url('%s' % (id))
52+
return self.client.delete(url)
53+
54+
def create_new(self, body):
55+
"""Configure a new custom domain
56+
57+
Args:
58+
body (str): The domain, tye and verification method in json
59+
60+
61+
See: https://auth0.com/docs/api/management/v2#!/Custom_Domains/post_custom_domains
62+
"""
63+
return self.client.post(self._url(), data=body)
64+
65+
def verify(self, id):
66+
"""Verify a custom domain
67+
68+
Args:
69+
id (str): The id of the custom domain to delete
70+
71+
72+
See: https://auth0.com/docs/api/management/v2#!/Custom_Domains/post_verify
73+
"""
74+
url = self._url('%s/verify' % (id))
75+
return self.client.post(url)

auth0/v3/management/grants.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from .rest import RestClient
2+
3+
4+
class Grants(object):
5+
6+
"""Auth0 grants endpoints
7+
8+
Args:
9+
domain (str): Your Auth0 domain, e.g: 'username.auth0.com'
10+
11+
token (str): Management API v2 Token
12+
13+
telemetry (bool, optional): Enable or disable Telemetry
14+
(defaults to True)
15+
"""
16+
17+
def __init__(self, domain, token, telemetry=True):
18+
self.domain = domain
19+
self.client = RestClient(jwt=token, telemetry=telemetry)
20+
21+
def _url(self, id=None):
22+
url = 'https://%s/api/v2/grants' % self.domain
23+
if id is not None:
24+
return url + '/' + id
25+
return url
26+
27+
def all(self, page=None, per_page=None, include_totals=False, extra_params=None):
28+
"""Retrieves all grants.
29+
30+
Args:
31+
page (int, optional): The result's page number (zero based).
32+
33+
per_page (int, optional): The amount of entries per page.
34+
35+
include_totals (bool, optional): True if the query summary is
36+
to be included in the result, False otherwise.
37+
38+
extra_params (dictionary, optional): The extra parameters to add to
39+
the request. The page, per_page, and include_totals values
40+
specified as parameters take precedence over the ones defined here.
41+
42+
See: https://auth0.com/docs/api/management/v2#!/Grants/get_grants
43+
"""
44+
params = extra_params or {}
45+
params.update({
46+
'page': page,
47+
'per_page': per_page,
48+
'include_totals': str(include_totals).lower()
49+
})
50+
51+
return self.client.get(self._url(), params=params)
52+
53+
def delete(self, id):
54+
"""Deletes a grant.
55+
56+
Args:
57+
id (str): The id of the grant to delete
58+
59+
60+
See: https://auth0.com/docs/api/management/v2#!/Grants/delete_grants_by_id
61+
"""
62+
url = self._url('%s' % (id))
63+
return self.client.delete(url)

auth0/v3/management/jobs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ def get_failed_job(self, id):
4545
url = self._url('{}/errors'.format(id))
4646
return self.client.get(url)
4747

48+
def get_results(self, job_id):
49+
"""Get results of a job
50+
51+
Args:
52+
job_id (str): The ID of the job.
53+
54+
See: https://auth0.com/docs/api/management/v2#!/Jobs/get_results
55+
"""
56+
url = self._url('%s/results' % job_id)
57+
return self.client.get(url)
58+
4859
def export_users(self, body):
4960
"""Export all users to a file using a long running job.
5061

auth0/v3/management/rules_configs.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from .rest import RestClient
2+
3+
4+
class RulesConfigs(object):
5+
6+
"""RulesConfig endpoint implementation.
7+
8+
Args:
9+
domain (str): Your Auth0 domain, e.g: 'username.auth0.com'
10+
11+
token (str): Management API v2 Token
12+
13+
telemetry (bool, optional): Enable or disable Telemetry
14+
(defaults to True)
15+
"""
16+
17+
def __init__(self, domain, token, telemetry=True):
18+
self.domain = domain
19+
self.client = RestClient(jwt=token, telemetry=telemetry)
20+
21+
def _url(self, id=None):
22+
url = 'https://%s/api/v2/rules-configs' % self.domain
23+
if id is not None:
24+
return url + '/' + id
25+
return url
26+
27+
def all(self):
28+
"""Lists the config variable keys for rules.
29+
30+
See: https://auth0.com/docs/api/management/v2#!/Rules_Configs/get_rules_configs
31+
"""
32+
return self.client.get(self._url())
33+
34+
def unset(self, key):
35+
"""Removes the rules config for a given key.
36+
37+
Args:
38+
key (str): rules config key to remove
39+
40+
See: https://auth0.com/docs/api/management/v2#!/Rules_Configs/delete_rules_configs_by_key
41+
"""
42+
params = {
43+
'key': key
44+
}
45+
return self.client.delete(self._url(), params=params)
46+
47+
def set(self, key, value):
48+
"""Sets the rules config for a given key.
49+
50+
Args:
51+
key (str): rules config key to set
52+
53+
value (str): value to set for the rules config key
54+
55+
See: https://auth0.com/docs/api/management/v2#!/Rules_Configs/put_rules_configs_by_key
56+
"""
57+
url = self._url('{}'.format(key))
58+
body = {'value': value}
59+
return self.client.put(url, data=body)
60+

auth0/v3/test/management/test_clients.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,17 @@ def test_update(self, mock_rc):
111111

112112
self.assertEqual('https://domain/api/v2/clients/this-id', args[0])
113113
self.assertEqual(kwargs['data'], {'a': 'b', 'c': 'd'})
114+
115+
@mock.patch('auth0.v3.management.clients.RestClient')
116+
def test_rotate_secret(self, mock_rc):
117+
mock_instance = mock_rc.return_value
118+
119+
c = Clients(domain='domain', token='jwttoken')
120+
c.rotate_secret('this-id')
121+
122+
mock_instance.get.assert_called_with(
123+
'https://domain/api/v2/clients/this-id/rotate-secret', params={'id': 'this-id'}
124+
)
125+
126+
127+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import unittest
2+
import mock
3+
from ...management.custom_domains import CustomDomains
4+
5+
6+
class TestCustomDomains(unittest.TestCase):
7+
8+
@mock.patch('auth0.v3.management.custom_domains.RestClient')
9+
def test_get_all(self, mock_rc):
10+
mock_instance = mock_rc.return_value
11+
12+
g = CustomDomains(domain='domain', token='jwttoken')
13+
g.all()
14+
15+
mock_instance.get.assert_called_with(
16+
'https://domain/api/v2/custom-domains'
17+
)
18+
19+
@mock.patch('auth0.v3.management.custom_domains.RestClient')
20+
def test_create_new(self, mock_rc):
21+
mock_instance = mock_rc.return_value
22+
23+
g = CustomDomains(domain='domain', token='jwttoken')
24+
g.create_new(body={'a': 'b', 'c': 'd','e': 'f'})
25+
26+
args, kwargs = mock_instance.post.call_args
27+
28+
self.assertEqual('https://domain/api/v2/custom-domains',args[0])
29+
self.assertEqual(kwargs['data'], {'a': 'b', 'c': 'd','e': 'f'})
30+
31+
@mock.patch('auth0.v3.management.custom_domains.RestClient')
32+
def test_get_domain_by_id(self, mock_rc):
33+
mock_instance = mock_rc.return_value
34+
35+
g = CustomDomains(domain='domain', token='jwttoken')
36+
g.get('an-id')
37+
38+
mock_instance.get.assert_called_with('https://domain/api/v2/custom-domains/an-id')
39+
40+
41+
@mock.patch('auth0.v3.management.custom_domains.RestClient')
42+
def test_verify(self, mock_rc):
43+
mock_instance = mock_rc.return_value
44+
45+
g = CustomDomains(domain='domain', token='jwttoken')
46+
g.verify('an-id')
47+
48+
args, kwargs = mock_instance.post.call_args
49+
50+
self.assertEqual('https://domain/api/v2/custom-domains/an-id/verify', args[0])
51+
52+

0 commit comments

Comments
 (0)