Skip to content

Commit

Permalink
run ruff format in tests and docstrings, remove blacken-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
guillp committed Feb 12, 2024
1 parent c93080c commit d764a5a
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 272 deletions.
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ repos:
- tomli
args:
- --in-place
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
hooks:
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ blank = true
[tool.ruff]
target-version = "py38"
line-length = 120
exclude = [
extend-exclude = [
"tests"
]

[tool.ruff.format]
docstring-code-format = true
line-ending = "lf"

[tool.ruff.lint]
select = [
"A",
Expand Down
11 changes: 4 additions & 7 deletions requests_oauth2client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ class OAuth2ClientCredentialsAuth(BaseOAuth2RenewableTokenAuth):
Usage:
```python
client = OAuth2Client(
token_endpoint="https://my.as.local/token", auth=("client_id", "client_secret")
)
client = OAuth2Client(token_endpoint="https://my.as.local/token", auth=("client_id", "client_secret"))
oauth2cc = OAuth2ClientCredentialsAuth(client, scope="my_scope")
resp = requests.post("https://my.api.local/resource", auth=oauth2cc)
```
Expand Down Expand Up @@ -197,9 +195,8 @@ class OAuth2AccessTokenAuth(BaseOAuth2RenewableTokenAuth):
```python
client = OAuth2Client(token_endpoint="https://my.as.local/token", auth=("client_id", "client_secret"))
token = BearerToken(
access_token="access_token",
expires_in=600,
refresh_token="refresh_token") # obtain a BearerToken any way you see fit, including a refresh token
access_token="access_token", expires_in=600, refresh_token="refresh_token"
) # obtain a BearerToken any way you see fit, including a refresh token
oauth2at_auth = OAuth2ClientCredentialsAuth(client, token, scope="my_scope")
resp = requests.post("https://my.api.local/resource", auth=oauth2at_auth)
````
Expand Down Expand Up @@ -227,7 +224,7 @@ class OAuth2AuthorizationCodeAuth(OAuth2AccessTokenAuth):
Usage:
```python
client = OAuth2Client(token_endpoint="https://my.as.local/token", auth=("client_id", "client_secret"))
code = "my_code" # you must obtain this code yourself
code = "my_code" # you must obtain this code yourself
resp = requests.post("https://my.api.local/resource", auth=OAuth2AuthorizationCodeAuth(client, code))
````
Expand Down
280 changes: 139 additions & 141 deletions requests_oauth2client/vendor_specific/auth0.py
Original file line number Diff line number Diff line change
@@ -1,141 +1,139 @@
"""Implements subclasses for [Auth0](https://auth0.com)."""

from __future__ import annotations

from typing import Any

import requests
from jwskate import Jwk

from requests_oauth2client import ApiClient, OAuth2Client, OAuth2ClientCredentialsAuth


class Auth0:
"""Auth0-related utilities."""

@classmethod
def tenant(cls, tenant: str) -> str:
"""Given a short tenant name, returns the full tenant FQDN."""
if not tenant:
msg = "You must specify a tenant name."
raise ValueError(msg)
if (
"." not in tenant
or tenant.endswith(".eu")
or tenant.endswith(".us")
or tenant.endswith(".au")
or tenant.endswith(".jp")
):
tenant = f"{tenant}.auth0.com"
if "://" in tenant:
if tenant.startswith("https://"):
return tenant[8:]
msg = (
"Invalid tenant name. "
"It must be a tenant name like 'mytenant.myregion' "
"or a full FQDN like 'mytenant.myregion.auth0.com'."
"or an issuer like 'https://mytenant.myregion.auth0.com'"
)
raise ValueError(msg)
return tenant

@classmethod
def client(
cls,
tenant: str,
auth: (
requests.auth.AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
) = None,
*,
client_id: str | None = None,
client_secret: str | None = None,
private_jwk: Any | None = None,
session: requests.Session | None = None,
**kwargs: Any,
) -> OAuth2Client:
"""Initialise an OAuth2Client for an Auth0 tenant."""
tenant = cls.tenant(tenant)
token_endpoint = f"https://{tenant}/oauth/token"
authorization_endpoint = f"https://{tenant}/authorize"
revocation_endpoint = f"https://{tenant}/oauth/revoke"
userinfo_endpoint = f"https://{tenant}/userinfo"
jwks_uri = f"https://{tenant}/.well-known/jwks.json"

return OAuth2Client(
auth=auth,
client_id=client_id,
client_secret=client_secret,
private_jwk=private_jwk,
session=session,
token_endpoint=token_endpoint,
authorization_endpoint=authorization_endpoint,
revocation_endpoint=revocation_endpoint,
userinfo_endpoint=userinfo_endpoint,
issuer=tenant,
jwks_uri=jwks_uri,
**kwargs,
)

@classmethod
def management_api_client(
cls,
tenant: str,
auth: (
requests.auth.AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
) = None,
*,
client_id: str | None = None,
client_secret: str | None = None,
private_jwk: Any | None = None,
session: requests.Session | None = None,
**kwargs: Any,
) -> ApiClient:
"""Initialize a client for the Auth0 Management API.
See [Auth0 Management API v2](https://auth0.com/docs/api/management/v2). You must provide the
target tenant name and the credentials for a client that is allowed access to the Management
API.
Args:
tenant: the tenant name.
Same definition as for [Auth0.client][requests_oauth2client.vendor_specific.auth0.Auth0.client]
auth: client credentials.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
client_id: the Client ID.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
client_secret: the Client Secret.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
private_jwk: the private key to use for client authentication.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
session: requests session.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
**kwargs: additional kwargs to pass to the ApiClient base class
Usage:
```python
from requests_oauth2client.vendor_specific import Auth0
a0mgmt = Auth0.management_api_client(
"mytenant.eu", client_id=client_id, client_secret=client_secret
)
users = a0mgmt.get("users", params={"page": 0, "per_page": 100})
```
"""
tenant = cls.tenant(tenant)
client = cls.client(
tenant,
auth=auth,
client_id=client_id,
client_secret=client_secret,
private_jwk=private_jwk,
session=session,
)
audience = f"https://{tenant}/api/v2/"
api_auth = OAuth2ClientCredentialsAuth(client, audience=audience)
return ApiClient(
base_url=audience,
auth=api_auth,
session=session,
**kwargs,
)
"""Implements subclasses for [Auth0](https://auth0.com)."""

from __future__ import annotations

from typing import Any

import requests
from jwskate import Jwk

from requests_oauth2client import ApiClient, OAuth2Client, OAuth2ClientCredentialsAuth


class Auth0:
"""Auth0-related utilities."""

@classmethod
def tenant(cls, tenant: str) -> str:
"""Given a short tenant name, returns the full tenant FQDN."""
if not tenant:
msg = "You must specify a tenant name."
raise ValueError(msg)
if (
"." not in tenant
or tenant.endswith(".eu")
or tenant.endswith(".us")
or tenant.endswith(".au")
or tenant.endswith(".jp")
):
tenant = f"{tenant}.auth0.com"
if "://" in tenant:
if tenant.startswith("https://"):
return tenant[8:]
msg = (
"Invalid tenant name. "
"It must be a tenant name like 'mytenant.myregion' "
"or a full FQDN like 'mytenant.myregion.auth0.com'."
"or an issuer like 'https://mytenant.myregion.auth0.com'"
)
raise ValueError(msg)
return tenant

@classmethod
def client(
cls,
tenant: str,
auth: (
requests.auth.AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
) = None,
*,
client_id: str | None = None,
client_secret: str | None = None,
private_jwk: Any | None = None,
session: requests.Session | None = None,
**kwargs: Any,
) -> OAuth2Client:
"""Initialise an OAuth2Client for an Auth0 tenant."""
tenant = cls.tenant(tenant)
token_endpoint = f"https://{tenant}/oauth/token"
authorization_endpoint = f"https://{tenant}/authorize"
revocation_endpoint = f"https://{tenant}/oauth/revoke"
userinfo_endpoint = f"https://{tenant}/userinfo"
jwks_uri = f"https://{tenant}/.well-known/jwks.json"

return OAuth2Client(
auth=auth,
client_id=client_id,
client_secret=client_secret,
private_jwk=private_jwk,
session=session,
token_endpoint=token_endpoint,
authorization_endpoint=authorization_endpoint,
revocation_endpoint=revocation_endpoint,
userinfo_endpoint=userinfo_endpoint,
issuer=tenant,
jwks_uri=jwks_uri,
**kwargs,
)

@classmethod
def management_api_client(
cls,
tenant: str,
auth: (
requests.auth.AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
) = None,
*,
client_id: str | None = None,
client_secret: str | None = None,
private_jwk: Any | None = None,
session: requests.Session | None = None,
**kwargs: Any,
) -> ApiClient:
"""Initialize a client for the Auth0 Management API.
See [Auth0 Management API v2](https://auth0.com/docs/api/management/v2). You must provide the
target tenant name and the credentials for a client that is allowed access to the Management
API.
Args:
tenant: the tenant name.
Same definition as for [Auth0.client][requests_oauth2client.vendor_specific.auth0.Auth0.client]
auth: client credentials.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
client_id: the Client ID.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
client_secret: the Client Secret.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
private_jwk: the private key to use for client authentication.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
session: requests session.
Same definition as for [OAuth2Client][requests_oauth2client.client.OAuth2Client]
**kwargs: additional kwargs to pass to the ApiClient base class
Usage:
```python
from requests_oauth2client.vendor_specific import Auth0
a0mgmt = Auth0.management_api_client("mytenant.eu", client_id=client_id, client_secret=client_secret)
users = a0mgmt.get("users", params={"page": 0, "per_page": 100})
```
"""
tenant = cls.tenant(tenant)
client = cls.client(
tenant,
auth=auth,
client_id=client_id,
client_secret=client_secret,
private_jwk=private_jwk,
session=session,
)
audience = f"https://{tenant}/api/v2/"
api_auth = OAuth2ClientCredentialsAuth(client, audience=audience)
return ApiClient(
base_url=audience,
auth=api_auth,
session=session,
**kwargs,
)
Loading

0 comments on commit d764a5a

Please sign in to comment.