Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Saml Laravel Package

onelogin/php-saml

PHP toolkit for adding SAML 2.0 SSO to your app. Handles login/logout, assertion processing, metadata generation, and signature/encryption validation, with strict security options and PHP 7.3+ (4.x) or older PHP support via branches.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Laravel Compatibility: The package is PHP-based and framework-agnostic, making it a strong fit for Laravel (PHP 7.3+ or 8.x). Laravel’s middleware, routing, and service container can seamlessly integrate with the package’s endpoints (acs.php, sls.php, metadata.php).
  • SAML Use Case Alignment: Ideal for implementing Service Provider (SP) functionality in Laravel apps requiring SSO/SLO with Identity Providers (IdPs) like Okta, Azure AD, or OneLogin.
  • Modularity: The package’s session-less design avoids conflicts with Laravel’s session management, allowing clean integration via middleware or service classes.

Integration Feasibility

  • Low-Coupling Design: The package’s endpoints can be mapped to Laravel routes (e.g., /saml/acs, /saml/sls) with minimal boilerplate. Middleware can pre-process SAML requests/responses.
  • Configuration Flexibility: Supports both file-based (risky with Composer updates) and programmatic settings (recommended). Laravel’s config/ system can centralize SAML settings.
  • Dependency Conflicts: Minimal risks—core dependencies (xmlseclibs, openssl) are standard in Laravel’s ecosystem. Potential conflicts with mcrypt (deprecated in PHP 7.2+) can be mitigated via ext-openssl for encryption.

Technical Risk

  • Security Hardening: Requires strict validation (e.g., strict: true, SHA-256 signatures) to mitigate replay/redirect attacks. Laravel’s middleware can enforce these checks pre/post-SAML processing.
  • Certificate Management: SP/IdP certificates must be securely stored (e.g., Laravel’s storage/ or environment variables). Key rollover (e.g., sp_new.crt) needs automation (e.g., cron jobs or Laravel tasks).
  • Binding Support: HTTP-Redirect/POST bindings must align with Laravel’s routing. POST bindings may require custom middleware to handle raw POST data.
  • Legacy Code: Backward compatibility with v1.x APIs exists but should be avoided in new Laravel projects.

Key Questions

  1. IdP Selection: Which IdP(s) will integrate? Metadata parsing (via curl) may need Laravel-specific HTTP clients (e.g., Guzzle).
  2. Session Management: How will Laravel sessions interact with SAML sessions? Use Laravel’s auth:guard() or custom session drivers.
  3. Error Handling: How to log SAML validation failures? Laravel’s logging system can integrate with the package’s error outputs.
  4. Testing: How to mock SAML responses for unit tests? Use Laravel’s HTTP tests or custom test doubles for OneLogin_Saml2_Response.
  5. Performance: Will SAML assertions be cached? Laravel’s cache system (Redis/Memcached) can store assertion IDs to prevent replay attacks.

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Routing: Map SAML endpoints to Laravel routes (e.g., Route::post('/saml/acs', [SamlController::class, 'handleAcs'])).
    • Middleware: Create middleware to validate SAML requests/responses before/after processing.
    • Service Container: Bind the SAML auth class to Laravel’s IoC (e.g., app()->bind(OneLogin_Saml2_Auth::class, fn() => new OneLogin_Saml2_Auth($settings))).
    • Configuration: Store settings in config/saml.php (merge with settings_example.php).
  • Dependencies:
    • Required: php-openssl, php-xml (enabled by default in Laravel).
    • Optional: php-curl (for IdP metadata parsing), php-mcrypt (deprecated; replace with openssl for encryption).

Migration Path

  1. Installation:
    composer require onelogin/php-saml:^4.0  # For PHP 7.3+/8.x
    
  2. Configuration:
    • Copy settings_example.phpconfig/saml.php.
    • Replace hardcoded paths with Laravel’s storage_path() or environment variables.
    • Example:
      $settings = [
          'strict' => true,
          'sp' => [
              'entityId' => env('SAML_ENTITY_ID'),
              'x509cert' => file_get_contents(storage_path('certs/sp.crt')),
              'privateKey' => file_get_contents(storage_path('certs/sp.key')),
          ],
          'security' => [
              'signatureAlgorithm' => 'sha256',
              'digestAlgorithm' => 'sha256',
          ],
      ];
      
  3. Endpoint Integration:
    • Create a SamlController to handle acs, sls, and metadata:
      public function handleAcs(Request $request) {
          $auth = app(OneLogin_Saml2_Auth::class);
          $requestId = $auth->validateAuthnRequest();
          // Process SAML response and redirect to Laravel route.
      }
      
    • Use Laravel’s Route::post() for POST bindings (handle raw POST data).
  4. Middleware:
    • Create SamlMiddleware to validate SAML responses before processing:
      public function handle($request, Closure $next) {
          $auth = app(OneLogin_Saml2_Auth::class);
          if (!$auth->isValidResponse($request->get('SAMLResponse'))) {
              abort(403, 'Invalid SAML response');
          }
          return $next($request);
      }
      
  5. Session Binding:
    • After SAML validation, create a Laravel session/user:
      Auth::loginUsingId($auth->getAttributes()['email']);
      

Compatibility

  • Laravel Versions: Tested with Laravel 8+ (PHP 7.4+) and 9/10 (PHP 8.x). Avoid PHP 7.0–7.2 due to package version constraints.
  • Binding Support:
    • HTTP-Redirect: Use Laravel’s redirect()->to() with RelayState.
    • HTTP-POST: Laravel’s Request object can access $_POST['SAMLResponse'].
  • Certificate Storage: Prefer Laravel’s storage/app/certs/ or environment variables over vendor/ to avoid Composer update risks.

Sequencing

  1. Phase 1: Configure SP metadata and test IdP-initiated SSO.
  2. Phase 2: Implement SP-initiated SSO with Laravel route redirection.
  3. Phase 3: Add Single Logout (SLO) via sls.php endpoint.
  4. Phase 4: Integrate with Laravel’s auth system (e.g., sync SAML attributes to Laravel users).
  5. Phase 5: Add replay attack protection (cache assertion IDs in Laravel’s cache).

Operational Impact

Maintenance

  • Updates: Monitor onelogin/php-saml for security patches (e.g., CVE-2025-66475). Use Composer’s update cautiously (back up config/saml.php).
  • Certificate Rotation: Automate key rollover using Laravel tasks or cron jobs to update sp.crt/sp.key.
  • Logging: Centralize SAML logs in Laravel’s logs/saml.log using Monolog:
    $auth->setDebugLogFile(storage_path('logs/saml.log'));
    $auth->setDebugLogLevel(OneLogin_Saml2_Utils::LOG_DEBUG);
    
  • Monitoring: Track SAML failures (e.g., invalid signatures) via Laravel’s report() method or Sentry.

Support

  • Troubleshooting:
    • Enable debug logs (debugLogLevel: LOG_DEBUG) for SAML validation errors.
    • Use Laravel’s dd() or dump() to inspect $auth->getLastErrorReason().
  • IdP-Specific Issues: Maintain contact info for the IdP (e.g., Okta support) in SAML settings.
  • User Support: Document SAML-specific error messages (e.g., "Invalid RelayState") for end-users.

Scaling

  • Performance:
    • Caching: Cache IdP metadata and SP certificates in Laravel’s cache (e.g., Redis) to reduce parsing overhead.
    • Load Testing: Simulate high-traffic SAML flows (e.g., 1000+ SSO requests/hour) to validate Laravel’s session handling.
  • Horizontal Scaling:
    • Stateless Endpoints: Ensure acs.php/sls.php are stateless (no shared memory). Use Laravel’s queue system for async processing if needed.
    • Database: Offload assertion ID storage to a shared database (e.g., PostgreSQL) for replay attack prevention in clustered environments.
  • Microservices: If using Laravel as a microservice, expose SAML endpoints via an API gateway (e.g., Laravel Octane + Swoole).

Failure Modes

| Failure Scenario | Impact | Mitigation | |--------------------------------|--------------------------------------

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony