fereydooni/laravel-sso

A robust Single Sign-On (SSO) system for Laravel

dev-master 2025-04-24 15:42 UTC

This package is auto-updated.

Last update: 2025-04-24 15:51:27 UTC


README

A robust Single Sign-On (SSO) system for Laravel applications using Laravel Passport for OAuth2 authentication, with optional social media authentication via Laravel Socialite and two-factor authentication (2FA).

Features

  • OAuth2 authentication using Laravel Passport
  • Social media login via Laravel Socialite (Google, Facebook, Twitter, GitHub)
  • Two-factor authentication via SMS and email channels
  • Vue.js-based admin panel for managing OAuth2 applications
  • Dedicated authentication page
  • Support for SSO across multiple Laravel applications

Requirements

  • PHP 8.1+
  • Laravel 10.x
  • Laravel Passport 11.x
  • Laravel Socialite 5.x
  • Vue 3.x (for admin panel and authentication page)

Installation

1. Composer Installation

composer require fereydooni/laravel-sso

2. Publish Configuration

php artisan vendor:publish --provider="Fereydooni\LaravelSso\SSOAuthServiceProvider"

3. Run Migrations

php artisan migrate

4. Set Up Laravel Passport

php artisan passport:install

5. Set Up Socialite Providers (Optional)

Add your Socialite provider credentials to the config/sso-auth.php configuration file.

6. Set Up Vue.js Components (Optional)

npm install
npm run dev

Configuration

Edit the config/sso-auth.php file to customize:

  • OAuth2 settings
  • Socialite providers
  • Two-factor authentication
  • Admin panel settings

Usage

Basic Usage with Attributes

use Fereydooni\LaravelSso\Attributes\SSOAuth;
use Fereydooni\LaravelSso\Attributes\TwoFactorChannel;
use Fereydooni\LaravelSso\SSOManager;

#[SSOAuth(provider: 'oauth2', scopes: ['read', 'write'], twoFactor: true)]
#[TwoFactorChannel(channel: 'email', priority: 1)]
class AuthController extends Controller
{
    public function login(Request $request)
    {
        $manager = app(SSOManager::class);
        $user = $manager->authenticate('oauth2', $request);
        
        if ($manager->requiresTwoFactor($user)) {
            $manager->sendTwoFactorCode($user, 'email');
            return redirect()->route('2fa.verify');
        }
        
        return $manager->generateToken($user);
    }
}

Social Authentication

#[SSOAuth(provider: 'socialite:google', scopes: ['profile'], twoFactor: true)]
#[TwoFactorChannel(channel: 'sms', priority: 1)]
public function googleLogin(Request $request)
{
    $manager = app(SSOManager::class);
    return $manager->redirectToProvider('google');
}

public function googleCallback(Request $request)
{
    $manager = app(SSOManager::class);
    $user = $manager->handleProviderCallback('google', $request);
    
    if ($manager->requiresTwoFactor($user)) {
        $manager->sendTwoFactorCode($user, 'sms');
        return redirect()->route('2fa.verify');
    }
    
    return $manager->generateToken($user);
}

Admin Panel

Access the admin panel at /sso/admin to manage your OAuth2 clients.

Authentication Page

Direct users to /sso/login for the authentication page.

Testing

composer test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This package is open-sourced software licensed under the MIT license.