1. Go to this page and download the library: Download inmanturbo/homework library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
inmanturbo / homework example snippets
use Inmanturbo\Homework\Services\ClientService;
$config = ClientService::createFirstPartyWorkOsClient(
redirectUris: 'http://your-app.test/authenticate',
name: 'My Application'
);
// Output the configuration
print_r($config['env_config']);
use WorkOS\WorkOS;
public function boot()
{
WorkOS::setApiKey(config('services.workos.secret'));
$baseUrl = config('services.workos.base_url');
if ($baseUrl && $baseUrl !== 'https://api.workos.com/') {
WorkOS::setApiBaseUrl($baseUrl);
}
}
use Inmanturbo\Homework\Models\Client;
use Laravel\Passport\Passport;
// In your AppServiceProvider or a custom service provider
public function boot()
{
Passport::useClientModel(Client::class);
}
namespace App\Models;
use Inmanturbo\Homework\Concerns\SkipsAuthorizationForFirstPartyClients;
use Laravel\Passport\Client as PassportClient;
class Client extends PassportClient
{
use SkipsAuthorizationForFirstPartyClients;
// Your custom model code...
}
use App\Models\Client;
use Laravel\Passport\Passport;
public function boot()
{
Passport::useClientModel(Client::class);
}
use Inmanturbo\Homework\Http\Middleware\AutoApproveFirstPartyClients;
// In your AppServiceProvider or a custom service provider
public function boot()
{
// Register the middleware alias
$this->app['router']->aliasMiddleware(
'auto-approve-first-party',
AutoApproveFirstPartyClients::class
);
// Add to web middleware group
$this->app['router']->pushMiddlewareToGroup(
'web',
AutoApproveFirstPartyClients::class
);
}
use Laravel\Passport\Passport;
// In your AppServiceProvider or a custom service provider
public function boot()
{
Passport::authorizationView('homework::auth.authorize');
}
use Laravel\WorkOS\User as WorkOsUser;
class User extends Authenticatable
{
public function workosUser(): WorkOsUser
{
return new WorkOsUser(
id: (string) $this->id,
organizationId: $this->organization_id, // Support for organizations
firstName: $this->first_name,
lastName: $this->last_name,
email: $this->email,
avatar: $this->avatar_url,
);
}
}
use App\Services\CustomUserResponse;
use Inmanturbo\Homework\Contracts\UserResponseContract;
public function register()
{
$this->app->bind(UserResponseContract::class, CustomUserResponse::class);
}
use App\Services\CustomUserResponse;
use Inmanturbo\Homework\Homework;
public function boot()
{
Homework::useUserResponse(CustomUserResponse::class);
}
namespace App\Services;
use Illuminate\Contracts\Auth\Authenticatable;
use Inmanturbo\Homework\Support\UserResponse;
class CustomUserResponse extends UserResponse
{
public function transform(Authenticatable $user): array
{
$response = parent::transform($user);
// Add additional fields
$response['organization_id'] = $user->organization_id ?? null;
$response['metadata'] = $user->metadata ?? [];
return $response;
}
}
namespace App\Services;
use Illuminate\Contracts\Auth\Authenticatable;
use Inmanturbo\Homework\Contracts\AuthenticationResponseContract;
use Inmanturbo\Homework\Contracts\UserResponseContract;
class CustomAuthenticationResponse implements AuthenticationResponseContract
{
public function __construct(
protected UserResponseContract $userResponse
) {
}
public function build(Authenticatable $user, array $tokenData): array
{
$userResponse = $this->userResponse->transform($user);
// Extract organization_id to top level (WorkOS SDK personator ?? null,
];
}
}
use App\Services\CustomAuthenticationResponse;
use Inmanturbo\Homework\Contracts\AuthenticationResponseContract;
public function register()
{
$this->app->bind(AuthenticationResponseContract::class, CustomAuthenticationResponse::class);
}
use App\Services\CustomAuthenticationResponse;
use Inmanturbo\Homework\Homework;
public function boot()
{
Homework::useAuthenticationResponse(CustomAuthenticationResponse::class);
}
namespace App\Services;
use Illuminate\Contracts\Auth\Authenticatable;
use Inmanturbo\Homework\Contracts\TokenClaimsProviderContract;
class UserTokenClaimsProvider implements TokenClaimsProviderContract
{
/**
* Get the organization IDs for the user.
* Return array of organization IDs the user belongs to.
*/
public function getOrganizations(Authenticatable $user): array
{
// Example: Query user's organizations
return $user->organizations()->pluck('id')->toArray();
// Or return a single organization
// return $user->organization_id ? [$user->organization_id] : [];
}
/**
* Get the permissions for the user.
* Return null to omit permissions from response.
*/
public function getPermissions(Authenticatable $user): ?array
{
// Example: Get from a permissions system
return $user->getAllPermissions()->pluck('name')->toArray();
// Or return null if not using permissions
// return null;
}
/**
* Get the roles for the user.
* Return null to omit roles from response.
*/
public function getRoles(Authenticatable $user): ?array
{
// Example: Get user roles
return $user->roles()->pluck('name')->toArray();
// Or return null if not using roles
// return null;
}
/**
* Get custom metadata for the user.
* These will be merged into the user object.
* Return null or empty array to omit.
*/
public function getMetadata(Authenticatable $user): ?array
{
return [
'department' => $user->department,
'employee_id' => $user->employee_id,
'custom_claim' => 'custom_value',
];
// Or return null if no custom metadata
// return null;
}
}
use App\Services\UserTokenClaimsProvider;
use Inmanturbo\Homework\Homework;
public function boot()
{
Homework::useTokenClaimsProvider(UserTokenClaimsProvider::class);
}
namespace App\Services;
use Illuminate\Contracts\Auth\Authenticatable;
use Inmanturbo\Homework\Contracts\OrganizationProviderContract;
class UserOrganizationProvider implements OrganizationProviderContract
{
public function getOrganizationsForUser(Authenticatable $user): array
{
return [
[
'id' => 'org_123',
'name' => 'Acme Corp',
'logo_url' => 'https://example.com/logos/acme.png', // optional
],
[
'id' => 'org_456',
'name' => 'Tech Startup Inc',
],
];
// Or from a relationship:
// return $user->organizations->map(fn($org) => [
// 'id' => $org->id,
// 'name' => $org->name,
// 'logo_url' => $org->logo_url, // optional - defaults to building icon
// ])->toArray();
}
}
use App\Services\UserOrganizationProvider;
use Inmanturbo\Homework\Homework;
public function boot()
{
Homework::useOrganizationProvider(UserOrganizationProvider::class);
}
use Inmanturbo\Homework\Http\Middleware\RequireOrganizationSelection;
public function boot()
{
$this->app['router']->pushMiddlewareToGroup(
'web',
RequireOrganizationSelection::class
);
}
use Inmanturbo\Homework\Homework;
public function boot()
{
Homework::organizationSelectionView(fn ($data) => view('auth.select-organization', $data));
// $data contains: organizations, user, state, clientId, redirectUri, responseType
}
// In your routes/auth.php
Route::get('authenticate', function (AuthKitAuthenticationRequest $request) {
return tap(redirect()->intended(route('dashboard')), fn () => $request->authenticate());
})->middleware(['guest']);