|
| 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://passport.feishu.cn/suite/passport/oauth/authorize' |
| 8 | +ACCESS_TOKEN_ENDPOINT = 'https://passport.feishu.cn/suite/passport/oauth/token' |
| 9 | +REFRESH_TOKEN_ENDPOINT = AUTHORIZE_ENDPOINT |
| 10 | +DEFAULT_SCOPES = ['contact:user.employee_id:readonly', 'contact:user.base:readonly', 'contact:user.email:readonly'] |
| 11 | +PROFILE_ENDPOINT = 'https://passport.feishu.cn/suite/passport/oauth/userinfo' |
| 12 | + |
| 13 | + |
| 14 | +class FeiShuOAuth20(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='feishu', |
| 24 | + default_scopes=DEFAULT_SCOPES, |
| 25 | + ) |
| 26 | + |
| 27 | + async def get_userinfo(self, access_token: str) -> dict: |
| 28 | + """Get user info from FeiShu""" |
| 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 |
0 commit comments