Skip to content

Commit

Permalink
Add OSChina OAuth2 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan authored Feb 6, 2024
1 parent d9e2599 commit d21ab6e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
- [ ] 百度
- [x] Gitee
- [x] Github
- [ ] 开源中国
- [X] 开源中国
- [ ] 阿里云
- [ ] TestHome
35 changes: 35 additions & 0 deletions src/fastapi_oauth20/clients/oschina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import httpx

from fastapi_oauth20.oauth20 import OAuth20Base

AUTHORIZE_ENDPOINT = 'https://www.oschina.net/action/oauth2/authorize'
ACCESS_TOKEN_ENDPOINT = 'https://www.oschina.net/action/openapi/token'
REFRESH_TOKEN_ENDPOINT = ACCESS_TOKEN_ENDPOINT
PROFILE_ENDPOINT = 'https://www.oschina.net/action/openapi/user'


class OSChinaOAuth20(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='oschina',
default_scopes=None,
)

async def get_userinfo(self, access_token: str) -> dict:
"""Get user info from OSChina"""
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

0 comments on commit d21ab6e

Please sign in to comment.