Skip to content

Add OSChina OAuth2 #4

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 2 commits into from
Feb 6, 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
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