|
| 1 | +"""Notion SSO Oauth Helper class""" |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, Optional |
| 4 | + |
| 5 | +from fastapi_sso.sso.base import DiscoveryDocument, OpenID, SSOBase, SSOLoginError |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + import httpx # pragma: no cover |
| 9 | + |
| 10 | + |
| 11 | +class NotionSSO(SSOBase): |
| 12 | + """Class providing login using Notion OAuth""" |
| 13 | + |
| 14 | + provider = "notion" |
| 15 | + scope = ["openid"] |
| 16 | + additional_headers = {"Notion-Version": "2022-06-28"} |
| 17 | + |
| 18 | + async def get_discovery_document(self) -> DiscoveryDocument: |
| 19 | + return { |
| 20 | + "authorization_endpoint": "https://api.notion.com/v1/oauth/authorize?owner=user", |
| 21 | + "token_endpoint": "https://api.notion.com/v1/oauth/token", |
| 22 | + "userinfo_endpoint": "https://api.notion.com/v1/users/me", |
| 23 | + } |
| 24 | + |
| 25 | + async def openid_from_response(self, response: dict, session: Optional["httpx.AsyncClient"] = None) -> OpenID: |
| 26 | + owner = response["bot"]["owner"] |
| 27 | + if owner["type"] != "user": |
| 28 | + raise SSOLoginError(401, f"Notion login failed, owner is not a user but {response['bot']['owner']['type']}") |
| 29 | + return OpenID( |
| 30 | + id=owner["user"]["id"], |
| 31 | + email=owner["user"]["person"]["email"], |
| 32 | + picture=owner["user"]["avatar_url"], |
| 33 | + display_name=owner["user"]["name"], |
| 34 | + provider=self.provider, |
| 35 | + ) |
0 commit comments