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

fix: default e-mail "" was invalid and should be None #166

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions examples/facebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Facebook Login Example"""

import os

import uvicorn
from fastapi import FastAPI, Request

from fastapi_sso.sso.facebook import FacebookSSO

CLIENT_ID = os.environ["CLIENT_ID"]
CLIENT_SECRET = os.environ["CLIENT_SECRET"]

app = FastAPI()

sso = FacebookSSO(
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(params={"prompt": "consent", "access_type": "offline"})


@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.facebook:app", host="127.0.0.1", port=5000)
5 changes: 3 additions & 2 deletions fastapi_sso/sso/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FacebookSSO(SSOBase):
"""Class providing login via Facebook OAuth."""

provider = "facebook"
base_url = "https://graph.facebook.com/v9.0"
base_url = "https://graph.facebook.com/v19.0"
scope: ClassVar = ["email"]

async def get_discovery_document(self) -> DiscoveryDocument:
Expand All @@ -25,8 +25,9 @@ async def get_discovery_document(self) -> DiscoveryDocument:

async def openid_from_response(self, response: dict, session: Optional["httpx.AsyncClient"] = None) -> OpenID:
"""Return OpenID from user information provided by Facebook."""

return OpenID(
email=response.get("email", ""),
email=response.get("email"),
first_name=response.get("first_name"),
last_name=response.get("last_name"),
display_name=response.get("name"),
Expand Down
2 changes: 1 addition & 1 deletion fastapi_sso/sso/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def openid_from_response(self, response: dict, session: Optional["httpx.As
"""Return OpenID from user information provided by Google."""
if response.get("email_verified"):
return OpenID(
email=response.get("email", ""),
email=response.get("email"),
provider=self.provider,
id=response.get("sub"),
first_name=response.get("given_name"),
Expand Down
2 changes: 1 addition & 1 deletion fastapi_sso/sso/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def openid_from_response(self, response: dict, session: Optional["httpx.As
"""Return OpenID from user information provided by Spotify."""
picture = response["images"][0]["url"] if response.get("images", []) else None
return OpenID(
email=response.get("email", ""),
email=response.get("email"),
display_name=response.get("display_name"),
provider=self.provider,
id=response.get("id"),
Expand Down
Loading