-
Notifications
You must be signed in to change notification settings - Fork 17
Implement Authkit Sessions #315
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
base: main
Are you sure you want to change the base?
Conversation
Greptile Summary
Important Files Changed
Confidence score: 4/5
Sequence DiagramsequenceDiagram
participant User
participant App
participant CookieSession
participant UserManagement
participant HaliteSessionEncryption
participant WorkOSAPI
User->>App: "Request with session cookie"
App->>UserManagement: "getSessionFromCookie(cookiePassword)"
UserManagement->>UserManagement: "Extract cookie from $_COOKIE"
UserManagement->>UserManagement: "loadSealedSession(sealedSession, cookiePassword)"
UserManagement->>CookieSession: "new CookieSession(userManagement, sealedSession, cookiePassword)"
App->>CookieSession: "authenticate()"
CookieSession->>UserManagement: "authenticateWithSessionCookie(sealedSession, cookiePassword)"
UserManagement->>HaliteSessionEncryption: "unseal(sealedSession, cookiePassword)"
HaliteSessionEncryption-->>UserManagement: "sessionData"
UserManagement->>WorkOSAPI: "POST user_management/sessions/authenticate"
WorkOSAPI-->>UserManagement: "authentication response"
UserManagement-->>CookieSession: "SessionAuthenticationSuccessResponse"
CookieSession-->>App: "authentication result"
App-->>User: "authenticated response"
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
11 files reviewed, 1 comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
11 files reviewed, no comments
c80a0ed to
19d649b
Compare
|
This doesn't have to wait, but it would likely be beneficial to wait until #316 merges so I can update the new |
dandorman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good to me, outside of a couple nits. (And i am woefully out of date on my PHP knowledge, so take anything i say with a grain of salt.)
The code looks good, Halite seems like a reasonable choice for an encryption library. I think that is my sole caveat with this PR. It does indeed bring the PHP SDK closer to how the others operate, but that includes the sealed session stuff i think we're hoping to deprecate. Most frameworks provide their own way to encrypt and/or sign cookies, there's little reason for us to add our own layer. Additionally, this creates another island of un-interoperability, where sessions sealed in one platform can't easily be unsealed in another. (cc @cmatheson , who has more thoughts/knowledge on the matter)
Would it be possible to make the sealed-session stuff optional? I don't want to overburden you, but @nicknisi has done some excellent work at paving the way to make session backing more plugin in the JS world.
But if we're not interested in pursuing any of that now, let me know. I don't want to hold this up if we'd rather get parity.
@dandorman everything is possible and "pairity" is only one concern. I'm happy to wait and discuss, I just figured I'd use whatever skills I could to push the SDKs to unity (and, selfishly, I want to be able to use this specific feature in my own projects) |
Add paragonie/halite ^4.0 for PHP 7.3+ compatible symmetric encryption. This library provides libsodium-based encryption for session data.
Add Session resource class for representing user sessions with properties: id, userId, ipAddress, userAgent, organizationId, authenticationMethod, status, expiresAt, endedAt, createdAt, updatedAt.
Add SessionAuthenticationFailureResponse with failure reason constants: - NO_SESSION_COOKIE_PROVIDED - INVALID_SESSION_COOKIE - ENCRYPTION_ERROR - HTTP_ERROR Add SessionAuthenticationSuccessResponse with full session data including user, tokens, organization, roles, permissions, entitlements, and impersonator.
Add SessionEncryptionInterface defining seal/unseal contract for session encryption providers. Add HaliteSessionEncryption implementing libsodium-based symmetric encryption with HKDF key derivation and TTL validation. Default TTL is 30 days to match WorkOS session lifetime. Include comprehensive tests for encryption, TTL expiration, wrong password handling, and data integrity.
Add SigningOnlySessionHandler as an alternative to full encryption. Uses HMAC-SHA256 for integrity verification without encrypting the payload. Suitable for TLS-only environments where encryption overhead is undesirable. Includes version field for future compatibility, TTL validation, and constant-time signature comparison to prevent timing attacks.
Add session management methods: - listSessions: List sessions for a user with pagination - revokeSession: Revoke a specific session - authenticateWithSessionCookie: Validate sealed session cookies - loadSealedSession: Create CookieSession from sealed data - getSessionFromCookie: Extract session from HTTP cookies Support injectable SessionEncryptionInterface for custom encryption providers, defaulting to HaliteSessionEncryption.
Add CookieSession for high-level session management: - authenticate: Validate session and return user data - refresh: Refresh expired tokens and return raw tokens for re-sealing - getLogoutUrl: Generate logout URL for the session Designed to match workos-node CookieSession behavior. Sealing tokens is the responsibility of the calling code (e.g., authkit-php).
4971150 to
36e0279
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
13 files reviewed, no comments
Change catch blocks from \Exception to Exception\BaseRequestException to only catch HTTP-related errors from Client::request(). This prevents accidentally swallowing unrelated errors like TypeErrors or configuration issues that should bubble up for debugging. Addresses review feedback on PR #315.
dandorman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, i guess it's not worth keeping our PHP friends out in the cold.
Description
This PR implements AuthKit Sessions support for the PHP SDK, bringing it to parity with the Node, Python, and Ruby sdks.
Documentation
Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.
If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.