eighteen73 / laravel-sso
Abstract SSO logic into a reusable, self-contained Laravel package.
Requires
- php: ^8.3
- firebase/php-jwt: ^6.4 || ^7.0
- illuminate/contracts: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
- laravel/socialite: ^5.12
- socialiteproviders/logto: ^1.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.50
- laravel/pint: ^1.29
- orchestra/testbench: ^10.0 || ^11.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/phpstan: ^2.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A reusable Laravel package for integrating Single Sign-On (SSO) using Laravel Socialite, with built-in support for Logto and Filament.
Note: This is an opinionated, internal project designed primarily to meet the requirements of eighteen73. While it is open-sourced and feedback is welcome, its development is driven by our specific needs and workflows.
Features
- Automatic Socialite provider registration for Logto.
- Dedicated
sso_accountstable to map SSO identities to local users. - Optional user model helpers for SSO account relationships and login policy checks.
- Configurable user resolution with opt-in auto-creation logic.
- Automatic integration with Filament panels: login button, SSO-aware login page, global sign-out and session checks.
- App-only sign-out by default, an optional global sign-out route, and OpenID Connect back-channel logout.
- Access revalidation every few minutes, with profile claims kept in sync.
- ID token verification against Logto's published keys.
- A
sso.profileroute to the user's Logto account page. php artisan sso:checkto verify the setup against Logto.- Translatable user-facing messages.
- Support for multiple SSO connections per user.
Installation
You can install the package via composer:
composer require eighteen73/laravel-sso
The migrations load automatically. Optionally publish the config file, then run the migrations:
php artisan vendor:publish --tag="sso-config"
php artisan migrate
Add your Logto application's details to .env. LOGTO_BASE_URL is the Logto endpoint without the trailing /oidc.
LOGTO_BASE_URL=https://auth.example.com LOGTO_CLIENT_ID= LOGTO_CLIENT_SECRET=
Then check the setup against Logto. The command lists the URIs and permissions to register:
php artisan sso:check
In the Logto Admin Console, the application (a Traditional Web application) needs:
- Redirect URI:
{APP_URL}/sso/callback - Post sign-out redirect URI:
{APP_URL}, orLOGTO_POST_LOGOUT_REDIRECT_URIif set - Back-channel logout URI:
{APP_URL}/sso/backchannel-logout, with Is session required turned on - Permissions:
profile,emailandoffline_access, plus anything added tosso.scopes - Rules: who may use the application (app-level access control)
MFA is enforced through Logto's own multi-factor auth settings rather than by this package.
Configuration
The configuration file is located at config/sso.php. You can customise the following:
provider: The Socialite driver to use (defaulting tologto).auto_create_users: Whether to create a new local user if the SSO email is not found. This is disabled by default, so unknown SSO users are rejected unless you explicitly opt into provisioning.require_verified_email: When an SSO identity is not yet linked, it is matched to (or used to create) a local user by email. This is only allowed when Logto reportsemail_verified: true. Enabled by default; setSSO_REQUIRE_VERIFIED_EMAIL=falseto disable. Identities already linked by provider ID are unaffected.scopes: Extra scopes to request on top ofopenid,profileandemail. Logto only returns some claims when their scope is requested, for example['custom_data', 'roles'].revalidate_after: How often, in minutes, to confirm that a signed-in SSO user still has access. See Access Revalidation. Defaults to10; setSSO_REVALIDATE_AFTER=nullto disable it.redirect_path: The path to redirect to after a successful login.user_resolver: The action class used to map SSO data to a local user.filament: Settings for Filament integration, including which panels to display the SSO button on.
Filament Integration
On the panels in sso.filament.panels, the package needs no further setup. It:
- shows the "Sign in with eighteen73" button, with any SSO error above it;
- replaces Filament's default login page with
Eighteen73\SSO\Filament\Login, which:- sends browsers that last signed in through SSO straight to Logto, which signs the user in silently while its session lasts. It never does this straight after a sign-out or while showing an SSO error. Add
?password=1to the login URL to show the form instead; - refuses password logins for users whose
requiresSsoLogin()is true (by default, users linked to the current provider), so access removed in Logto cannot be bypassed with a password. The check runs after the password is verified;
- sends browsers that last signed in through SSO straight to Logto, which signs the user in silently while its session lasts. It never does this straight after a sign-out or while showing an SSO error. Add
- keeps Filament's sign-out app-only (see Sessions and Sign-out), but marks the session so the login page then shows the form rather than sending the user straight back to Logto;
- adds the
RevalidateSsoSessionmiddleware to the authenticated routes, and registers it as Livewire persistent middleware so actions on an open page are checked too.
Panels with a custom login page keep it; extend Eighteen73\SSO\Filament\Login to get the same behaviour. Outside Filament, add RevalidateSsoSession to your authenticated routes yourself.
Sessions and Sign-out
- No remember-me. SSO sign-ins never set Laravel's remember-me cookie. Logto's own session (14 days by default) keeps users signed in: when the app session expires, signing in again through Logto needs no password.
- App sign-out. Signing out of an app (including Filament's sign-out) ends only that app's session, as with sites that use "Sign in with Google". The user stays signed in to Logto, so "Sign in with eighteen73" signs them back in without a password. On a shared computer, use global sign-out instead.
- Global sign-out.
route('sso.logout')ends the app session and the Logto session via Logto's end-session endpoint, then returns toLOGTO_POST_LOGOUT_REDIRECT_URI(defaultAPP_URL). Link to it where users should be able to sign out everywhere, and register that URL as a post sign-out redirect URI in Logto. Through back-channel logout, other apps the user is signed in to follow. - Back-channel logout. When a user signs out of Logto elsewhere, Logto calls
/sso/backchannel-logoutwith a signed logout token. The package verifies it and signs out the app session that belongs to that Logto session (matched bysid) on its next request, throughRevalidateSsoSession. If the token has nosid, every earlier session of that user is signed out. Logto only putssidin ID tokens when the application's back-channel logout has Is session required turned on, so turn it on before users sign in. Logto must be able to reach the URI, so this does not work against a local development site. - ID tokens are verified. At sign-in, the ID token's signature (against Logto's published keys), issuer, audience, expiry and subject are checked before the user is signed in.
Profile Link
route('sso.profile') redirects to the user's account page in Logto, so apps can link to it anywhere. It uses sso.profile_url (SSO_PROFILE_URL) if set, otherwise Logto's Account Center at {LOGTO_BASE_URL}/account, which must be enabled in Logto.
Translations
User-facing text, including the button label and every error, is in lang/en/messages.php. Publish it to reword it:
php artisan vendor:publish --tag="sso-lang"
Access Revalidation
A local session would otherwise outlive changes made in Logto, such as removing a user from an application's access rules. The package confirms access every revalidate_after minutes (10 by default) and signs out users who have lost it.
How it works:
- Logins request
offline_accesswithprompt=consent(unlesslogin_parameterssets aprompt), as Logto only issues a refresh token when both are present. Allowoffline_accessfor the application in Logto. - On a request from a user with an SSO account whose last check is older than the interval, the middleware uses the refresh token. Logto checks the application's access rules again for refresh token requests.
- If Logto refuses the refresh token (
invalid_grant, oraccess_deniedwhen the user no longer matches the application's access rules), the user is signed out with a message. Users with no refresh token, such as those signed in before revalidation was enabled, are asked to sign in again. - After a successful check, the user's claims are read again from Logto and synced (see Automatic Attribute Syncing), so profile changes such as a new nickname arrive without signing in again.
- If Logto cannot be reached or returns another error, the user stays signed in, the error is reported, and the check is retried after a minute.
- The check is per SSO account, so all of a user's sessions share the result. Idle users are checked on their next request.
- Users without an SSO account for the current provider are not checked, and can still sign in with a password.
Customising User Resolution
By default, the package links an SSO identity to an existing local user by matching the SSO email address. If no local user exists, login fails unless auto_create_users is explicitly enabled.
The built-in auto-creation path only fills common name, email, and password attributes. If your application requires additional columns such as first_name, last_name, is_enabled, role, tenant IDs, or any other app-specific state, create a custom action that implements Eighteen73\SSO\Actions\ResolveUserContract and update the user_resolver in your config.
You can also extend the default resolver if you only need to add behavior around the package's built-in linking logic:
namespace App\Actions; use Eighteen73\SSO\Actions\ResolveUser; use Laravel\Socialite\Contracts\User as ProviderUser; use Illuminate\Contracts\Auth\Authenticatable; class CustomResolveUser extends ResolveUser { public function resolve(string $provider, ProviderUser $ssoUser): Authenticatable { $user = parent::resolve($provider, $ssoUser); // Add your custom logic here return $user; } }
User Model Helpers
Your application user model can opt into SSO relationship and policy helpers by using the HasSsoAccounts trait:
namespace App\Models; use Eighteen73\SSO\Concerns\HasSsoAccounts; use Eighteen73\SSO\Contracts\HasSsoAccounts as HasSsoAccountsContract; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements HasSsoAccountsContract { use HasSsoAccounts; public function requiresSsoLogin(): bool { return $this->role->requiresSso(); } }
By default, requiresSsoLogin() returns true for users linked to the current provider (sso.provider), which the package's Filament login page uses to refuse their password logins. canUsePasswordLogin() returns true. Override either in your application when roles, tenants, or other local rules need different behaviour.
User Profile Management URL
The trait also provides a getSsoProfileUrl(?string $provider = null) method that returns the same URL as route('sso.profile'): sso.profile_url if set, otherwise Logto's Account Center:
// Returns sso.profile_url, or {LOGTO_BASE_URL}/account $profileUrl = $user->getSsoProfileUrl();
SSO User Details & Attribute Syncing
The package automatically captures all raw OpenID Connect (OIDC) claims returned by Logto / Socialite on every login and stores them as JSON in the sso_accounts.claims column.
Accessing Claims via HasSsoAccounts Trait
// Retrieve all raw claims returned during the last login $claims = $user->getSsoClaims(); // Retrieve a specific claim (e.g. nickname, locale, or Logto custom data) // custom_data is only returned when 'custom_data' is in sso.scopes $department = $user->getSsoClaim('custom_data.department'); // Get the user's avatar URL $avatar = $user->getSsoAvatar(); // Logto global roles (requires 'roles' in sso.scopes) $roles = $user->getSsoRoles(); $isAdmin = $user->hasSsoRole('admin');
Claims are stored at sign-in and refreshed by each successful access revalidation, so changes made in Logto (such as a new nickname or role) arrive within revalidate_after minutes of the user's next request. With revalidation disabled, they arrive at the next SSO sign-in.
Automatic Attribute Syncing
By default, the package syncs SSO claims to matching columns on your local User model on login: name (the full display name), nickname (the short name) and avatar_url. Columns that do not exist are skipped, and claims that are not set (Logto omits empty profile values) leave the column unchanged. Given and family name are not used. You can configure this in config/sso.php:
'sync_attributes_on_login' => env('SSO_SYNC_ATTRIBUTES', true), 'attribute_map' => [ 'name' => 'name', 'nickname' => 'nickname', 'avatar_url' => 'avatar', ],
In Logto, set the short name as nickname in the user's profile.
Upgrading from the Zitadel version
This release replaces Zitadel with Logto and is not backwards compatible. The last Zitadel release is tagged zitadel-final.
socialiteproviders/zitadelis replaced bysocialiteproviders/logto, and the provider name changes fromzitadeltologto.ZITADEL_*environment variables are replaced byLOGTO_*, andservices.zitadelbyservices.logto.- The
sso.zitadeloptions (enforce_mfa,select_account) are removed. Enforce MFA in Logto instead. getSsoProfileUrl()returnssso.profile_url, or Logto's Account Center ({LOGTO_BASE_URL}/account) when it is not set.sso.rememberis removed: SSO sign-ins never set a remember-me cookie, so back-channel logout can always match the session. Logto's own session keeps users signed in.- The login page sends returning SSO users straight to Logto (
?password=1shows the form). Filament's sign-out stays app-only; link toroute('sso.logout')to sign out of Logto too. - Register the back-channel logout URI (
{APP_URL}/sso/backchannel-logout, with "Is session required" on) and the post sign-out redirect URI in Logto.php artisan sso:checklists them. - ID tokens are now verified at sign-in. A Logto application whose ID token fails verification (for example a mismatched
LOGTO_BASE_URL) can no longer sign in. - Unlinked identities now need a verified email to be linked by email (see
require_verified_email). - Run
php artisan migratefor the newsso_accounts.validated_atcolumn. - Access revalidation is on by default. Existing users have no refresh token, so they are asked to sign in again once, on their first request at least
revalidate_afterminutes after their last SSO login. The application in Logto must allowoffline_access. - Users linked to the current provider can no longer sign in with a password on Filament panels that use the default login page.
- Logto access rules are enforced through the application's Rules tab in Logto; denied users see a message on the login page.
- The default
attribute_mapand auto-created users no longer use given or family name (first_name,last_name). They usenameandnicknameinstead. Add the old mappings to your published config if you still need them.
Testing
The package uses Pest for testing. You can run the tests with:
./vendor/bin/pest
License
The MIT License (MIT). Please see License File for more information.