ae/oneloginsaml-bundle
Symfony bundle wrapping OneLogin’s PHP SAML toolkit to add SAML 2.0 SSO/SLO to your app. Configure IdP/SP metadata via YAML, expose ACS/logout/metadata endpoints, and integrate with Symfony security firewalls for authentication flows.
Installation Add the bundle via Composer:
composer require ae/oneloginsaml-bundle:dev-master
Enable it in config/bundles.php (Symfony 4+) or app/AppKernel.php (Symfony 2/3):
AE\OneLoginSamlBundle\AEOneLoginSamlBundle::class => ['all' => true],
Basic Configuration
Define SAML settings in config/packages/ae_one_login_saml.yaml (Symfony 4+) or app/config/config.yml:
ae_one_login_saml:
default:
idp:
entityId: 'https://your-idp.com/saml2/idp/metadata'
singleSignOnService:
url: 'https://your-idp.com/saml2/idp/SSOService.php'
binding: 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect'
x509cert: '%env(ONELOGIN_IDP_CERT)%'
sp:
entityId: 'https://your-app.com/saml/metadata'
assertionConsumerService:
url: 'https://your-app.com/saml/acs'
binding: 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST'
First Use Case: Authentication
Configure a firewall in config/packages/security.yaml:
security:
firewalls:
main:
saml: true # Enables SAML auth
context: saml
Test by accessing a protected route—you’ll be redirected to the IdP for login.
User Authentication Flow
saml: true is set in the firewall.OneLogin_Saml_Utils::getAttributes() in controllers to access user attributes post-auth:
use AE\OneLoginSamlBundle\Utils\OneLoginSamlUtils;
public function postAuthAction(Request $request)
{
$attributes = OneLoginSamlUtils::getAttributes();
$userEmail = $attributes['email'][0] ?? null;
// Create/update user in your system
}
Metadata Generation Generate SP metadata dynamically for IdP configuration:
php bin/console ae:onelogin:saml:metadata
Outputs XML to var/saml/metadata.xml.
Custom Attribute Mapping
Map IdP attributes to Symfony users in config/packages/ae_one_login_saml.yaml:
ae_one_login_saml:
default:
attribute_map:
email: 'email'
first_name: 'firstName'
last_name: 'lastName'
Logout Handling Trigger SAML logout via a route:
# config/routes.yaml
saml_logout:
path: /logout
methods: GET
defaults:
_controller: AE\OneLoginSamlBundle\Controller\SamlController::logoutAction
Symfony Security Events: Listen for security.interactive_login to sync user data:
// src/EventListener/SamlAuthListener.php
public function onSamlLogin(InteractiveLoginEvent $event)
{
$user = $event->getUser();
$attributes = OneLoginSamlUtils::getAttributes();
// Update user entity with SAML attributes
}
Register in config/services.yaml:
services:
App\EventListener\SamlAuthListener:
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSamlLogin }
Environment Variables: Store sensitive IdP certs in .env:
ONELOGIN_IDP_CERT="-----BEGIN CERTIFICATE-----\n..."
Reference in YAML:
idp:
x509cert: '%env(ONELOGIN_IDP_CERT)%'
Debugging SAML: Enable debug mode in config:
ae_one_login_saml:
default:
debug: true
Logs SAML messages to var/log/saml.log.
Certificate Handling
x509cert causes SAML validation failures.openssl x509 -in cert.pem -noout -text).%env() for .env vars).EntityID Mismatch
sp.entityId doesn’t match the ACS URL.entityId matches the route path (e.g., https://your-app.com/saml/metadata).Binding Configuration
HTTP-Redirect for ACS) may fail if IdP expects HTTP-POST.Attribute Access Timing
OneLoginSamlUtils::getAttributes() returns null outside SAML flows.if (OneLoginSamlUtils::isSamlRequest()) {
$attributes = OneLoginSamlUtils::getAttributes();
}
Symfony 4+ Kernel Changes
AppKernel (Symfony 2/3). In Symfony 4+, ensure:
config/bundles.php.config/packages/ae_one_login_saml.yaml.CSRF on ACS
security.yaml:
firewalls:
main:
saml: true
csrf_protection: false
debug: true and check var/log/saml.log for raw SAML messages.Custom Auth Provider
Override the default SamlAuthenticationProvider by extending AE\OneLoginSamlBundle\Security\SamlAuthenticationProvider and configuring it in security.yaml:
security:
providers:
saml_provider:
id: App\Security\CustomSamlProvider
Attribute Filters Filter or transform attributes before user creation:
// src/EventListener/SamlAttributeListener.php
public function onSamlAuth(SamlAuthEvent $event)
{
$attributes = $event->getAttributes();
$attributes['normalized_email'] = strtolower($attributes['email'][0]);
$event->setAttributes($attributes);
}
Dynamic Configuration Load SAML settings from a database or API:
ae_one_login_saml:
default:
idp:
entityId: '%env(SAML_IDP_ENTITYID)%'
# Other dynamic settings...
Use a compiler pass to resolve values at runtime.
Multi-IdP Support Configure multiple IdPs in the same app by defining multiple configs:
ae_one_login_saml:
idp1:
# Config for IdP 1
idp2:
# Config for IdP 2
Route users to the correct IdP based on context (e.g., subdomain).
How can I help you explore Laravel packages today?