Installation
composer require druidfi/simplesamlphp-bundle
Add the bundle to config/bundles.php:
return [
// ...
DruidFi\SimpleSAMLphpBundle\DruidFiSimpleSAMLphpBundle::class => ['all' => true],
];
Configuration Copy the default config to your project:
php bin/console config:dump-reference DruidFi\SimpleSAMLphpBundle > config/packages/druidfi_simplesamlphp.yaml
Edit config/packages/druidfi_simplesamlphp.yaml to define:
path: Path to your SimpleSAMLphp installation (e.g., %kernel.project_dir%/var/simplesaml).authentication_method: core (default) or proxy.proxy_host: If using proxy mode (e.g., https://idp.example.com).First Use Case: Basic Authentication Secure a route with SimpleSAMLphp:
// src/Controller/SecuredController.php
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use DruidFi\SimpleSAMLphpBundle\Annotation\Secure;
class SecuredController {
#[Route('/secure', name: 'secure')]
#[Secure]
public function secureAction(): Response {
return new Response('This is a secured page!');
}
}
@Secure annotation enforces authentication via SimpleSAMLphp’s SP-initiated login.@Secure on controllers/methods to enforce authentication.
#[Secure(roles: ['user', 'admin'])] // Optional role checks
public function adminDashboard(): Response { ... }
use DruidFi\SimpleSAMLphpBundle\Service\SimpleSAMLphpService;
public function login(SimpleSAMLphpService $simplesaml): Response {
return $simplesaml->redirectToLogin();
}
metadata/saml20-idp-remote.php to include your SP.simplesamlphp:auth:check command to validate IdP metadata:
php bin/console simplesamlphp:auth:check
use DruidFi\SimpleSAMLphpBundle\Service\SimpleSAMLphpService;
public function userProfile(SimpleSAMLphpService $simplesaml): Response {
$attributes = $simplesaml->getAttributes();
$email = $attributes['email'][0] ?? null;
return $this->render('profile.html.twig', ['email' => $email]);
}
# config/packages/security.yaml
providers:
simplesaml:
id: DruidFi\SimpleSAMLphpBundle\Security\SimpleSAMLphpUserProvider
public function logout(SimpleSAMLphpService $simplesaml): Response {
$simplesaml->logout();
return $this->redirectToRoute('home');
}
# config/packages/security.yaml
firewalls:
main:
simplesamlphp: ~ # Delegates to the bundle
logout:
path: app_logout
target: home
SimpleSAMLphpUserProvider to map SimpleSAMLphp attributes to Symfony roles:
// src/Security/SimpleSAMLphpUserProvider.php
public function loadUserByUsername($username) {
$attributes = $simplesaml->getAttributes();
$roles = ['ROLE_USER'];
if ($attributes['is_admin'][0] ?? false) {
$roles[] = 'ROLE_ADMIN';
}
return new User($username, null, $roles);
}
SimpleSAMLphpAuthenticator to add pre/post-login logic:
// src/Security/ExtendedSimpleSAMLphpAuthenticator.php
use DruidFi\SimpleSAMLphpBundle\Security\SimpleSAMLphpAuthenticator;
class ExtendedSimpleSAMLphpAuthenticator extends SimpleSAMLphpAuthenticator {
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) {
// Log user activity, update DB, etc.
return parent::onAuthenticationSuccess($request, $token, $providerKey);
}
}
Register it in security.yaml:
firewalls:
main:
simplesamlphp:
authenticator: app.extended_simple_saml_authenticator
php bin/console simplesamlphp:metadata:sp
Outputs to var/simplesaml/metadata/sp-metadata.xml.Path Configuration:
path in druidfi_simplesamlphp.yaml causes FileNotFoundException.%kernel.project_dir%/var/simplesaml).Session Invalidation:
logout() method to clear both sessions:
$simplesaml->logout(); // Clears SimpleSAMLphp session
$this->get('security.token_storage')->setToken(null); // Clears Symfony token
Attribute Access:
getAttributes() returns null if the user isn’t authenticated.if ($simplesaml->isAuthenticated()) {
$attributes = $simplesaml->getAttributes();
}
Proxy Mode Quirks:
authentication_method: proxy) may fail if the IdP URL is misconfigured.proxy_host in config matches the IdP’s endpoint (e.g., https://idp.example.com/simplesaml).Metadata Validation:
simplesamlphp:auth:check command to validate:
php bin/console simplesamlphp:auth:check
Enable Debugging:
Set debug: true in druidfi_simplesamlphp.yaml to log SimpleSAMLphp errors to Symfony’s log channel.
Check SimpleSAMLphp Logs:
var/simplesaml/log/ (e.g., auth.log, admin.log).tail -f var/simplesaml/log/auth.log
Common Errors:
SimpleSAML_Error_Exception: Often due to misconfigured metadata or missing files.
php bin/console cache:clear
php bin/console simplesamlphp:metadata:sp
Session not found: Typically occurs if the session cookie is blocked or expired.
session.cookie_lifetime in Symfony’s framework.yaml matches SimpleSAMLphp’s session settings.Environment-Specific Config: Use Symfony’s parameter system to switch between dev/prod SimpleSAMLphp paths:
# config/packages/dev/druidfi_simplesamlphp.yaml
path: "%kernel.project_dir%/var/simplesaml_dev"
Custom Templates:
Override SimpleSAMLphp’s templates by copying them from vendor/simplesamlphp/simplesamlphp to var/simplesaml/templates/.
Performance:
metadata_cache_lifetime: 86400 # 24 hours
simplesamlphp:metadata:sp --cache to pre-generate metadata.Testing:
simplesamlphp:test:auth command to simulate authentication in tests:
// tests/Functional/SecurityTest.php
public
How can I help you explore Laravel packages today?