diff --git a/README.md b/README.md index 0fae69c..e9ec889 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,6 @@ - [ ] 百度 - [x] Gitee - [x] Github -- [ ] 开源中国 +- [X] 开源中国 - [ ] 阿里云 - [ ] TestHome diff --git a/src/fastapi_oauth20/clients/oschina.py b/src/fastapi_oauth20/clients/oschina.py new file mode 100644 index 0000000..9d0edd7 --- /dev/null +++ b/src/fastapi_oauth20/clients/oschina.py @@ -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