Skip to content

Commit

Permalink
Add OSChina OAuth2
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan committed Feb 6, 2024
1 parent d9e2599 commit 4e56522
Showing 1 changed file with 35 additions and 0 deletions.
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 4e56522

Please sign in to comment.