Skip to content

Commit 7a5ae35

Browse files
authored
Update coding styles in code and docs (#31)
1 parent 61a6200 commit 7a5ae35

28 files changed

Lines changed: 91 additions & 131 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ pip install fastapi_oauth20
2121

2222
## Sponsor
2323

24-
如果此项目能够帮助到你,你可以赞助作者一些咖啡豆表示鼓励:[:coffee: Sponsor :coffee:](https://wu-clan.github.io/sponsor/)
24+
如果这个项目对你有帮助,欢迎[请作者喝杯咖啡](https://wu-clan.github.io/sponsor/)

docs/index.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,37 @@
1414

1515
### Installation
1616

17-
```bash
18-
pip install fastapi-oauth20
19-
```
17+
=== "pip"
18+
19+
```bash
20+
pip install fastapi-oauth20
21+
```
22+
23+
=== "uv"
24+
25+
```bash
26+
uv add fastapi-oauth20
27+
```
2028

2129
### Basic Usage
2230

2331
```python
32+
from typing import Annotated
33+
2434
from fastapi import FastAPI, Depends
25-
from fastapi_oauth20 import GitHubOAuth20, FastAPIOAuth20
2635
from fastapi.responses import RedirectResponse
27-
import secrets
36+
37+
from fastapi_oauth20 import GitHubOAuth20, FastAPIOAuth20
2838

2939
app = FastAPI()
3040

31-
# 定义重定向地址
3241
redirect_uri = "http://localhost:8000/auth/github/callback"
3342

34-
# 初始化 GitHub OAuth2 客户端
3543
github_client = GitHubOAuth20(
3644
client_id="your_github_client_id",
3745
client_secret="your_github_client_secret"
3846
)
3947

40-
# 创建 FastAPI OAuth2 依赖项
41-
github_oauth = FastAPIOAuth20(
42-
client=github_client,
43-
redirect_uri=redirect_uri
44-
)
45-
4648

4749
@app.get("/auth/github")
4850
async def github_auth():
@@ -51,16 +53,14 @@ async def github_auth():
5153

5254

5355
@app.get("/auth/github/callback")
54-
async def github_callback(oauth_result: tuple = Depends(github_oauth)):
55-
token_data, state = oauth_result
56-
user_info = await github_client.get_userinfo(token_data["access_token"])
56+
async def github_callback(
57+
oauth2: Annotated[
58+
FastAPIOAuth20,
59+
Depends(FastAPIOAuth20(github_client, redirect_uri=redirect_uri)),
60+
],
61+
):
62+
token_data, state = oauth2
63+
access_token = token_data['access_token']
64+
user_info = await github_client.get_userinfo(access_token)
5765
return {"user": user_info}
5866
```
59-
60-
## 互动
61-
62-
[TG / Discord](https://wu-clan.github.io/homepage/)
63-
64-
## 赞助
65-
66-
如果此项目能够帮助到你,你可以赞助作者一些咖啡豆表示鼓励:[Sponsor](https://wu-clan.github.io/sponsor/)

docs/status.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
下面展示了我们的计划,如果你有更多需求,请在仓库内创建 Issues,我们将尽力完成所有目标
22

3-
!!! danger
4-
5-
对于强制要求【实名 + 人脸认证】的平台,植入变得困难,所以它们不会很快到来
6-
73
## FINISHED
84

95
- [x] [LinuxDo](clients/linuxdo.md)
@@ -20,5 +16,3 @@
2016
- [ ] [企业微信二维码登录](clients/wechat_work.md)
2117
- [ ] [钉钉](clients/dingtalk.md)
2218
- [ ] [QQ](clients/qq.md)
23-
- [ ] [微博](clients/weibo.md)
24-
- [ ] [抖音](clients/douyin.md)

docs/usage.md

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ async def oauth2_github():
5656
```python
5757
@app.get("/oauth2/github/callback")
5858
async def oauth2_github_callback(
59-
oauth_result: Annotated[FastAPIOAuth20, Depends(github_oauth)]
59+
oauth_result: Annotated[
60+
FastAPIOAuth20,
61+
Depends(github_oauth),
62+
],
6063
):
6164
"""处理 GitHub OAuth 回调"""
6265
token_data, state = oauth_result
@@ -131,46 +134,37 @@ async def oauth2_authorize_callback_error_handler(request: Request, exc: OAuth20
131134
```python
132135
from typing import Annotated
133136

134-
from fastapi import FastAPI, Depends, HTTPException
137+
from fastapi import FastAPI, Depends
135138
from fastapi.responses import RedirectResponse
136-
from fastapi_oauth20 import GitHubOAuth20, FastAPIOAuth20
137-
from fastapi_oauth20 import OAuth20AuthorizeCallbackError
138139

139-
import secrets
140+
from fastapi_oauth20 import GitHubOAuth20, FastAPIOAuth20
140141

141142
app = FastAPI()
142143

143-
# 初始化客户端
144-
github_client = GitHubOAuth20(
145-
client_id="your_client_id",
146-
client_secret="your_client_secret"
147-
)
144+
redirect_uri = "http://localhost:8000/auth/github/callback"
148145

149-
# 创建依赖
150-
github_oauth = FastAPIOAuth20(
151-
client=github_client,
152-
redirect_uri="http://localhost:8000/auth/github/callback"
146+
github_client = GitHubOAuth20(
147+
client_id="your_github_client_id",
148+
client_secret="your_github_client_secret"
153149
)
154150

155151

156-
@app.get("/oauth2/github")
157-
async def oauth2_github():
158-
"""GitHub 授权入口"""
159-
state = secrets.token_urlsafe(32)
160-
auth_url = await github_client.get_authorization_url(
161-
redirect_uri="http://localhost:8000/auth/github/callback",
162-
state=state
163-
)
152+
@app.get("/auth/github")
153+
async def github_auth():
154+
auth_url = await github_client.get_authorization_url(redirect_uri=redirect_uri)
164155
return RedirectResponse(url=auth_url)
165156

166157

167-
@app.get("/oauth2/github/callback")
168-
async def oauth2_github_callback(
169-
oauth_result: Annotated[FastAPIOAuth20, Depends(github_oauth)]
158+
@app.get("/auth/github/callback")
159+
async def github_callback(
160+
oauth2: Annotated[
161+
FastAPIOAuth20,
162+
Depends(FastAPIOAuth20(github_client, redirect_uri=redirect_uri)),
163+
],
170164
):
171-
"""GitHub 授权回调"""
172-
token_data, state = oauth_result
173-
user_info = await github_client.get_userinfo(token_data["access_token"])
165+
token_data, state = oauth2
166+
access_token = token_data['access_token']
167+
user_info = await github_client.get_userinfo(access_token)
174168
return {"user": user_info}
175169

176170

fastapi_oauth20/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
31
from .callback import FastAPIOAuth20 as FastAPIOAuth20
42
from .callback import OAuth20AuthorizeCallbackError as OAuth20AuthorizeCallbackError
53
from .clients.feishu import FeiShuOAuth20 as FeiShuOAuth20

fastapi_oauth20/callback.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
3-
from typing import Any
1+
from typing import Annotated, Any
42

53
import httpx
64

7-
from fastapi import HTTPException, Request
5+
from fastapi import HTTPException, Query, Request
86

97
from fastapi_oauth20.errors import OAuth20BaseError, OAuth20RequestError
108
from fastapi_oauth20.oauth20 import OAuth20Base
@@ -53,10 +51,10 @@ def __init__(
5351
async def __call__(
5452
self,
5553
request: Request,
56-
code: str | None = None,
57-
state: str | None = None,
58-
code_verifier: str | None = None,
59-
error: str | None = None,
54+
code: Annotated[str | None, Query(description='Authorization code from OAuth2 provider')] = None,
55+
state: Annotated[str | None, Query(description='State parameter for CSRF protection')] = None,
56+
code_verifier: Annotated[str | None, Query(description='PKCE code verifier for enhanced security')] = None,
57+
error: Annotated[str | None, Query(description='Error code if authorization failed')] = None,
6058
) -> tuple[dict[str, Any], str | None]:
6159
"""
6260
Process OAuth2 callback request and exchange authorization code for access token.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-

fastapi_oauth20/clients/feishu.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
3-
41
from fastapi_oauth20.oauth20 import OAuth20Base
52

63

fastapi_oauth20/clients/gitee.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
3-
41
from fastapi_oauth20.oauth20 import OAuth20Base
52

63

fastapi_oauth20/clients/github.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
31
from typing import Any
42

53
import httpx

0 commit comments

Comments
 (0)