-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: apply additional headers to user endpoint * feat: add support for Notion
- Loading branch information
1 parent
16fbcc0
commit e2f8eb1
Showing
8 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Github Login Example | ||
""" | ||
|
||
import os | ||
import uvicorn | ||
from fastapi import FastAPI, Request | ||
from fastapi_sso.sso.notion import NotionSSO | ||
|
||
CLIENT_ID = os.environ["CLIENT_ID"] | ||
CLIENT_SECRET = os.environ["CLIENT_SECRET"] | ||
|
||
app = FastAPI() | ||
|
||
sso = NotionSSO( | ||
client_id=CLIENT_ID, | ||
client_secret=CLIENT_SECRET, | ||
redirect_uri="http://localhost:3000/oauth2/callback", | ||
allow_insecure_http=True, | ||
) | ||
|
||
|
||
@app.get("/oauth2/login") | ||
async def auth_init(): | ||
"""Initialize auth and redirect""" | ||
with sso: | ||
return await sso.get_login_redirect() | ||
|
||
|
||
@app.get("/oauth2/callback") | ||
async def auth_callback(request: Request): | ||
"""Verify login""" | ||
with sso: | ||
user = await sso.verify_and_process(request) | ||
return user | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app="examples.notion:app", host="127.0.0.1", port=3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Notion SSO Oauth Helper class""" | ||
|
||
from typing import TYPE_CHECKING, Optional | ||
|
||
from fastapi_sso.sso.base import DiscoveryDocument, OpenID, SSOBase, SSOLoginError | ||
|
||
if TYPE_CHECKING: | ||
import httpx # pragma: no cover | ||
|
||
|
||
class NotionSSO(SSOBase): | ||
"""Class providing login using Notion OAuth""" | ||
|
||
provider = "notion" | ||
scope = ["openid"] | ||
additional_headers = {"Notion-Version": "2022-06-28"} | ||
|
||
async def get_discovery_document(self) -> DiscoveryDocument: | ||
return { | ||
"authorization_endpoint": "https://api.notion.com/v1/oauth/authorize?owner=user", | ||
"token_endpoint": "https://api.notion.com/v1/oauth/token", | ||
"userinfo_endpoint": "https://api.notion.com/v1/users/me", | ||
} | ||
|
||
async def openid_from_response(self, response: dict, session: Optional["httpx.AsyncClient"] = None) -> OpenID: | ||
owner = response["bot"]["owner"] | ||
if owner["type"] != "user": | ||
raise SSOLoginError(401, f"Notion login failed, owner is not a user but {response['bot']['owner']['type']}") | ||
return OpenID( | ||
id=owner["user"]["id"], | ||
email=owner["user"]["person"]["email"], | ||
picture=owner["user"]["avatar_url"], | ||
display_name=owner["user"]["name"], | ||
provider=self.provider, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pytest | ||
from fastapi_sso import NotionSSO, OpenID, SSOLoginError | ||
|
||
|
||
async def test_notion_openid_response(): | ||
sso = NotionSSO("client_id", "client_secret") | ||
valid_response = { | ||
"bot": { | ||
"owner": { | ||
"type": "user", | ||
"user": { | ||
"id": "test", | ||
"person": {"email": "[email protected]"}, | ||
"avatar_url": "avatar", | ||
"name": "Test User", | ||
}, | ||
} | ||
} | ||
} | ||
invalid_response = {"bot": {"owner": {"type": "workspace", "workspace": {}}}} | ||
with pytest.raises(SSOLoginError): | ||
await sso.openid_from_response(invalid_response) | ||
openid = OpenID(id="test", email="[email protected]", display_name="Test User", picture="avatar", provider="notion") | ||
assert await sso.openid_from_response(valid_response) == openid |