jam.aio.oauth2¶
oauth2
¶
Async OAuth2 module.
Modules:
| Name | Description |
|---|---|
builtin |
Async builtin OAuth2 providers. |
client |
|
Classes:
| Name | Description |
|---|---|
BaseOAuth2Client |
Base OAuth2 client instance. |
GitHubOAuth2Client |
Async ready to use GitHub OAuth2 provider. |
GitLabOAuth2Client |
Async ready to use GitLab OAuth2 provider. |
GoogleOAuth2Client |
Async ready to use Google OAuth2 provider. |
OAuth2Client |
Async universal OAuth2 client implementation. |
YandexOAuth2Client |
Async ready to use yandex oauth2 client. |
Functions:
| Name | Description |
|---|---|
create_instance |
Create async OAuth2 clients for configured providers. |
BaseOAuth2Client
¶
BaseOAuth2Client(
client_id: str,
client_secret: str,
auth_url: str,
token_url: str,
redirect_url: str,
serializer: BaseEncoder
| type[BaseEncoder] = JsonEncoder,
)
Bases: ABC
Base OAuth2 client instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
ID or your client |
required |
client_secret
|
str
|
Secret key for your application |
required |
auth_url
|
str
|
App auth url |
required |
token_url
|
str
|
App token url |
required |
redirect_url
|
str
|
Your app url |
required |
serializer
|
Union[BaseEncoder, type[BaseEncoder]]
|
JSON encoder/decoder. Defaults to JsonEncoder. |
JsonEncoder
|
Methods:
| Name | Description |
|---|---|
client_credentials_flow |
Obtain access token using client credentials flow (no user interaction). |
fetch_token |
Exchange code for access token. |
get_authorization_url |
Get OAuth2 url. |
refresh_token |
Update access token. |
client_credentials_flow
abstractmethod
¶
client_credentials_flow(
scope: list[str] | None = None,
) -> dict[str, Any]
Obtain access token using client credentials flow (no user interaction).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Optional[list[str]]
|
Auth scope |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
JSON with access token |
fetch_token
abstractmethod
¶
fetch_token(
code: str, grant_type: str = "authorization_code"
) -> dict[str, Any]
Exchange code for access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Auth code |
required |
grant_type
|
str
|
Grant type |
'authorization_code'
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
OAuth2 token response |
get_authorization_url
abstractmethod
¶
get_authorization_url(scope: list[str]) -> str
Get OAuth2 url.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
list[str]
|
Auth scope |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
URL for auth |
refresh_token
abstractmethod
¶
refresh_token(
refresh_token: str, grant_type: str = "refresh_token"
) -> dict[str, Any]
Update access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refresh_token
|
str
|
Refresh token |
required |
grant_type
|
str
|
Grant type |
'refresh_token'
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
New token response |
GitHubOAuth2Client
¶
GitHubOAuth2Client(
client_id: str, client_secret: str, redirect_url: str
)
Bases: OAuth2Client
Async ready to use GitHub OAuth2 provider.
See: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
ID of your app |
required |
client_secret
|
str
|
Secret key |
required |
redirect_url
|
str
|
URL for your app |
required |
GitLabOAuth2Client
¶
GitLabOAuth2Client(
client_id: str, client_secret: str, redirect_url: str
)
Bases: OAuth2Client
Async ready to use GitLab OAuth2 provider.
See: https://docs.gitlab.com/api/oauth2/
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
Client ID |
required |
client_secret
|
str
|
Secret key |
required |
redirect_url
|
str
|
Your app url |
required |
GoogleOAuth2Client
¶
GoogleOAuth2Client(
client_id: str, client_secret: str, redirect_url: str
)
Bases: OAuth2Client
Async ready to use Google OAuth2 provider.
See: https://developers.google.com/identity/protocols/oauth2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
Client ID |
required |
client_secret
|
str
|
Secret key |
required |
redirect_url
|
str
|
URL for your app |
required |
OAuth2Client
¶
OAuth2Client(
client_id: str,
client_secret: str,
auth_url: str,
token_url: str,
redirect_url: str,
serializer: BaseEncoder
| type[BaseEncoder] = JsonEncoder,
)
Bases: BaseAsyncOAuth2Client
Async universal OAuth2 client implementation.
Methods:
| Name | Description |
|---|---|
client_credentials_flow |
Obtain access token using client credentials flow (no user interaction). |
fetch_token |
Exchange authorization code for access token. |
get_authorization_url |
Generate full OAuth2 authorization URL. |
refresh_token |
Use refresh token to obtain a new access token. |
__post_form
async
¶
__post_form(
url: str, params: dict[str, Any]
) -> dict[str, Any]
Send POST form and parse JSON response (async version).
client_credentials_flow
async
¶
client_credentials_flow(
scope: list[str] | None = None, **extra_params: Any
) -> dict[str, Any]
Obtain access token using client credentials flow (no user interaction).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
list[str] | None
|
Auth scope |
None
|
extra_params
|
Any
|
Extra auth params if needed |
{}
|
Raises:
| Type | Description |
|---|---|
JamOAuth2EmptyRaw
|
If response is empty |
JamOAuth2Error
|
HTTP error |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
JSON with access token |
fetch_token
async
¶
fetch_token(
code: str,
grant_type: str = "authorization_code",
**extra_params: Any,
) -> dict[str, Any]
Exchange authorization code for access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
OAuth2 code |
required |
grant_type
|
str
|
Type of oauth2 grant |
'authorization_code'
|
extra_params
|
Any
|
Extra auth params if needed |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
OAuth2 token |
get_authorization_url
async
¶
get_authorization_url(
scope: list[str], **extra_params: Any
) -> str
Generate full OAuth2 authorization URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
list[str]
|
Auth scope |
required |
extra_params
|
Any
|
Extra auth params |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Authorization url |
refresh_token
async
¶
refresh_token(
refresh_token: str,
grant_type: str = "refresh_token",
**extra_params: Any,
) -> dict[str, Any]
Use refresh token to obtain a new access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refresh_token
|
str
|
Refresh token |
required |
grant_type
|
str
|
Grant type |
'refresh_token'
|
extra_params
|
Any
|
Extra auth params if needed |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
Refresh token |
YandexOAuth2Client
¶
YandexOAuth2Client(
client_id: str, client_secret: str, redirect_url: str
)
Bases: OAuth2Client
Async ready to use yandex oauth2 client.
See: https://yandex.ru/dev/id/doc
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
Client ID |
required |
client_secret
|
str
|
Secret key |
required |
redirect_url
|
str
|
Your app URL |
required |
create_instance
¶
create_instance(
providers: dict[str, dict],
logger: BaseLogger = logger,
serializer: BaseEncoder
| type[BaseEncoder] = JsonEncoder,
**kwargs: Any,
) -> dict[str, BaseOAuth2Client]
Create async OAuth2 clients for configured providers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
providers
|
dict[str, dict]
|
{provider_name: {client_id, client_secret, redirect_uri, ...}} |
required |
logger
|
BaseLogger
|
Logger instance |
logger
|
serializer
|
BaseEncoder | type[BaseEncoder]
|
JSON encoder/decoder |
JsonEncoder
|
**kwargs
|
Any
|
Additional params |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, BaseOAuth2Client]
|
{provider_name: OAuth2Client instance (async)} |