forked from tomasvotava/fastapi-sso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Seznam SSO provider (tomasvotava#194)
* add seznam example * add info to README.md * add SeznamSSO to test_providers * add SeznamSSO * add client_secret comment * Organize imports (ruff)
- Loading branch information
1 parent
73e7467
commit 989ce0c
Showing
4 changed files
with
81 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""Seznam Login Example""" | ||
|
||
import os | ||
import uvicorn | ||
from fastapi import FastAPI | ||
from fastapi import Request | ||
|
||
from fastapi_sso.sso.seznam import SeznamSSO | ||
|
||
CLIENT_ID = os.environ["CLIENT_ID"] | ||
CLIENT_SECRET = os.environ["CLIENT_SECRET"] | ||
|
||
app = FastAPI() | ||
|
||
sso = SeznamSSO( | ||
client_id=CLIENT_ID, | ||
client_secret=CLIENT_SECRET, | ||
redirect_uri="http://localhost:5000/auth/callback", | ||
allow_insecure_http=True, | ||
) | ||
|
||
|
||
@app.get("/auth/login") | ||
async def auth_init(): | ||
"""Initialize auth and redirect""" | ||
with sso: | ||
return await sso.get_login_redirect() | ||
|
||
|
||
@app.get("/auth/callback") | ||
async def auth_callback(request: Request): | ||
"""Verify login""" | ||
with sso: | ||
user = await sso.verify_and_process(request, params={"client_secret": CLIENT_SECRET}) # <- "client_secret" parameter is needed! | ||
return user | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app="examples.seznam:app", host="127.0.0.1", port=5000) |
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,39 @@ | ||
"""Seznam SSO Login Helper.""" | ||
|
||
from typing import TYPE_CHECKING, ClassVar, Optional | ||
|
||
from fastapi_sso.sso.base import DiscoveryDocument, OpenID, SSOBase | ||
|
||
if TYPE_CHECKING: | ||
import httpx # pragma: no cover | ||
|
||
|
||
# https://vyvojari.seznam.cz/oauth/doc | ||
|
||
|
||
class SeznamSSO(SSOBase): | ||
"""Class providing login via Seznam OAuth.""" | ||
|
||
provider = "seznam" | ||
base_url = "https://login.szn.cz/api/v1" | ||
scope: ClassVar = ["identity", "avatar"] # + ["contact-phone", "adulthood", "birthday", "gender"] | ||
|
||
async def get_discovery_document(self) -> DiscoveryDocument: | ||
"""Get document containing handy urls.""" | ||
return { | ||
"authorization_endpoint": f"{self.base_url}/oauth/auth", | ||
"token_endpoint": f"{self.base_url}/oauth/token", | ||
"userinfo_endpoint": f"{self.base_url}/user", | ||
} | ||
|
||
async def openid_from_response(self, response: dict, session: Optional["httpx.AsyncClient"] = None) -> OpenID: | ||
"""Return OpenID from user information provided by Seznam.""" | ||
return OpenID( | ||
email=response.get("email"), | ||
first_name=response.get("firstname"), | ||
last_name=response.get("lastname"), | ||
display_name=response.get("accountDisplayName"), | ||
provider=self.provider, | ||
id=response.get("oauth_user_id"), | ||
picture=response.get("avatar_url"), | ||
) |
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