diff --git a/examples/facebook.py b/examples/facebook.py new file mode 100644 index 0000000..7294fc9 --- /dev/null +++ b/examples/facebook.py @@ -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) diff --git a/fastapi_sso/sso/facebook.py b/fastapi_sso/sso/facebook.py index 930e3f2..dad9042 100644 --- a/fastapi_sso/sso/facebook.py +++ b/fastapi_sso/sso/facebook.py @@ -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: @@ -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"), diff --git a/fastapi_sso/sso/google.py b/fastapi_sso/sso/google.py index 0df30b3..f5dc67c 100644 --- a/fastapi_sso/sso/google.py +++ b/fastapi_sso/sso/google.py @@ -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"), diff --git a/fastapi_sso/sso/spotify.py b/fastapi_sso/sso/spotify.py index d1e9423..3782b42 100644 --- a/fastapi_sso/sso/spotify.py +++ b/fastapi_sso/sso/spotify.py @@ -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"),