-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added Yandex SSO provider (#146)
* feat: added Yandex SSO provider * chore: fixed module docstring for yandex example * chore: removed useless imports, updated imports sort * docs: added contribute author for YandexSSO
- Loading branch information
Showing
5 changed files
with
105 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,38 @@ | ||
"""Yandex Login Example | ||
""" | ||
|
||
import os | ||
import uvicorn | ||
from fastapi import FastAPI, Request | ||
from fastapi_sso.sso.yandex import YandexSSO | ||
|
||
CLIENT_ID = os.environ["CLIENT_ID"] | ||
CLIENT_SECRET = os.environ["CLIENT_SECRET"] | ||
|
||
app = FastAPI() | ||
|
||
sso = YandexSSO( | ||
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) | ||
return user | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app="examples.yandex: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,44 @@ | ||
"""Yandex SSO Login Helper | ||
""" | ||
|
||
from typing import TYPE_CHECKING, Optional | ||
|
||
from fastapi_sso.sso.base import DiscoveryDocument, OpenID, SSOBase | ||
|
||
if TYPE_CHECKING: | ||
import httpx # pragma: no cover | ||
|
||
|
||
class YandexSSO(SSOBase): | ||
"""Class providing login using Yandex OAuth.""" | ||
|
||
provider = "yandex" | ||
scope = ["login:email", "login:info", "login:avatar"] | ||
avatar_url = "https://avatars.yandex.net/get-yapic" | ||
|
||
async def get_discovery_document(self) -> DiscoveryDocument: | ||
"""Override the discovery document method to return Yandex OAuth endpoints.""" | ||
|
||
return { | ||
"authorization_endpoint": "https://oauth.yandex.ru/authorize", | ||
"token_endpoint": "https://oauth.yandex.ru/token", | ||
"userinfo_endpoint": "https://login.yandex.ru/info", | ||
} | ||
|
||
async def openid_from_response(self, response: dict, session: Optional["httpx.AsyncClient"] = None) -> OpenID: | ||
"""Converts Yandex user info response to OpenID object.""" | ||
|
||
picture = None | ||
|
||
if (avatar_id := response.get("default_avatar_id")) is not None: | ||
picture = f"{self.avatar_url}/{avatar_id}/islands-200" | ||
|
||
return OpenID( | ||
email=response.get("default_email"), | ||
display_name=response.get("display_name"), | ||
provider=self.provider, | ||
id=response.get("id"), | ||
first_name=response.get("first_name"), | ||
last_name=response.get("last_name"), | ||
picture=picture, | ||
) |
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