Skip to content

Commit 48dcbcc

Browse files
authored
Add the base code with Google oauth (#1)
* Add the base code and Google oauth * fix client program and integration * add ci and funding
1 parent c2bb1e5 commit 48dcbcc

File tree

19 files changed

+927
-0
lines changed

19 files changed

+927
-0
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom: https://wu-clan.github.io/sponsor/

.github/workflows/ci.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
tags:
8+
- '**'
9+
pull_request:
10+
11+
jobs:
12+
lint:
13+
runs-on: ubuntu-latest
14+
name: lint ${{ matrix.python-version }}
15+
strategy:
16+
matrix:
17+
python-version: [ '3.10', '3.11', '3.12' ]
18+
fail-fast: false
19+
steps:
20+
- uses: actions/checkout@v3
21+
22+
- name: Setup PDM
23+
uses: pdm-project/setup-pdm@v3
24+
with:
25+
python-version: ${{ matrix.python-version }}
26+
27+
- name: Install dependencies
28+
run: |
29+
pdm sync -dG lint
30+
31+
# - name: Run Tests
32+
# run: |
33+
# pdm run -v pytest tests
34+
35+
- name: pre-commit
36+
uses: pre-commit/action@v3.0.0
37+
with:
38+
extra_args: --all-files --verbose

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import uvicorn
5+
6+
from fastapi import Depends, FastAPI
7+
from fastapi_oauth20.clients.google import GoogleOAuth20
8+
from fastapi_oauth20.integrations.fastapi import OAuth20
9+
from starlette.responses import PlainTextResponse
10+
11+
app = FastAPI()
12+
13+
GOOGLE_CLIENT_ID = '1053650337583-ljnla4m1e5cg16erq3tld5vjflqh4bij.apps.googleusercontent.com'
14+
GOOGLE_CLIENT_SECRET = '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'
15+
GOOGLE_REDIRECT_URI = 'http://localhost:8000/auth/google'
16+
17+
google_client = GoogleOAuth20(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
18+
oauth20 = OAuth20(google_client, GOOGLE_REDIRECT_URI)
19+
20+
21+
@app.get('/login/google')
22+
async def login_google():
23+
return await google_client.get_authorization_url(redirect_uri=GOOGLE_REDIRECT_URI)
24+
25+
26+
@app.get('/auth/google', response_model=None)
27+
async def auth_google(oauth: oauth20 = Depends()):
28+
token, state = oauth
29+
access_token = token['access_token']
30+
print(access_token)
31+
# do something
32+
userinfo = await google_client.get_userinfo(access_token)
33+
print(userinfo)
34+
# do something
35+
return PlainTextResponse(content='恭喜你,OAuth2 登录成功')
36+
37+
38+
if __name__ == '__main__':
39+
uvicorn.run('main:app', reload=True)

0 commit comments

Comments
 (0)