Skip to content

Commit

Permalink
Add Gitee OAuth2 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan authored Feb 6, 2024
1 parent 48dcbcc commit 4419f9e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

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

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

import uvicorn

from fastapi import Depends, FastAPI
Expand Down
36 changes: 36 additions & 0 deletions src/fastapi_oauth20/clients/gitee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import httpx

from fastapi_oauth20.oauth20 import OAuth20Base

AUTHORIZE_ENDPOINT = 'https://gitee.com/oauth/authorize'
ACCESS_TOKEN_ENDPOINT = 'https://gitee.com/oauth/token'
REFRESH_TOKEN_ENDPOINT = ACCESS_TOKEN_ENDPOINT
DEFAULT_SCOPES = ['user_info']
PROFILE_ENDPOINT = 'https://gitee.com/api/v5/user'


class GiteeOAuth20(OAuth20Base):
def __init__(self, client_id: str, client_secret: str):
super().__init__(
client_id=client_id,
client_secret=client_secret,
authorize_endpoint=AUTHORIZE_ENDPOINT,
access_token_endpoint=ACCESS_TOKEN_ENDPOINT,
refresh_token_endpoint=REFRESH_TOKEN_ENDPOINT,
revoke_token_endpoint=None,
oauth_callback_route_name='gitee',
default_scopes=DEFAULT_SCOPES,
)

async def get_userinfo(self, access_token: str) -> dict:
"""Get user info from Gitee"""
headers = {'Authorization': f'Bearer {access_token}'}
async with httpx.AsyncClient() as client:
response = await client.get(PROFILE_ENDPOINT, headers=headers)
await self.raise_httpx_oauth20_errors(response)

res = response.json()

return res
4 changes: 2 additions & 2 deletions src/fastapi_oauth20/clients/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ async def get_userinfo(self, access_token: str) -> dict:
response = await client.get(PROFILE_ENDPOINT, headers=headers)
await self.raise_httpx_oauth20_errors(response)

res = response.json()
res = response.json()

return res
return res
8 changes: 8 additions & 0 deletions src/fastapi_oauth20/oauth20.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import abc

from urllib.parse import urlencode, urljoin

import httpx
Expand Down Expand Up @@ -126,7 +128,13 @@ async def revoke_token(self, token: str, token_type_hint: str | None = None) ->

@staticmethod
async def raise_httpx_oauth20_errors(response: httpx.Response) -> None:
"""Raise HTTPXOAuth20Error if the response is invalid"""
try:
response.raise_for_status()
except httpx.HTTPStatusError as e:
raise HTTPXOAuth20Error(e) from e

@abc.abstractmethod
async def get_userinfo(self, access_token: str) -> dict:
"""Get user info"""
...

0 comments on commit 4419f9e

Please sign in to comment.