Skip to content

Commit

Permalink
fix: default e-mail "" was invalid and should be None (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasvotava authored May 18, 2024
1 parent 8ae7c5d commit df1f8ea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
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

0 comments on commit df1f8ea

Please sign in to comment.