Skip to content

Commit 347d7ec

Browse files
authored
Add WeChat OAuth2 (#32)
* Add WeChat OAuth2 * Update testcases
1 parent 7a5ae35 commit 347d7ec

27 files changed

Lines changed: 1002 additions & 512 deletions

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ pip install fastapi_oauth20
1919

2020
[fastapi oauth20](https://fastapi-practices.github.io/fastapi-oauth20/)
2121

22+
## Demo
23+
24+
查看完整的示例项目:[fastapi-oauth20-demo](https://github.com/fastapi-practices/fastapi-oauth20-demo)
25+
26+
该示例项目展示了如何在实际应用中使用 fastapi-oauth20,包括:
27+
28+
- 多个 OAuth2 提供商的集成示例
29+
- 完整的授权流程实现
30+
- 用户信息获取和处理
31+
- 错误处理最佳实践
32+
2233
## Sponsor
2334

2435
如果这个项目对你有帮助,欢迎[请作者喝杯咖啡](https://wu-clan.github.io/sponsor/)

docs/status.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
下面展示了我们的计划,如果你有更多需求,请在仓库内创建 Issues,我们将尽力完成所有目标
1+
如果你有更多需求,请在仓库内创建 [Issues](https://github.com/fastapi-practices/fastapi-oauth20/issues)
22

3-
## FINISHED
4-
5-
- [x] [LinuxDo](clients/linuxdo.md)
63
- [x] [GitHub](clients/github.md)
4+
- [x] [Google](clients/google.md)
5+
- [x] [LinuxDo](clients/linuxdo.md)
76
- [x] [Gitee](clients/gitee.md)
87
- [x] [开源中国](clients/oschina.md)
98
- [x] [飞书](clients/feishu.md)
10-
- [x] [Google](clients/google.md)
9+
- [x] [微信小程序](clients/wechat_open.md)
10+
- [x] [微信开放平台](clients/wechat_mp.md)
1111

1212
## TODO
1313

14-
- [ ] [微信小程序](clients/wechat_open.md)
15-
- [ ] [微信开放平台](clients/wechat_mp.md)
1614
- [ ] [企业微信二维码登录](clients/wechat_work.md)
1715
- [ ] [钉钉](clients/dingtalk.md)
1816
- [ ] [QQ](clients/qq.md)

docs/usage.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ from fastapi_oauth20 import FastAPIOAuth20
44

55
本指南介绍如何将 FastAPI OAuth2.0 库与各种 OAuth2 提供程序一起使用。
66

7+
## 演示项目
8+
9+
在开始之前,强烈推荐查看我们的完整演示项目
10+
11+
**[fastapi-oauth20-demo](https://github.com/fastapi-practices/fastapi-oauth20-demo)**
12+
13+
该演示项目包含:
14+
15+
- 多个 OAuth2 提供商的集成示例
16+
- 详细的代码注释和实现说明
17+
- 生产环境的最佳实践
18+
- 错误处理示例
19+
- 可直接运行的完整应用
20+
21+
通过演示项目,你可以快速了解如何在真实应用中使用 fastapi-oauth20
22+
723
## 基本用法
824

925
### 1. 选择 OAuth2 提供商并初始化客户端

fastapi_oauth20/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
from .clients.google import GoogleOAuth20 as GoogleOAuth20
77
from .clients.linuxdo import LinuxDoOAuth20 as LinuxDoOAuth20
88
from .clients.oschina import OSChinaOAuth20 as OSChinaOAuth20
9+
from .clients.weixin_mp import WeChatMpOAuth20 as WeChatMpOAuth20
10+
from .clients.weixin_open import WeChatOpenOAuth20 as WeChatOpenOAuth20
911

1012
__version__ = '0.0.2'

fastapi_oauth20/callback.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import inspect
2+
13
from typing import Annotated, Any
24

35
import httpx
@@ -25,6 +27,7 @@ def __init__(
2527
:param detail: Error detail message describing what went wrong.
2628
:param headers: Additional HTTP headers to include in the error response.
2729
:param response: The original HTTP response that caused the error (if any).
30+
:return:
2831
"""
2932
self.response = response
3033
super().__init__(status_code=status_code, detail=detail, headers=headers)
@@ -44,6 +47,7 @@ def __init__(
4447
4548
:param client: An OAuth2 client instance that inherits from OAuth20Base.
4649
:param redirect_uri: The full callback URL where the OAuth2 provider redirects after authorization. Must match the URL registered with the OAuth2 provider.
50+
:return:
4751
"""
4852
self.client = client
4953
self.redirect_uri = redirect_uri
@@ -64,19 +68,27 @@ async def __call__(
6468
:param state: The state parameter for CSRF protection (extracted from query parameters).
6569
:param code_verifier: PKCE code verifier if PKCE was used in the authorization request.
6670
:param error: Error parameter from OAuth2 provider if authorization was denied or failed.
71+
:return:
6772
"""
6873
if code is None or error is not None:
6974
raise OAuth20AuthorizeCallbackError(
7075
status_code=400,
7176
detail=error if error is not None else None,
7277
)
7378

79+
kwargs = {'code': code}
80+
7481
try:
75-
access_token = await self.client.get_access_token(
76-
code=code,
77-
redirect_uri=self.redirect_uri,
78-
code_verifier=code_verifier,
79-
)
82+
sig = inspect.signature(self.client.get_access_token)
83+
params = sig.parameters
84+
85+
if 'redirect_uri' in params:
86+
kwargs['redirect_uri'] = self.redirect_uri
87+
88+
if 'code_verifier' in params:
89+
kwargs['code_verifier'] = code_verifier
90+
91+
access_token = await self.client.get_access_token(**kwargs)
8092
except OAuth20RequestError as e:
8193
raise OAuth20AuthorizeCallbackError(
8294
status_code=500,

fastapi_oauth20/clients/feishu.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self, client_id: str, client_secret: str):
1010
1111
:param client_id: FeiShu app client ID from the FeiShu developer console.
1212
:param client_secret: FeiShu app client secret from the FeiShu developer console.
13+
:return:
1314
"""
1415
super().__init__(
1516
client_id=client_id,

fastapi_oauth20/clients/gitee.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self, client_id: str, client_secret: str):
1010
1111
:param client_id: Gitee OAuth application client ID.
1212
:param client_secret: Gitee OAuth application client secret.
13+
:return:
1314
"""
1415
super().__init__(
1516
client_id=client_id,

fastapi_oauth20/clients/github.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self, client_id: str, client_secret: str):
1515
1616
:param client_id: GitHub OAuth App client ID.
1717
:param client_secret: GitHub OAuth App client secret.
18+
:return:
1819
"""
1920
super().__init__(
2021
client_id=client_id,

fastapi_oauth20/clients/google.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self, client_id: str, client_secret: str):
1010
1111
:param client_id: Google OAuth 2.0 client ID from Google Cloud Console.
1212
:param client_secret: Google OAuth 2.0 client secret from Google Cloud Console.
13+
:return:
1314
"""
1415
super().__init__(
1516
client_id=client_id,

fastapi_oauth20/clients/linuxdo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self, client_id: str, client_secret: str):
1010
1111
:param client_id: Linux.do OAuth application client ID.
1212
:param client_secret: Linux.do OAuth application client secret.
13+
:return:
1314
"""
1415
super().__init__(
1516
client_id=client_id,

0 commit comments

Comments
 (0)