Skip to content

Add the base code with Google oauth #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: https://wu-clan.github.io/sponsor/
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: CI

on:
push:
branches:
- master
tags:
- '**'
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
name: lint ${{ matrix.python-version }}
strategy:
matrix:
python-version: [ '3.10', '3.11', '3.12' ]
fail-fast: false
steps:
- uses: actions/checkout@v3

- name: Setup PDM
uses: pdm-project/setup-pdm@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
pdm sync -dG lint

# - name: Run Tests
# run: |
# pdm run -v pytest tests

- name: pre-commit
uses: pre-commit/action@v3.0.0
with:
extra_args: --all-files --verbose
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
__pycache__/
.pytest_cache/
.pdm.toml
.pdm-python
.pdm-build/
.env
.venv
venv/
.idea/
.vscode/
.ruff_cache/
dist/
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.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
hooks:
- id: ruff
args:
- '--fix'
- id: ruff-format

- repo: https://github.com/pdm-project/pdm
rev: 2.12.3
hooks:
- id: pdm-lock-check
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# fastapi_oauth20

在 fastapi 中轻松集成 OAuth 2.0 客户端

全局异步,支持 python >= 3.10

我们的目标是集成多个 CN 第三方客户端,敬请期待

感谢 [httpx_oauth](https://github.com/frankie567/httpx-oauth) 鼎力相助

#### TODO:

如果我们能够很容易获取测试客户端,对接将会很快发生

- [ ] tests
- [ ] 微信
- [ ] QQ
- [ ] 抖音
- [ ] 飞书
- [ ] 钉钉
- [ ] 微博
- [ ] 百度
- [ ] Gitee
- [ ] Github
- [ ] 开源中国
- [ ] 阿里云
- [ ] TestHome
2 changes: 2 additions & 0 deletions example/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
39 changes: 39 additions & 0 deletions example/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import uvicorn

from fastapi import Depends, FastAPI
from fastapi_oauth20.clients.google import GoogleOAuth20
from fastapi_oauth20.integrations.fastapi import OAuth20
from starlette.responses import PlainTextResponse

app = FastAPI()

GOOGLE_CLIENT_ID = '1053650337583-ljnla4m1e5cg16erq3tld5vjflqh4bij.apps.googleusercontent.com'
GOOGLE_CLIENT_SECRET = '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'
GOOGLE_REDIRECT_URI = 'http://localhost:8000/auth/google'

google_client = GoogleOAuth20(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)
oauth20 = OAuth20(google_client, GOOGLE_REDIRECT_URI)


@app.get('/login/google')
async def login_google():
return await google_client.get_authorization_url(redirect_uri=GOOGLE_REDIRECT_URI)


@app.get('/auth/google', response_model=None)
async def auth_google(oauth: oauth20 = Depends()):
token, state = oauth
access_token = token['access_token']
print(access_token)
# do something
userinfo = await google_client.get_userinfo(access_token)
print(userinfo)
# do something
return PlainTextResponse(content='恭喜你,OAuth2 登录成功')


if __name__ == '__main__':
uvicorn.run('main:app', reload=True)
Loading