Skip to content

Commit 4419f9e

Browse files
authored
Add Gitee OAuth2 (#2)
1 parent 48dcbcc commit 4419f9e

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

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

15-
- [ ] tests
15+
- [x] Google
1616
- [ ] 微信
1717
- [ ] QQ
1818
- [ ] 抖音
1919
- [ ] 飞书
2020
- [ ] 钉钉
2121
- [ ] 微博
2222
- [ ] 百度
23-
- [ ] Gitee
23+
- [x] Gitee
2424
- [ ] Github
2525
- [ ] 开源中国
2626
- [ ] 阿里云

example/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
43
import uvicorn
54

65
from fastapi import Depends, FastAPI

src/fastapi_oauth20/clients/gitee.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import httpx
4+
5+
from fastapi_oauth20.oauth20 import OAuth20Base
6+
7+
AUTHORIZE_ENDPOINT = 'https://gitee.com/oauth/authorize'
8+
ACCESS_TOKEN_ENDPOINT = 'https://gitee.com/oauth/token'
9+
REFRESH_TOKEN_ENDPOINT = ACCESS_TOKEN_ENDPOINT
10+
DEFAULT_SCOPES = ['user_info']
11+
PROFILE_ENDPOINT = 'https://gitee.com/api/v5/user'
12+
13+
14+
class GiteeOAuth20(OAuth20Base):
15+
def __init__(self, client_id: str, client_secret: str):
16+
super().__init__(
17+
client_id=client_id,
18+
client_secret=client_secret,
19+
authorize_endpoint=AUTHORIZE_ENDPOINT,
20+
access_token_endpoint=ACCESS_TOKEN_ENDPOINT,
21+
refresh_token_endpoint=REFRESH_TOKEN_ENDPOINT,
22+
revoke_token_endpoint=None,
23+
oauth_callback_route_name='gitee',
24+
default_scopes=DEFAULT_SCOPES,
25+
)
26+
27+
async def get_userinfo(self, access_token: str) -> dict:
28+
"""Get user info from Gitee"""
29+
headers = {'Authorization': f'Bearer {access_token}'}
30+
async with httpx.AsyncClient() as client:
31+
response = await client.get(PROFILE_ENDPOINT, headers=headers)
32+
await self.raise_httpx_oauth20_errors(response)
33+
34+
res = response.json()
35+
36+
return res

src/fastapi_oauth20/clients/google.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ async def get_userinfo(self, access_token: str) -> dict:
3232
response = await client.get(PROFILE_ENDPOINT, headers=headers)
3333
await self.raise_httpx_oauth20_errors(response)
3434

35-
res = response.json()
35+
res = response.json()
3636

37-
return res
37+
return res

src/fastapi_oauth20/oauth20.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
import abc
4+
35
from urllib.parse import urlencode, urljoin
46

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

127129
@staticmethod
128130
async def raise_httpx_oauth20_errors(response: httpx.Response) -> None:
131+
"""Raise HTTPXOAuth20Error if the response is invalid"""
129132
try:
130133
response.raise_for_status()
131134
except httpx.HTTPStatusError as e:
132135
raise HTTPXOAuth20Error(e) from e
136+
137+
@abc.abstractmethod
138+
async def get_userinfo(self, access_token: str) -> dict:
139+
"""Get user info"""
140+
...

0 commit comments

Comments
 (0)