Skip to content

Commit d70cecc

Browse files
Victor Silvavictorhos
authored andcommitted
feat: adding sessions endpoints
1 parent 49da078 commit d70cecc

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

auth0/management/sessions.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from typing import Any
2+
3+
from auth0.rest import RestClient
4+
from auth0.rest import RestClientOptions
5+
from auth0.types import TimeoutType
6+
7+
8+
class Sessions:
9+
"""Auth0 users endpoints
10+
11+
Args:
12+
domain (str): Your Auth0 domain, e.g: 'username.auth0.com'
13+
14+
token (str): Management API v2 Token
15+
16+
telemetry (bool, optional): Enable or disable Telemetry
17+
(defaults to True)
18+
19+
timeout (float or tuple, optional): Change the requests
20+
connect and read timeout. Pass a tuple to specify
21+
both values separately or a float to set both to it.
22+
(defaults to 5.0 for both)
23+
24+
protocol (str, optional): Protocol to use when making requests.
25+
(defaults to "https")
26+
27+
rest_options (RestClientOptions): Pass an instance of
28+
RestClientOptions to configure additional RestClient
29+
options, such as rate-limit retries.
30+
(defaults to None)
31+
"""
32+
33+
def __init__(
34+
self,
35+
domain: str,
36+
token: str,
37+
telemetry: bool = True,
38+
timeout: TimeoutType = 5.0,
39+
protocol: str = "https",
40+
rest_options: RestClientOptions | None = None,
41+
) -> None:
42+
self.domain = domain
43+
self.protocol = protocol
44+
self.client = RestClient(jwt=token, telemetry=telemetry, timeout=timeout, options=rest_options)
45+
46+
def _url(self, id: str | None = None) -> str:
47+
url = f"{self.protocol}://{self.domain}/api/v2/sessions"
48+
if id is not None:
49+
return f"{url}/{id}"
50+
return url
51+
52+
def get(self, id: str) -> dict[str, Any]:
53+
"""Get a session.
54+
55+
Args:
56+
id (str): The id of the session to retrieve.
57+
58+
See: https://auth0.com/docs/api/management/v2#!/Sessions/get-session
59+
"""
60+
61+
return self.client.get(self._url(id))
62+
63+
def delete(self, id: str) -> None:
64+
"""Delete a session.
65+
66+
Args:
67+
id (str): The id of the session to delete.
68+
69+
See: https://auth0.com/docs/api/management/v2#!/Sessions/delete-session
70+
"""
71+
72+
return self.client.delete(self._url(id))
73+
74+
def revoke(self, id: str) -> None:
75+
"""Revokes a session by ID and all associated refresh tokens..
76+
77+
Args:
78+
id (str): The id of the session to revoke.
79+
80+
See: https://auth0.com/docs/api/management/v2#!/Sessions/revoke-session
81+
"""
82+
83+
url = self._url(f"{id}/sessions")
84+
return self.client.post(url)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import unittest
2+
from unittest import mock
3+
4+
from ...management.users import Sessions
5+
6+
7+
class TestUsers(unittest.TestCase):
8+
def test_init_with_optionals(self):
9+
t = Sessions(domain="domain", token="jwttoken", telemetry=False, timeout=(10, 2))
10+
self.assertEqual(t.client.options.timeout, (10, 2))
11+
telemetry_header = t.client.base_headers.get("Auth0-Client", None)
12+
self.assertEqual(telemetry_header, None)
13+
14+
@mock.patch("auth0.management.users.RestClient")
15+
def test_get(self, mock_rc):
16+
mock_instance = mock_rc.return_value
17+
18+
u = Sessions(domain="domain", token="jwttoken")
19+
u.get("user_id")
20+
21+
mock_instance.get.assert_called_with(
22+
"https://domain/api/v2/sessions/session_id"
23+
)
24+
25+
@mock.patch("auth0.management.users.RestClient")
26+
def test_delete(self, mock_rc):
27+
mock_instance = mock_rc.return_value
28+
29+
u = Sessions(domain="domain", token="jwttoken")
30+
u.delete("session_id")
31+
32+
mock_instance.delete.assert_called_with(
33+
"https://domain/api/v2/sessions/session_id"
34+
)
35+
36+
@mock.patch("auth0.management.users.RestClient")
37+
def test_revoke(self, mock_rc):
38+
mock_instance = mock_rc.return_value
39+
40+
u = Sessions(domain="domain", token="jwttoken")
41+
u.revoke("session_id")
42+
43+
mock_instance.post.assert_called_with(
44+
"https://domain/api/v2/sessions/session_id/sessions"
45+
)

0 commit comments

Comments
 (0)