Download the PHP package inmanturbo/homework without Composer
On this page you can find all versions of the php package inmanturbo/homework. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download inmanturbo/homework
More information about inmanturbo/homework
Files in inmanturbo/homework
Package homework
Short Description Laravel Passport extension for WorkOS UserManagement authentication
License MIT
Informations about the package homework
inmanturbo/homework
A Laravel Passport extension that provides WorkOS UserManagement API compatibility, allowing you to use Laravel Passport as a drop-in replacement for WorkOS authentication services.
Quick Start
Table of Contents
- Quick Start
- Overview
- Features
- Related Packages
- Requirements
- Installation
- Usage
- Setting Up OAuth Clients
- Client Application Setup
- Authentication Flow
- Authorization Flow
- Customizing User Responses
- Customizing Authentication Responses
- Token Claims, Permissions & Metadata
- Organization Selection Flow
- Response Format
- Testing
- Contributing
- License
Overview
This package extends Laravel Passport to provide WorkOS-compatible OAuth endpoints, enabling you to self-host your authentication service while maintaining full compatibility with the Laravel WorkOS SDK client applications.
Key Benefits:
- Self-host your authentication instead of relying on WorkOS.com
- Full control over user data and authentication flow
- No vendor lock-in while maintaining SDK compatibility
- Support for advanced features like multi-organization selection
Features
- WorkOS UserManagement API Compatibility: Implements WorkOS UserManagement authentication endpoints
- OAuth 2.0 Authorization Code Flow: Full implementation using Laravel Passport
- Optional Auto-Approval: Choose between standard OAuth authorization flow or automatic approval for first-party clients
- RSA Key Support (RS256): JWKS endpoint for JWT token verification
- Organization Support: Multi-organization selection flow without database migrations
- Customizable Responses: Flexible user and authentication response transformations
- Headless Views: Customize authorization and organization selection screens
- Easy Client Creation: Artisan command and service for quick OAuth client setup
- Auto-Install Script: One-liner bash script to automatically configure client applications
- Session Management: Preserves intended URLs through OAuth flow
- Drop-in Replacement: Works seamlessly with existing Laravel WorkOS integrations
Related Packages
Homework Organizations - Database-per-organization multi-tenancy for Laravel applications. Provides automatic organization database switching, storage isolation, and queue context propagation. Perfect companion for building multi-organization SaaS applications with Homework OAuth authentication.
Requirements
- PHP 8.2 or higher
- Laravel 11.x or 12.x
- Laravel Passport 13.x
- Laravel WorkOS ^0.5.0 (for organization support on client apps)
Installation
Step 1: Install the Package
Via Composer:
Or for local development, add to your composer.json:
Step 2: Install Laravel Passport
If you haven't already installed Passport:
That's it! The package automatically registers all necessary routes and views.
Usage
Setting Up OAuth Clients
Option 1: Using Artisan Command (Easiest)
Create a first-party WorkOS client using the provided Artisan command:
This will output ready-to-copy environment variables and an auto-install script:
Auto-Install Script:
The one-liner bash command will automatically:
- Add/update environment variables in your
.envfile - Add
base_urlto yourconfig/services.phpworkos configuration - Create
app/Providers/WorkOsServiceProvider.phpwith proper WorkOS SDK initialization - Register the service provider in your application
Simply cd into your client Laravel project and paste the command!
Option 2: Using the ClientService
Programmatically create a first-party WorkOS-compatible client:
This will return all the configuration details you need:
Multiple Redirect URIs:
Option 3: Using Passport's Artisan Commands
You can also use Laravel Passport's built-in Artisan commands:
Public Client (No Client Secret)
Public clients are suitable for SPAs and mobile apps that cannot securely store secrets:
When prompted, enter your redirect URI (e.g., http://your-app.test/authenticate).
Confidential Client (With Client Secret)
Confidential clients are suitable for server-side applications that can securely store secrets:
When prompted:
- Enter your redirect URI (e.g.,
http://your-app.test/authenticate) - Choose whether to enable device authorization flow (usually
no)
The command will output:
- Client ID: Use this as
WORKOS_CLIENT_ID - Client Secret: Use this as
WORKOS_API_KEY
Note: First-party clients (those without a user_id) can automatically bypass the authorization screen when using the optional auto-approval features (see Authorization Flow section below).
Client Application Configuration
Configure your client application's .env to point to your OAuth server:
Then configure the WorkOS SDK to use your OAuth server in app/Providers/AppServiceProvider.php:
Authentication Flow
The package provides these WorkOS-compatible endpoints:
GET /user_management/authorize- OAuth authorization endpoint (redirects to Passport's /oauth/authorize)POST /user_management/authenticate- Token exchange endpointPOST /user_management/authenticate_with_refresh_token- Refresh token endpointGET /user_management/users/{userId}- Get user informationGET /sso/jwks/{clientId}- JWKS endpoint for token verification
Authorization Flow
By default, the package uses the standard OAuth authorization flow where users see an authorization screen asking them to approve access. You have three options for customizing this behavior:
Option 1: Standard OAuth Flow (Default)
No configuration needed. Users will see the authorization screen on first login.
Option 2: Auto-Approval Using Client Model (Recommended)
Use the provided Client model or trait to automatically approve first-party clients:
Using the Provided Client Model:
Using Your Own Client Model:
If you already have a custom Client model, add the trait:
Then configure Passport to use your model:
Option 3: Auto-Approval Using Middleware
Alternatively, use the provided middleware (kept for backward compatibility):
How Auto-Approval Works:
When using either the Client model approach (Option 2) or middleware approach (Option 3):
- Users are not shown an authorization screen for your own applications (first-party clients)
- The OAuth flow completes transparently
- Third-party clients (those with a
user_id) still see the authorization prompt - First-party clients are identified by having an empty
user_idfield
Which approach should you use?
- Option 1 (Standard Flow): Best for maximum security and transparency, or if you want users to explicitly approve access
- Option 2 (Client Model/Trait): Cleanest approach, uses Passport's native functionality, recommended for most use cases
- Option 3 (Middleware): Provides more control over the approval logic, useful if you need custom approval rules
Custom Authorization View (Optional)
The package includes a modern, dark-mode-enabled authorization view that you can optionally use. This view is displayed when users need to approve access (when not using auto-approval, or for third-party clients).
Using the Provided Authorization View
Configure Passport to use the package's authorization view:
View Features:
- Modern, clean design
- Dark mode support (respects system preference and localStorage)
- Responsive layout
- Clear permission display with icons
- Professional authorization/cancel buttons
- Shows signed-in user email
Customizing the View
To use your own authorization view, simply don't configure Passport to use the homework view, and create your own view according to Passport's documentation.
Alternatively, you can override the package's view by creating a file at resources/views/vendor/homework/auth/authorize.blade.php in your application - Laravel will automatically use your version instead of the package's version.
Customizing User Responses
The package provides a flexible way to customize how user data is returned in WorkOS-compatible responses. This is useful for adding support for organizations, custom avatars, or any other user-related data.
Using the workosUser() Method (Recommended)
The simplest way to customize the user response is by adding a workosUser() method to your User model:
The default UserResponse will:
- Check if your User model has a
workosUser()method - If found, use it to create the WorkOS User object
- If not found, create one automatically from common attributes
- Automatically include
organization_idin the response if present - Handle avatar URL detection from
avatar_url,profile_picture_url, oravatarattributes
Creating a Custom User Response
-
Create a class that implements
UserResponseContract: - Bind your custom class in a service provider:
Option 1: Direct Binding
Option 2: Using Homework Facade (Recommended)
The custom user response will now be used for:
/user_management/authenticateendpoint/user_management/authenticate_with_refresh_tokenendpoint/user_management/users/{userId}endpoint
Extending the Default Response
You can also extend the default UserResponse class:
Customizing Authentication Responses
For advanced use cases, you can customize the entire authentication response structure (including top-level fields like organization_id, impersonator, etc.):
Creating a Custom Authentication Response
-
Create a class that implements
AuthenticationResponseContract: - Bind your custom class in a service provider:
Option 1: Direct Binding
Option 2: Using Homework Facade (Recommended)
Note: The organization_id must be at the top level of the response for the WorkOS SDK to properly extract it. The default AuthenticationResponse handles this automatically.
Token Claims, Permissions & Metadata
The package supports adding custom claims, permissions, roles, and metadata to authentication tokens, following WorkOS patterns. This allows you to enrich tokens with authorization data without implementing the actual authorization logic in the package.
WorkOS org_id Pattern
Following WorkOS conventions:
- If a user belongs to exactly one organization,
org_idis included in the response - If a user belongs to zero or multiple organizations,
org_idis omitted (keeping the server stateless) - For multi-org users, organization selection happens on the client side
Creating a Token Claims Provider
-
Create a class that implements
TokenClaimsProviderContract: - Bind your provider in a service provider:
Response Structure
When a TokenClaimsProvider is bound, the authentication response will include:
Single Organization User:
Multi-Organization User (stateless):
Note: No org_id is included when the user belongs to multiple organizations, keeping the server stateless.
Implementation Notes
- Optional: All methods can return
nullto omit that claim type - Flexible: You decide how to fetch permissions/roles (database, cache, external API, etc.)
- Stateless: The package doesn't manage permissions - it just includes them if provided
- Custom Metadata: Any additional fields are merged directly into the user object
- Performance: Consider caching expensive queries in your provider implementation
Organization Selection Flow
The package supports multi-organization users with an optional organization selection step after login. This is useful when users belong to multiple organizations and need to choose which one to access.
Enabling Organization Selection
-
Create a class that implements
OrganizationProviderContract: -
Bind the provider using
Homework::useOrganizationProvider(): - Add the middleware to your web middleware group:
How it works:
- If a user belongs to multiple organizations, they'll see a selection screen after login
- The selected
organization_idis stored in the session and cache - The
organization_idis automatically included in the authentication response - If a user belongs to 0 or 1 organization, the selection step is skipped
Customizing the Organization Selection View
Similar to Passport's authorization view, you can customize the organization selection screen:
Option 1: Using a Custom View Callback (Headless)
Option 2: Override the Package View
Create a file at resources/views/vendor/homework/auth/select-organization.blade.php - Laravel will automatically use your version instead of the package's version.
Default View Features:
- Modern, clean design matching the authorization view
- Dark mode support (respects system preference and localStorage)
- Responsive layout
- Organization cards with icons
- Shows signed-in user email
Preserving Intended URLs (Client Application)
In your client application, use Laravel's intended() redirect to preserve the URL users were trying to access:
Response Format
The package returns WorkOS-compatible user responses:
Architecture
Directory Structure
Key Components
- Homework: Central configuration class providing static helper methods for binding contracts:
useOrganizationProvider(),useUserResponse(),useAuthenticationResponse(),useTokenClaimsProvider(), and configuring headless views withorganizationSelectionView()(similar to Passport) - HomeworkServiceProvider: Registers routes, loads views, and binds contracts
- ClientService: Static service for easily creating WorkOS-compatible OAuth clients
- CreateWorkOsClientCommand: Artisan command for creating OAuth clients with auto-install script output
- AuthenticateRequest: Handles OAuth authentication for both authorization code and refresh token flows
- GetUserRequest: Handles user retrieval by ID with proper authentication
- InstallScriptRequest: Generates bash installation script for automatic client configuration
- JwksRequest: Provides JWKS endpoint for JWT token verification using RSA keys (RS256)
- UserResponseContract: Interface for customizing user response transformation
- UserResponse: Default implementation with WorkOS User integration and avatar support
- AuthenticationResponseContract: Interface for customizing complete authentication response
- AuthenticationResponse: Default implementation handling top-level organization_id
- TokenClaimsProviderContract: Interface for providing custom token claims (organizations, permissions, roles, metadata)
- OrganizationProviderContract: Interface for providing user organizations
- RequireOrganizationSelection: Middleware for multi-organization selection flow
- Client Model: Custom Passport Client model with first-party auto-approval (optional)
- SkipsAuthorizationForFirstPartyClients Trait: Reusable trait for adding auto-approval to any Client model (optional)
- AutoApproveFirstPartyClients Middleware: Alternative middleware approach for auto-approval (optional)
- Authorization View: Modern, dark-mode-enabled authorization screen (optional)
- Organization Selection View: Dark-mode organization selection screen with headless support (optional)
Testing
You can test the OAuth flow using curl:
Migration from WorkOS
To migrate from WorkOS to this self-hosted solution:
- Install this package on your OAuth server
- Create OAuth clients matching your WorkOS application IDs
- Update your client application's
WORKOS_BASE_URLto point to your OAuth server - Your existing Laravel WorkOS integration will now use your local OAuth server
Benefits
- Complete Control: All authentication happens in your application
- No External Dependencies: No need for WorkOS account or API calls
- Development Friendly: Easy to test and develop locally
- Cost Effective: No WorkOS subscription required
- Data Privacy: User data stays in your own database
- Customizable: Full control over authentication flow and UI
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues and questions, please use the GitHub issue tracker.
Credits
This package was created to provide a self-hosted alternative to WorkOS authentication while maintaining full compatibility with the Laravel WorkOS SDK.
All versions of homework with dependencies
laravel/framework Version ^11.0|^12.0
laravel/passport Version ^13.0
laravel/workos Version ^0.5.0