Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Seznam SSO provider #194

Merged
merged 6 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ I tend to process Pull Requests faster when properly caffeinated 😉.
- Line (by Jimmy Yeh) - [jimmyyyeh](https://github.com/jimmyyyeh)
- LinkedIn (by Alessandro Pischedda) - [Cereal84](https://github.com/Cereal84)
- Yandex (by Akim Faskhutdinov) – [akimrx](https://github.com/akimrx)
- Seznam (by Tomas Koutek) - [TomasKoutek](https://github.com/TomasKoutek)

See [Contributing](#contributing) for a guide on how to contribute your own login provider.

Expand Down
39 changes: 39 additions & 0 deletions examples/seznam.py
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})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is passing the client secret explicitly in params neccessary? I would guess oauthlib adds the client secret to the request uri when doing prepare_token_request in verify_login method of SSOBase 🤔 If this is specific to Seznam, I'd probably also add it to documentation, or maybe emphasize it in the comment here so that users who come to documented examples actually notice that this is some specialty :)

Copy link
Contributor Author

@TomasKoutek TomasKoutek Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, unfortunately it is needed. It took me a while to figure it out, but it's also described in the documentation - "2. Převod kódu na token" contains a "client_secret" field. Without it, Seznam API returns the response "{'error': 'invalid_client'}".

I added a comment to the example - 3be5d2e

return user
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify the login implementation and consider filtering sensitive information.

The auth_callback function is implemented correctly and follows the expected flow for verifying the login credentials. It uses the verify_and_process method of the SeznamSSO instance to process the request and is decorated with the appropriate endpoint.

However, passing the client_secret as a parameter to the verify_and_process method and directly returning the user object could potentially expose sensitive information. Consider filtering out any sensitive fields from the user object before returning it to the client.



if __name__ == "__main__":
uvicorn.run(app="examples.seznam:app", host="127.0.0.1", port=5000)
43 changes: 43 additions & 0 deletions fastapi_sso/sso/seznam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Seznam SSO Login Helper."""

from typing import ClassVar
from typing import Optional
from typing import TYPE_CHECKING

from fastapi_sso.sso.base import DiscoveryDocument
from fastapi_sso.sso.base import OpenID
from fastapi_sso.sso.base import 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(

Check warning on line 35 in fastapi_sso/sso/seznam.py

View check run for this annotation

Codecov / codecov/patch

fastapi_sso/sso/seznam.py#L35

Added line #L35 was not covered by tests
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"),
)
2 changes: 2 additions & 0 deletions tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from fastapi_sso.sso.linkedin import LinkedInSSO
from fastapi_sso.sso.twitter import TwitterSSO
from fastapi_sso.sso.yandex import YandexSSO
from fastapi_sso.sso.seznam import SeznamSSO

GenericProvider = create_provider(
name="generic",
Expand Down Expand Up @@ -51,6 +52,7 @@
LinkedInSSO,
TwitterSSO,
YandexSSO,
SeznamSSO,
)

# Run all tests for each of the listed providers
Expand Down
Loading