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

Handle facebook limited login #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

kadyvillicana
Copy link
Collaborator

No description provided.

src/auth.py Outdated
@@ -107,6 +114,16 @@ def rq_get(request: Request, key: str) -> str:
return j.get(key, "")
return ""

def get_facebook_public_key():
Copy link
Member

Choose a reason for hiding this comment

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

Missing type annotation on return type

src/auth.py Outdated
@@ -107,6 +114,16 @@ def rq_get(request: Request, key: str) -> str:
return j.get(key, "")
return ""

def get_facebook_public_key():
# Get the public key from Facebook's JWKS endpoint
response = requests.get(FACEBOOK_JWT_ENDPOINT)
Copy link
Member

Choose a reason for hiding this comment

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

We need some kind of error handling around this, i.e. a try-catch block, and a way to return an error status if the fetch was not successful.

src/auth.py Outdated
@@ -107,6 +114,16 @@ def rq_get(request: Request, key: str) -> str:
return j.get(key, "")
return ""

def get_facebook_public_key():
Copy link
Member

Choose a reason for hiding this comment

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

Add an @lrucache or @cache decorator here - we don't need to re-fetch this on every login

src/auth.py Outdated
msg = ""

# Verify if this is a limited login (iOS only)
is_limited_login = rq.get("isLimitedLogin", False)
Copy link
Member

Choose a reason for hiding this comment

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

Consider using rq.get_bool() here

src/auth.py Outdated
except jwt.InvalidTokenError:
return (
jsonify(
{"status": "invalid", "msg": "Invalid Facebook expired",}
Copy link
Member

Choose a reason for hiding this comment

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

"Invalid Facebook token"

src/auth.py Outdated
public_key_pem = jwt.algorithms.RSAAlgorithm.from_jwk(key_data)

return public_key_pem
@lru_cache(maxsize=1)
Copy link
Member

Choose a reason for hiding this comment

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

Use @cache here, @lru_cache is not needed. But how often does the Facebook public key change? If it changes e.g. every 24 hours we need a mechanism to refetch it if the last value fetched is > 12 hours old.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The documentation doesn’t specify a time frame for public key changes, but I believe we should refetch it every week.

src/auth.py Outdated

return public_key_pem # Return the key and success status
except requests.exceptions.RequestException as e:
print(f"An error occurred while fetching the public key: {e}")
Copy link
Member

Choose a reason for hiding this comment

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

Use logging.error() instead of print

src/auth.py Outdated
print(f"An error occurred while fetching the public key: {e}")
return None # Return None and failure status
except KeyError as e:
print(f"Key error: {e}")
Copy link
Member

Choose a reason for hiding this comment

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

Use logging.error() instead of print

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants