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

Adds session helpers #384

Merged
merged 18 commits into from
Dec 2, 2024
Merged

Adds session helpers #384

merged 18 commits into from
Dec 2, 2024

Conversation

PaulAsjes
Copy link
Contributor

@PaulAsjes PaulAsjes commented Nov 22, 2024

Fixes #350

Description

Adds new session helpers. Example API:

# Load session
session = workos.user_management.load_sealed_session(session_data=request.cookies.get('wos_session'), cookie_password=cookie_password)

# Authenticate user
response = session.authenticate()

current_user = response.user if response.authenticated else None

# Refresh session
result = session.refresh()
if result.authenticated == False:
    print("Refresh failed)

# Get log out URL
url = session.get_logout_url()

Also makes it so dependencies are loaded from requirements.txt rather than hardcoded in setup.py.

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.

@PaulAsjes PaulAsjes requested a review from a team as a code owner November 22, 2024 14:20
@PaulAsjes PaulAsjes requested a review from tribble November 22, 2024 14:20
@PaulAsjes PaulAsjes assigned PaulAsjes and unassigned PaulAsjes Nov 22, 2024
@PaulAsjes PaulAsjes requested a review from mattgd November 22, 2024 14:20
Copy link
Contributor

@mattgd mattgd left a comment

Choose a reason for hiding this comment

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

Thanks for adding! Left a bunch of comments inline, but most are minor.

Copy link
Contributor

@mattgd mattgd left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! There's still two outstanding comments regarding List vs. Sequence in /types/user_management/session.py, but nothing new from second pass.

Comment on lines 175 to 196
def is_valid_jwt(self, token: str) -> bool:
try:
signing_key = self.jwks.get_signing_key_from_jwt(token)
jwt.decode(token, signing_key.key, algorithms=self.jwk_algorithms)
return True
except jwt.exceptions.InvalidTokenError:
return False

@staticmethod
def seal_data(data: Dict[str, Any], key: str) -> str:
fernet = Fernet(key)
# Encrypt and convert bytes to string
encrypted_bytes = fernet.encrypt(json.dumps(data).encode())
return encrypted_bytes.decode("utf-8")

@staticmethod
def unseal_data(sealed_data: str, key: str) -> Dict[str, Any]:
fernet = Fernet(key)
# Convert string back to bytes before decryption
encrypted_bytes = sealed_data.encode("utf-8")
decrypted_str = fernet.decrypt(encrypted_bytes).decode()
return cast(Dict[str, Any], json.loads(decrypted_str))
Copy link
Contributor

Choose a reason for hiding this comment

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

If these are all private, we may want to mark them with a _ prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They're not really private since they can still be accessed statically. Should we still add the prefix?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good question, up to you. "Private" is really just a suggestion anyway. It has no real encapsulation benefits in Python. is_valid_jwt could at least use it though. More-so just helps us so we can consider these methods internal if we need to update.

@PaulAsjes PaulAsjes merged commit 4faeb61 into main Dec 2, 2024
5 checks passed
@PaulAsjes PaulAsjes deleted the feature/session-helpers branch December 2, 2024 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Status of AuthKit
2 participants