Skip to content

Commit

Permalink
Add types to missing parts and apply first mypy pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Mar 19, 2023
1 parent 6619a63 commit 7b93c5e
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion auth0/authentication/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def post(
def authenticated_post(
self,
url: str,
data: dict[str, Any] | None = None,
data: dict[str, Any],
headers: dict[str, str] | None = None,
) -> Any:
return self.client.post(
Expand Down
4 changes: 2 additions & 2 deletions auth0/authentication/client_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def add_client_authentication(
payload: dict[str, Any],
domain: str,
client_id: str,
client_secret: str,
client_secret: str | None,
client_assertion_signing_key: str | None,
client_assertion_signing_alg: str | None,
) -> dict[str, Any]:
Expand All @@ -54,7 +54,7 @@ def add_client_authentication(
payload (dict): The POST payload that needs additional fields to be authenticated.
domain (str): The domain of your Auth0 tenant
client_id (str): Your application's client ID
client_secret (str): Your application's client secret
client_secret (str, optional): Your application's client secret
client_assertion_signing_key (str, optional): Private key used to sign the client assertion JWT
client_assertion_signing_alg (str, optional): Algorithm used to sign the client assertion JWT (defaults to 'RS256')
Expand Down
12 changes: 7 additions & 5 deletions auth0/authentication/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def signup(
name: str | None = None,
nickname: str | None = None,
picture: str | None = None,
) -> Any:
) -> dict[str, Any]:
"""Signup using email and password.
Args:
Expand Down Expand Up @@ -52,7 +52,7 @@ def signup(
See: https://auth0.com/docs/api/authentication#signup
"""
body = {
body: dict[str, Any] = {
"client_id": self.client_id,
"email": email,
"password": password,
Expand All @@ -73,13 +73,14 @@ def signup(
if picture:
body.update({"picture": picture})

return self.post(
data: dict[str, Any] = self.post(
f"{self.protocol}://{self.domain}/dbconnections/signup", data=body
)
return data

def change_password(
self, email: str, connection: str, password: str | None = None
) -> Any:
) -> str:
"""Asks to change a password for a given user.
email (str): The user's email address.
Expand All @@ -92,7 +93,8 @@ def change_password(
"connection": connection,
}

return self.post(
data: str = self.post(
f"{self.protocol}://{self.domain}/dbconnections/change_password",
data=body,
)
return data
2 changes: 1 addition & 1 deletion auth0/authentication/passwordless.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def email(
auth_params (dict, optional): Parameters to append or override.
"""

data = {
data: dict[str, Any] = {
"client_id": self.client_id,
"connection": "email",
"email": email,
Expand Down
4 changes: 2 additions & 2 deletions auth0/authentication/token_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def verify_signature(self, token: str) -> dict[str, Any]:
kid = self._get_kid(token)
secret_or_certificate = self._fetch_key(key_id=kid)

return self._decode_jwt(token, secret_or_certificate)
return self._decode_jwt(token, secret_or_certificate) # type: ignore[arg-type]


class SymmetricSignatureVerifier(SignatureVerifier):
Expand Down Expand Up @@ -165,7 +165,7 @@ def __init__(self, jwks_url: str, cache_ttl: int = CACHE_TTL) -> None:

def _init_cache(self, cache_ttl: int) -> None:
self._cache_value: dict[str, RSAPublicKey] = {}
self._cache_date = 0
self._cache_date = 0.0
self._cache_ttl = cache_ttl
self._cache_is_fresh = False

Expand Down
20 changes: 13 additions & 7 deletions auth0/authentication/users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from __future__ import annotations

from typing import Any

from auth0.rest import RestClient, RestClientOptions
from auth0.types import TimeoutType


class Users:
Expand All @@ -13,11 +18,11 @@ class Users:

def __init__(
self,
domain,
telemetry=True,
timeout=5.0,
protocol="https",
):
domain: str,
telemetry: bool = True,
timeout: TimeoutType = 5.0,
protocol: str = "https",
) -> None:
self.domain = domain
self.protocol = protocol
self.client = RestClient(
Expand All @@ -31,7 +36,7 @@ def __init__(
domain (str): Your auth0 domain (e.g: username.auth0.com)
"""

def userinfo(self, access_token):
def userinfo(self, access_token: str) -> dict[str, Any]:
"""Returns the user information based on the Auth0 access token.
This endpoint will work only if openid was granted as a scope for the access_token.
Expand All @@ -42,7 +47,8 @@ def userinfo(self, access_token):
The user profile.
"""

return self.client.get(
data: dict[str, Any] = self.client.get(
url=f"{self.protocol}://{self.domain}/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
)
return data
25 changes: 8 additions & 17 deletions auth0/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,20 @@ class RestClientOptions:

def __init__(
self,
telemetry: bool | None = None,
timeout: TimeoutType | None = None,
retries: int | None = None,
telemetry: bool = True,
timeout: TimeoutType = 5.0,
retries: int = 3,
) -> None:
self.telemetry = True
self.timeout = 5.0
self.retries = 3

if telemetry is not None:
self.telemetry = telemetry

if timeout is not None:
self.timeout = timeout

if retries is not None:
self.retries = retries
self.telemetry = telemetry
self.timeout = timeout
self.retries = retries


class RestClient:
"""Provides simple methods for handling all RESTful api endpoints.
Args:
jwt (str): The JWT to be used with the RestClient.
jwt (str, optional): The JWT to be used with the RestClient.
telemetry (bool, optional): Enable or disable Telemetry
(defaults to True)
timeout (float or tuple, optional): Change the requests
Expand All @@ -79,7 +70,7 @@ class RestClient:

def __init__(
self,
jwt: str,
jwt: str | None,
telemetry: bool = True,
timeout: TimeoutType = 5.0,
options: RestClientOptions | None = None,
Expand Down
3 changes: 2 additions & 1 deletion auth0/rest_async.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# mypy: disable-error-code=override
from __future__ import annotations

import asyncio
Expand Down Expand Up @@ -36,7 +37,7 @@ class AsyncRestClient(RestClient):

def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._session = None
self._session: aiohttp.ClientSession | None = None
sock_connect, sock_read = (
self.timeout
if isinstance(self.timeout, tuple)
Expand Down
8 changes: 8 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mypy]
python_version = 3.7

[mypy-auth0.test.*,auth0.test_async.*]
ignore_errors = True

[mypy-auth0.management.*]
ignore_errors = True

0 comments on commit 7b93c5e

Please sign in to comment.