Skip to content

Commit ea91d57

Browse files
committed
Add the base code and Google oauth
1 parent c2bb1e5 commit ea91d57

File tree

17 files changed

+827
-0
lines changed

17 files changed

+827
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
__pycache__/
2+
.pytest_cache/
3+
.pdm.toml
4+
.pdm-python
5+
.pdm-build/
6+
.env
7+
.venv
8+
venv/
9+
.idea/
10+
.vscode/
11+
.ruff_cache/
12+
dist/

.pre-commit-config.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.5.0
4+
hooks:
5+
- id: end-of-file-fixer
6+
- id: check-yaml
7+
- id: check-toml
8+
9+
- repo: https://github.com/charliermarsh/ruff-pre-commit
10+
rev: v0.2.0
11+
hooks:
12+
- id: ruff
13+
args:
14+
- '--fix'
15+
- id: ruff-format
16+
17+
- repo: https://github.com/pdm-project/pdm
18+
rev: 2.12.3
19+
hooks:
20+
- id: pdm-lock-check

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# fastapi_oauth20
2+
3+
在 fastapi 中轻松集成 OAuth 2.0 客户端
4+
5+
**WIP.**
6+
7+
我们的目标是集成多个 CN 第三方客户端,敬请期待
8+
9+
感谢 [httpx_oauth](https://github.com/frankie567/httpx-oauth) 鼎力相助
10+
11+
#### TODO:
12+
13+
如果我们能够很容易获取测试客户端,对接将会很快发生
14+
15+
- [ ] 微信
16+
- [ ] QQ
17+
- [ ] 抖音
18+
- [ ] 飞书
19+
- [ ] 钉钉
20+
- [ ] 微博
21+
- [ ] 百度
22+
- [ ] Gitee
23+
- [ ] Github
24+
- [ ] 开源中国
25+
- [ ] 阿里云
26+
- [ ] TestHome

example/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-

example/fastapi.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import os
4+
5+
from fastapi import Depends, FastAPI
6+
from fastapi_oauth20.clients.google import GoogleOAuth20
7+
from fastapi_oauth20.integrations.fastapi import OAuth20
8+
from starlette.responses import PlainTextResponse
9+
10+
app = FastAPI()
11+
12+
GOOGLE_CLIENT_ID = os.environ.get('GOOGLE_CLIENT_ID')
13+
GOOGLE_CLIENT_SECRET = os.environ.get('GOOGLE_CLIENT_SECRET')
14+
GOOGLE_REDIRECT_URI = 'http://localhost:8000/auth/google'
15+
16+
GOOGLE_OAUTH2 = GoogleOAuth20(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
17+
18+
19+
@app.get('/login/google')
20+
async def login_google():
21+
return GOOGLE_OAUTH2.get_authorization_url(redirect_uri='GOOGLE_REDIRECT_URI')
22+
23+
24+
@app.get('/auth/google')
25+
async def auth_google(oauth: OAuth20 = Depends()):
26+
token, state = oauth
27+
access_token = token['access_token']
28+
print(access_token)
29+
# do something
30+
userinfo = GOOGLE_OAUTH2.get_userinfo(access_token)
31+
print(userinfo)
32+
# do something
33+
return PlainTextResponse(content='恭喜你,OAuth2 登录成功', status_code=200)

pdm.lock

Lines changed: 464 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pre-commit.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
pre-commit run --all-files --verbose --show-diff-on-failure

pyproject.toml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[project]
2+
name = "fastapi_oauth20"
3+
version = "0.0.1"
4+
description = "FastAPI easily integrates with clients OAuth 2.0"
5+
authors = [
6+
{ name = "Wu Clan", email = "[email protected]" },
7+
]
8+
dependencies = [
9+
"httpx>=0.18.0",
10+
"fastapi>=0.100.0",
11+
]
12+
requires-python = ">=3.8"
13+
readme = "README.md"
14+
license = { text = "MIT" }
15+
16+
[tool.pdm.dev-dependencies]
17+
lint = [
18+
"ruff>=0.2.0",
19+
"pre-commit>=3.5.0",
20+
]
21+
22+
[tool.ruff]
23+
line-length = 120
24+
cache-dir = "./.ruff_cache"
25+
26+
[tool.ruff.lint]
27+
extend-select = ["I"]
28+
29+
[tool.ruff.lint.isort]
30+
lines-between-types = 1
31+
order-by-type = true
32+
33+
[tool.ruff.format]
34+
quote-style = "single"
35+
36+
[tool.pdm]
37+
package-type = "library"
38+
39+
[build-system]
40+
requires = ["pdm-backend"]
41+
build-backend = "pdm.backend"

src/fastapi_oauth20/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-

0 commit comments

Comments
 (0)