-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Linux Do OAuth2 * Fix Linux Do get token
- Loading branch information
Showing
11 changed files
with
106 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.5.0 | ||
rev: v4.6.0 | ||
hooks: | ||
- id: end-of-file-fixer | ||
- id: check-yaml | ||
- id: check-toml | ||
|
||
- repo: https://github.com/charliermarsh/ruff-pre-commit | ||
rev: v0.2.0 | ||
rev: v0.5.0 | ||
hooks: | ||
- id: ruff | ||
args: | ||
- '--fix' | ||
- id: ruff-format | ||
|
||
- repo: https://github.com/pdm-project/pdm | ||
rev: 2.12.3 | ||
rev: 2.16.1 | ||
hooks: | ||
- id: pdm-lock-check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
"""在 FastAPI 中异步授权 OAuth2 客户端""" | ||
|
||
__version__ = '0.0.1a1' | ||
__version__ = '0.0.1a2' | ||
|
||
__all__ = ['OSChinaOAuth20', 'GoogleOAuth20', 'FeiShuOAuth20', 'GiteeOAuth20', 'GitHubOAuth20', 'FastAPIOAuth20'] | ||
__all__ = [ | ||
'OSChinaOAuth20', | ||
'GoogleOAuth20', | ||
'FeiShuOAuth20', | ||
'GiteeOAuth20', | ||
'GitHubOAuth20', | ||
'FastAPIOAuth20', | ||
'LinuxDoOAuth20', | ||
] | ||
|
||
from .clients.feishu import FeiShuOAuth20 | ||
from .clients.gitee import GiteeOAuth20 | ||
from .clients.github import GitHubOAuth20 | ||
from .clients.google import GoogleOAuth20 | ||
from .clients.linuxdo import LinuxDoOAuth20 | ||
from .clients.oschina import OSChinaOAuth20 | ||
from .integrations.fastapi import OAuth20 as FastAPIOAuth20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
import httpx | ||
|
||
from fastapi_oauth20.oauth20 import OAuth20Base | ||
|
||
AUTHORIZE_ENDPOINT = 'https://connect.linux.do/oauth2/authorize' | ||
ACCESS_TOKEN_ENDPOINT = 'https://connect.linux.do/oauth2/token' | ||
REFRESH_TOKEN_ENDPOINT = ACCESS_TOKEN_ENDPOINT | ||
REVOKE_TOKEN_ENDPOINT = None | ||
DEFAULT_SCOPES = None | ||
PROFILE_ENDPOINT = 'https://connect.linux.do/api/user' | ||
|
||
|
||
class LinuxDoOAuth20(OAuth20Base): | ||
def __init__(self, client_id: str, client_secret: str): | ||
super().__init__( | ||
client_id=client_id, | ||
client_secret=client_secret, | ||
authorize_endpoint=AUTHORIZE_ENDPOINT, | ||
access_token_endpoint=ACCESS_TOKEN_ENDPOINT, | ||
refresh_token_endpoint=REFRESH_TOKEN_ENDPOINT, | ||
revoke_token_endpoint=REVOKE_TOKEN_ENDPOINT, | ||
oauth_callback_route_name='linuxdo', | ||
default_scopes=DEFAULT_SCOPES, | ||
) | ||
|
||
async def get_access_token(self, code: str, redirect_uri: str, code_verifier: str | None = None) -> dict: | ||
"""Obtain the token based on the Linux do authorization method""" | ||
data = { | ||
'code': code, | ||
'redirect_uri': redirect_uri, | ||
'grant_type': 'authorization_code', | ||
} | ||
|
||
auth = httpx.BasicAuth(self.client_id, self.client_secret) | ||
|
||
if code_verifier: | ||
data.update({'code_verifier': code_verifier}) | ||
async with httpx.AsyncClient(auth=auth) as client: | ||
response = await client.post( | ||
self.access_token_endpoint, | ||
data=data, | ||
headers=self.request_headers, | ||
) | ||
await self.raise_httpx_oauth20_errors(response) | ||
|
||
res = response.json() | ||
|
||
return res | ||
|
||
async def get_userinfo(self, access_token: str) -> dict: | ||
"""Get user info from Linux Do""" | ||
headers = {'Authorization': f'Bearer {access_token}'} | ||
async with httpx.AsyncClient() as client: | ||
response = await client.get(PROFILE_ENDPOINT, headers=headers) | ||
await self.raise_httpx_oauth20_errors(response) | ||
|
||
res = response.json() | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters