PHP code example of misakstvanu / laravel-zsso-client

1. Go to this page and download the library: Download misakstvanu/laravel-zsso-client 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/ */

    

misakstvanu / laravel-zsso-client example snippets


use Illuminate\Contracts\Auth\Authenticatable;
use Misakstvanu\ZssoClient\Contracts\ProvisionsSsoUser;
use Misakstvanu\ZssoClient\Exceptions\ProvisioningDeniedException;
use Misakstvanu\ZssoClient\SsoUser;

class ProvisionUser implements ProvisionsSsoUser
{
    public function provision(SsoUser $user): Authenticatable
    {
        if (! $user->emailVerified) {
            // Back to `login_path` with the message flashed as `error`.
            throw new ProvisioningDeniedException('Nejdřív si ověř e-mail v zSSO.');
        }

        return User::updateOrCreate(
            ['zsso_sub' => $user->sub],
            ['name' => $user->name, 'email' => $user->email],
        );
    }
}

// AppServiceProvider::register()
$this->app->bind(ProvisionsSsoUser::class, ProvisionUser::class);

use Misakstvanu\ZssoClient\Events\SsoLoginCompleted;
use Misakstvanu\ZssoClient\Facades\Zsso;

class HydrateSkautisSession
{
    public function handle(SsoLoginCompleted $event): void
    {
        if ($event->ssoUser->skautis === null) {
            return;
        }

        Zsso::skautis($event->user)->session()?->applyTo(app(\Skautis\Skautis::class));
    }
}

// routes/web.php
Route::middleware(['zsso.silent'])->get('/{any?}', SpaController::class)->where('any', '.*');

use Misakstvanu\ZssoClient\Facades\Zsso;

$token = Zsso::tokenFor($user); // a live access token

use Misakstvanu\ZssoClient\Facades\Zsso;

$session = Zsso::skautis($user)->session();   // ?SkautisSessionData
$session?->applyTo($skautis);                 // hydrate a \Skautis\Skautis client

Zsso::skautis($user)->refresh();              // SkautIS keep-alive
Zsso::skautis($user)->switchRole(679);        // activate another role
Zsso::skautis($user)->logout();               // end the SkautIS login

use Misakstvanu\ZssoClient\Facades\Zsso;

$events = Zsso::integration('zirafa')->get('events')->json('data');

Zsso::integration('zirafa')
    ->actingAs($user->zsso_sub)                 // X-Zsso-Acting-User
    ->post('attendees', ['event_id' => 42]);

// config/zsso.php
'apps' => [
    'zirafa' => 'https://zirafa.localhost/api/integration/v1',
],

// routes/api.php
Route::middleware('zsso.integration')->prefix('integration/v1')->group(function () {
    Route::get('events', [EventController::class, 'index']);

    // 403 unless the caller sends X-Zsso-Acting-User
    Route::middleware('zsso.integration:acting-user')->get('my-events', [EventController::class, 'mine']);
});

use Misakstvanu\ZssoClient\IntegrationContext;

$context = IntegrationContext::current();

$context->callerSlug;    // 'zirafa'
$context->actingUserSub; // the zSSO uuid from X-Zsso-Acting-User, or null
$context->actsForUser(); // whether the caller named one

use Misakstvanu\ZssoClient\Facades\Zsso;

Zsso::webhook('zirafa.attendee.upserted', ['attendee_id' => $attendee->id])->to('zskauting');

// or to every app that receives webhooks, this one excluded
Zsso::webhook('zirafa.attendee.upserted', ['attendee_id' => $attendee->id])->toAll();

use Misakstvanu\ZssoClient\Events\WebhookDeadLettered;

Event::listen(function (WebhookDeadLettered $event) {
    report(new WebhookLost($event->app, $event->envelope->type, $event->envelope->id));
});

use Misakstvanu\ZssoClient\Facades\Zsso;
use Misakstvanu\ZssoClient\Webhooks\WebhookEnvelope;

Zsso::onWebhook('zirafa.attendee.upserted', UpsertAttendee::class);

Zsso::onWebhook('zsso.session.logout', function (WebhookEnvelope $envelope) {
    // $envelope->get('sub'), $envelope->app, $envelope->occurredAt
});

use Laravel\Socialite\Facades\Socialite;

return Socialite::driver('zsso')->redirect();

$user = Socialite::driver('zsso')->user(); // Misakstvanu\ZssoClient\SsoUser
bash
php artisan vendor:publish --tag=zsso-config
php artisan vendor:publish --tag=zsso-migrations
php artisan migrate

$ php artisan zsso:doctor

 ✔  zsso.server_url
 ✔  zsso.client_id
 ✔  zsso.client_secret
 ✔  zsso.app_slug
 ✔  discovery document
 ✔  signing keys
 ✔  client-credentials token