Skip to content

Commit da56c33

Browse files
committed
Add types to all authentification module
1 parent 8729d32 commit da56c33

9 files changed

Lines changed: 123 additions & 74 deletions

File tree

auth0/authentication/base.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
15
from auth0.rest import RestClient, RestClientOptions
6+
from auth0.types import RequestData, TimeoutType
27

38
from .client_authentication import add_client_authentication
49

@@ -21,15 +26,15 @@ class AuthenticationBase:
2126

2227
def __init__(
2328
self,
24-
domain,
25-
client_id,
26-
client_secret=None,
27-
client_assertion_signing_key=None,
28-
client_assertion_signing_alg=None,
29-
telemetry=True,
30-
timeout=5.0,
31-
protocol="https",
32-
):
29+
domain: str,
30+
client_id: str,
31+
client_secret: str | None = None,
32+
client_assertion_signing_key: str | None = None,
33+
client_assertion_signing_alg: str | None = None,
34+
telemetry: bool = True,
35+
timeout: TimeoutType = 5.0,
36+
protocol: str = "https",
37+
) -> None:
3338
self.domain = domain
3439
self.client_id = client_id
3540
self.client_secret = client_secret
@@ -41,7 +46,7 @@ def __init__(
4146
options=RestClientOptions(telemetry=telemetry, timeout=timeout, retries=0),
4247
)
4348

44-
def _add_client_authentication(self, payload):
49+
def _add_client_authentication(self, payload: dict[str, Any]) -> dict[str, Any]:
4550
return add_client_authentication(
4651
payload,
4752
self.domain,
@@ -51,13 +56,28 @@ def _add_client_authentication(self, payload):
5156
self.client_assertion_signing_alg,
5257
)
5358

54-
def post(self, url, data=None, headers=None):
59+
def post(
60+
self,
61+
url: str,
62+
data: RequestData | None = None,
63+
headers: dict[str, str] | None = None,
64+
) -> Any:
5565
return self.client.post(url, data=data, headers=headers)
5666

57-
def authenticated_post(self, url, data=None, headers=None):
67+
def authenticated_post(
68+
self,
69+
url: str,
70+
data: dict[str, Any] | None = None,
71+
headers: dict[str, str] | None = None,
72+
) -> Any:
5873
return self.client.post(
5974
url, data=self._add_client_authentication(data), headers=headers
6075
)
6176

62-
def get(self, url, params=None, headers=None):
77+
def get(
78+
self,
79+
url: str,
80+
params: dict[str, Any] | None = None,
81+
headers: dict[str, str] | None = None,
82+
) -> Any:
6383
return self.client.get(url, params, headers)

auth0/authentication/client_authentication.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
from __future__ import annotations
2+
13
import datetime
24
import uuid
5+
from typing import Any
36

47
import jwt
58

69

710
def create_client_assertion_jwt(
8-
domain, client_id, client_assertion_signing_key, client_assertion_signing_alg
9-
):
11+
domain: str,
12+
client_id: str,
13+
client_assertion_signing_key: str | None,
14+
client_assertion_signing_alg: str | None,
15+
) -> str:
1016
"""Creates a JWT for the client_assertion field.
1117
1218
Args:
@@ -35,13 +41,13 @@ def create_client_assertion_jwt(
3541

3642

3743
def add_client_authentication(
38-
payload,
39-
domain,
40-
client_id,
41-
client_secret,
42-
client_assertion_signing_key,
43-
client_assertion_signing_alg,
44-
):
44+
payload: dict[str, Any],
45+
domain: str,
46+
client_id: str,
47+
client_secret: str,
48+
client_assertion_signing_key: str | None,
49+
client_assertion_signing_alg: str | None,
50+
) -> dict[str, Any]:
4551
"""Adds the client_assertion or client_secret fields to authenticate a payload.
4652
4753
Args:

auth0/authentication/database.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import warnings
1+
from __future__ import annotations
2+
3+
from typing import Any
24

35
from .base import AuthenticationBase
46

@@ -12,17 +14,17 @@ class Database(AuthenticationBase):
1214

1315
def signup(
1416
self,
15-
email,
16-
password,
17-
connection,
18-
username=None,
19-
user_metadata=None,
20-
given_name=None,
21-
family_name=None,
22-
name=None,
23-
nickname=None,
24-
picture=None,
25-
):
17+
email: str,
18+
password: str,
19+
connection: str,
20+
username: str | None = None,
21+
user_metadata: dict[str, Any] | None = None,
22+
given_name: str | None = None,
23+
family_name: str | None = None,
24+
name: str | None = None,
25+
nickname: str | None = None,
26+
picture: str | None = None,
27+
) -> Any:
2628
"""Signup using email and password.
2729
2830
Args:
@@ -75,7 +77,9 @@ def signup(
7577
f"{self.protocol}://{self.domain}/dbconnections/signup", data=body
7678
)
7779

78-
def change_password(self, email, connection, password=None):
80+
def change_password(
81+
self, email: str, connection: str, password: str | None = None
82+
) -> Any:
7983
"""Asks to change a password for a given user.
8084
8185
email (str): The user's email address.

auth0/authentication/delegated.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
15
from .base import AuthenticationBase
26

37

@@ -10,13 +14,13 @@ class Delegated(AuthenticationBase):
1014

1115
def get_token(
1216
self,
13-
target,
14-
api_type,
15-
grant_type,
16-
id_token=None,
17-
refresh_token=None,
18-
scope="openid",
19-
):
17+
target: str,
18+
api_type: str,
19+
grant_type: str,
20+
id_token: str | None = None,
21+
refresh_token: str | None = None,
22+
scope: str = "openid",
23+
) -> Any:
2024
"""Obtain a delegation token."""
2125

2226
if id_token and refresh_token:

auth0/authentication/enterprise.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
from .base import AuthenticationBase
24

35

@@ -9,7 +11,7 @@ class Enterprise(AuthenticationBase):
911
domain (str): Your auth0 domain (e.g: my-domain.us.auth0.com)
1012
"""
1113

12-
def saml_metadata(self):
14+
def saml_metadata(self) -> Any:
1315
"""Get SAML2.0 Metadata."""
1416

1517
return self.get(
@@ -18,7 +20,7 @@ def saml_metadata(self):
1820
)
1921
)
2022

21-
def wsfed_metadata(self):
23+
def wsfed_metadata(self) -> Any:
2224
"""Returns the WS-Federation Metadata."""
2325

2426
url = "{}://{}/wsfed/FederationMetadata/2007-06/FederationMetadata.xml"

auth0/authentication/get_token.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
15
from .base import AuthenticationBase
2-
from .client_authentication import add_client_authentication
36

47

58
class GetToken(AuthenticationBase):
@@ -12,10 +15,10 @@ class GetToken(AuthenticationBase):
1215

1316
def authorization_code(
1417
self,
15-
code,
16-
redirect_uri,
17-
grant_type="authorization_code",
18-
):
18+
code: str,
19+
redirect_uri: str | None,
20+
grant_type: str = "authorization_code",
21+
) -> Any:
1922
"""Authorization code grant
2023
2124
This is the OAuth 2.0 grant that regular web apps utilize in order
@@ -47,11 +50,11 @@ def authorization_code(
4750

4851
def authorization_code_pkce(
4952
self,
50-
code_verifier,
51-
code,
52-
redirect_uri,
53-
grant_type="authorization_code",
54-
):
53+
code_verifier: str,
54+
code: str,
55+
redirect_uri: str | None,
56+
grant_type: str = "authorization_code",
57+
) -> Any:
5558
"""Authorization code pkce grant
5659
5760
This is the OAuth 2.0 grant that mobile apps utilize in order to access an API.
@@ -86,9 +89,9 @@ def authorization_code_pkce(
8689

8790
def client_credentials(
8891
self,
89-
audience,
90-
grant_type="client_credentials",
91-
):
92+
audience: str,
93+
grant_type: str = "client_credentials",
94+
) -> Any:
9295
"""Client credentials grant
9396
9497
This is the OAuth 2.0 grant that server processes utilize in
@@ -116,13 +119,13 @@ def client_credentials(
116119

117120
def login(
118121
self,
119-
username,
120-
password,
121-
scope=None,
122-
realm=None,
123-
audience=None,
124-
grant_type="http://auth0.com/oauth/grant-type/password-realm",
125-
):
122+
username: str,
123+
password: str,
124+
scope: str | None = None,
125+
realm: str | None = None,
126+
audience: str | None = None,
127+
grant_type: str = "http://auth0.com/oauth/grant-type/password-realm",
128+
) -> Any:
126129
"""Calls /oauth/token endpoint with password-realm grant type
127130
128131
@@ -168,10 +171,10 @@ def login(
168171

169172
def refresh_token(
170173
self,
171-
refresh_token,
172-
scope="",
173-
grant_type="refresh_token",
174-
):
174+
refresh_token: str,
175+
scope: str = "",
176+
grant_type: str = "refresh_token",
177+
) -> Any:
175178
"""Calls /oauth/token endpoint with refresh token grant type
176179
177180
Use this endpoint to refresh an access token, using the refresh token you got during authorization.
@@ -199,7 +202,9 @@ def refresh_token(
199202
},
200203
)
201204

202-
def passwordless_login(self, username, otp, realm, scope, audience):
205+
def passwordless_login(
206+
self, username: str, otp: str, realm: str, scope: str, audience: str
207+
) -> Any:
203208
"""Calls /oauth/token endpoint with http://auth0.com/oauth/grant-type/passwordless/otp grant type
204209
205210
Once the verification code was received, login the user using this endpoint with their

auth0/authentication/passwordless.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import warnings
1+
from __future__ import annotations
2+
3+
from typing import Any
24

35
from .base import AuthenticationBase
46

@@ -11,7 +13,9 @@ class Passwordless(AuthenticationBase):
1113
domain (str): Your auth0 domain (e.g: my-domain.us.auth0.com)
1214
"""
1315

14-
def email(self, email, send="link", auth_params=None):
16+
def email(
17+
self, email: str, send: str = "link", auth_params: dict[str, str] | None = None
18+
) -> Any:
1519
"""Start flow sending an email.
1620
1721
Given the user email address, it will send an email with:
@@ -48,7 +52,7 @@ def email(self, email, send="link", auth_params=None):
4852
f"{self.protocol}://{self.domain}/passwordless/start", data=data
4953
)
5054

51-
def sms(self, phone_number):
55+
def sms(self, phone_number: str) -> Any:
5256
"""Start flow sending an SMS message.
5357
5458
Given the user phone number, it will send an SMS with

auth0/authentication/revoke_token.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
from .base import AuthenticationBase
24

35

@@ -8,7 +10,7 @@ class RevokeToken(AuthenticationBase):
810
domain (str): Your auth0 domain (e.g: my-domain.us.auth0.com)
911
"""
1012

11-
def revoke_refresh_token(self, token):
13+
def revoke_refresh_token(self, token: str) -> Any:
1214
"""Revokes a Refresh Token if it has been compromised
1315
1416
Each revocation request invalidates not only the specific token, but all other tokens

auth0/authentication/social.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
from .base import AuthenticationBase
24

35

@@ -9,7 +11,7 @@ class Social(AuthenticationBase):
911
domain (str): Your auth0 domain (e.g: my-domain.us.auth0.com)
1012
"""
1113

12-
def login(self, access_token, connection, scope="openid"):
14+
def login(self, access_token: str, connection: str, scope: str = "openid") -> Any:
1315
"""Login using a social provider's access token
1416
1517
Given the social provider's access_token and the connection specified,

0 commit comments

Comments
 (0)