Skip to content

Commit 0a2dc94

Browse files
authored
Merge pull request #459 from auth0/manage-client-credentials
2 parents 18f7fa9 + 71cd5a8 commit 0a2dc94

6 files changed

Lines changed: 150 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ For more code samples on how to integrate the auth0-python SDK in your Python ap
113113
- AttackProtection() (`Auth0().attack_protection`)
114114
- Blacklists() ( `Auth0().blacklists` )
115115
- Branding() ( `Auth0().branding` )
116+
- ClientCredentials() ( `Auth0().client_credentials` )
116117
- ClientGrants() ( `Auth0().client_grants` )
117118
- Clients() ( `Auth0().clients` )
118119
- Connections() ( `Auth0().connections` )

auth0/v3/management/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .attack_protection import AttackProtection
44
from .blacklists import Blacklists
55
from .branding import Branding
6+
from .client_credentials import ClientCredentials
67
from .client_grants import ClientGrants
78
from .clients import Clients
89
from .connections import Connections
@@ -39,6 +40,7 @@
3940
"AttackProtection",
4041
"Blacklists",
4142
"Branding",
43+
"ClientCredentials",
4244
"ClientGrants",
4345
"Clients",
4446
"Connections",

auth0/v3/management/auth0.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .attack_protection import AttackProtection
44
from .blacklists import Blacklists
55
from .branding import Branding
6+
from .client_credentials import ClientCredentials
67
from .client_grants import ClientGrants
78
from .clients import Clients
89
from .connections import Connections
@@ -34,6 +35,7 @@
3435
"attack_protection": AttackProtection,
3536
"blacklists": Blacklists,
3637
"branding": Branding,
38+
"client_credentials": ClientCredentials,
3739
"client_grants": ClientGrants,
3840
"clients": Clients,
3941
"connections": Connections,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from ..rest import RestClient
2+
3+
4+
class ClientCredentials(object):
5+
"""Auth0 client credentials endpoints.
6+
7+
Args:
8+
domain (str): Your Auth0 domain, for example: 'my-domain.us.auth0.com'
9+
10+
token (str): Management API v2 Token
11+
12+
telemetry (bool, optional): Enable or disable telemetry
13+
(defaults to True)
14+
15+
timeout (float or tuple, optional): Change the requests
16+
connect and read timeout. Pass a tuple to specify
17+
both values separately or a float to set both to it.
18+
(defaults to 5.0 for both)
19+
20+
rest_options (RestClientOptions): Pass an instance of
21+
RestClientOptions to configure additional RestClient
22+
options, such as rate-limit retries.
23+
(defaults to None)
24+
"""
25+
26+
def __init__(
27+
self,
28+
domain,
29+
token,
30+
telemetry=True,
31+
timeout=5.0,
32+
protocol="https",
33+
rest_options=None,
34+
):
35+
self.domain = domain
36+
self.protocol = protocol
37+
self.client = RestClient(
38+
jwt=token, telemetry=telemetry, timeout=timeout, options=rest_options
39+
)
40+
41+
def _url(self, client_id, id=None):
42+
url = "{}://{}/api/v2/clients/{}/credentials".format(
43+
self.protocol, self.domain, client_id
44+
)
45+
if id is not None:
46+
return "{}/{}".format(url, id)
47+
return url
48+
49+
def all(self, client_id):
50+
"""Get a list of credentials associated with a client.
51+
52+
Args:
53+
client_id (string): The id of a client that owns the credentials.
54+
55+
See: https://auth0.com/docs/api/management/v2#!/Client_Credentials/get_client_credentials
56+
"""
57+
return self.client.get(self._url(client_id))
58+
59+
def get(self, client_id, id):
60+
"""Retrieve a specified client credential.
61+
62+
Args:
63+
client_id (string): The id of a client that owns the credential.
64+
65+
id (string): The id of the credential.
66+
67+
See: https://auth0.com/docs/api/management/v2#!/Client_Credentials/get_client_credentials_by_id
68+
"""
69+
return self.client.get(self._url(client_id, id))
70+
71+
def create(self, client_id, body):
72+
"""Create a credential on a client.
73+
74+
Args:
75+
client_id (string): The id of a client to create the credential for.
76+
77+
See: https://auth0.com/docs/api/management/v2#!/Client_Credentials/post_client_credentials
78+
"""
79+
return self.client.post(self._url(client_id), data=body)
80+
81+
def delete(self, client_id, id):
82+
"""Delete a client's credential.
83+
84+
Args:
85+
id (str): The id of credential to delete.
86+
87+
See: https://auth0.com/docs/api/management/v2#!/Client_Credentials/delete_client_credentials_by_id
88+
"""
89+
90+
return self.client.delete(self._url(client_id, id))

auth0/v3/test/management/test_auth0.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ...management.attack_protection import AttackProtection
55
from ...management.auth0 import Auth0
66
from ...management.blacklists import Blacklists
7+
from ...management.client_credentials import ClientCredentials
78
from ...management.client_grants import ClientGrants
89
from ...management.clients import Clients
910
from ...management.connections import Connections
@@ -47,6 +48,9 @@ def test_attack_protection(self):
4748
def test_blacklists(self):
4849
self.assertIsInstance(self.a0.blacklists, Blacklists)
4950

51+
def test_client_credentials(self):
52+
self.assertIsInstance(self.a0.client_credentials, ClientCredentials)
53+
5054
def test_client_grants(self):
5155
self.assertIsInstance(self.a0.client_grants, ClientGrants)
5256

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import unittest
2+
3+
import mock
4+
5+
from ...management.client_credentials import ClientCredentials
6+
7+
8+
class TestClientCredentials(unittest.TestCase):
9+
def test_init_with_optionals(self):
10+
t = ClientCredentials(
11+
domain="domain", token="jwttoken", telemetry=False, timeout=(10, 2)
12+
)
13+
self.assertEqual(t.client.options.timeout, (10, 2))
14+
telemetry_header = t.client.base_headers.get("Auth0-Client", None)
15+
self.assertEqual(telemetry_header, None)
16+
17+
@mock.patch("auth0.v3.management.client_credentials.RestClient")
18+
def test_all(self, mock_rc):
19+
mock_instance = mock_rc.return_value
20+
c = ClientCredentials(domain="domain", token="jwttoken")
21+
c.all("cid")
22+
mock_instance.get.assert_called_with(
23+
"https://domain/api/v2/clients/cid/credentials"
24+
)
25+
26+
@mock.patch("auth0.v3.management.client_credentials.RestClient")
27+
def test_get(self, mock_rc):
28+
mock_instance = mock_rc.return_value
29+
c = ClientCredentials(domain="domain", token="jwttoken")
30+
c.get("cid", "this-id")
31+
mock_instance.get.assert_called_with(
32+
"https://domain/api/v2/clients/cid/credentials/this-id"
33+
)
34+
35+
@mock.patch("auth0.v3.management.client_credentials.RestClient")
36+
def test_create(self, mock_rc):
37+
mock_instance = mock_rc.return_value
38+
c = ClientCredentials(domain="domain", token="jwttoken")
39+
c.create("cid", {"a": "b", "c": "d"})
40+
mock_instance.post.assert_called_with(
41+
"https://domain/api/v2/clients/cid/credentials", data={"a": "b", "c": "d"}
42+
)
43+
44+
@mock.patch("auth0.v3.management.client_credentials.RestClient")
45+
def test_delete(self, mock_rc):
46+
mock_instance = mock_rc.return_value
47+
c = ClientCredentials(domain="domain", token="jwttoken")
48+
c.delete("cid", "this-id")
49+
mock_instance.delete.assert_called_with(
50+
"https://domain/api/v2/clients/cid/credentials/this-id"
51+
)

0 commit comments

Comments
 (0)