## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require litesaml/lightsaml:^5.0
Requires PHP 8.1+. Note: Version 5.x introduces breaking changes (see Gotchas and Tips).
First Use Case:
use LiteSaml\Model\AuthnRequest;
use LiteSaml\Model\Issuer;
use LiteSaml\Model\NameIdPolicy;
$request = new AuthnRequest();
$request->setIssuer(new Issuer('https://your-sp.example.com/metadata'));
$request->setDestination('https://idp.example.com/sso');
$request->setNameIdPolicy(new NameIdPolicy('urn:oasis:names:tc:SAML:2.0:nameid-format:persistent'));
$xml = $request->toXml();
use LiteSaml\Model\Response;
use LiteSaml\Binding\HttpPostBinding;
$response = Response::fromXml($xml);
$binding = new HttpPostBinding();
$binding->send($response, 'https://your-sp.example.com/acs');
Key Resources:
LiteSaml\Model\* classes for core SAML objects (e.g., AuthnRequest, Response, Assertion).LiteSaml\Binding\* for HTTP/Artifact bindings (e.g., HttpPostBinding, HttpRedirectBinding).SP-Initiated Login:
AuthnRequest → Serialize to XML → Send via HttpRedirectBinding.Response from IDP → Validate XSW protection → Authenticate user.
try {
$response = Response::fromXml($xml); // Automatically checks XSW invariants
// Proceed with validation...
} catch (\LiteSaml\Exception\SecurityException $e) {
// Handle XSW attack or signature failure
}
IDP-Initiated Login:
AuthnRequest from SP → Generate Response → Send via HttpPostBinding.ds:Signature in responses adheres to XSW protection rules (see Gotchas).Metadata Handling (Unchanged):
IdpSsoDescriptor).
use LiteSaml\Model\Metadata\EntitiesDescriptor;
$entities = EntitiesDescriptor::fromXml($metadataXml);
$idpDescriptor = $entities->getByEntityId('https://idp.example.com/metadata');
Signature Validation (Updated):
SignatureValidator (v5+).
use LiteSaml\Model\XmlDSig\SignatureValidator;
$validator = new SignatureValidator();
$validator->addCertificate($idpCertificate);
$isValid = $validator->validate($response->getSignature());
// Throws `SecurityException` if XSW attack detected.
Laravel Middleware (Updated for v5):
namespace App\Http\Middleware;
use LiteSaml\Binding\HttpPostBinding;
use LiteSaml\Model\Response;
class SamlAuthenticate
{
public function handle($request, Closure $next)
{
if ($request->isSamlPost()) {
try {
$binding = new HttpPostBinding();
$response = $binding->receive($request);
// XSW protection is automatic; proceed with validation...
} catch (\LiteSaml\Exception\SecurityException $e) {
\Log::warning("SAML Security Error: " . $e->getMessage());
return redirect('/error');
}
}
return $next($request);
}
}
Session Management (Unchanged):
session(['saml_session_id' => $response->getInResponseTo()]);
Error Handling (Updated):
LiteSaml\Exception\SecurityException for XSW attacks or signature failures.LiteSaml\Exception\ValidationException for malformed messages.Configuration (Recommended for v5):
config([
'saml' => [
'security' => [
'xsw_protection' => true, // Default: true (enabled)
'strict_canonicalization' => true, // Recommended for SAML 2.0
],
'sp' => [
'entity_id' => 'https://your-sp.example.com/metadata',
'certificate' => file_get_contents('path/to/sp-cert.pem'),
],
],
]);
XML Security (Critical for v5):
ds:Signature parent element must have an Id attribute matching the Signature’s Reference URI.Id must be unique in the document.Exclusive XML Canonicalization (default in v5) for SAML 2.0 compliance.Bindings (Unchanged):
RelayState and SAMLResponse encoding.Destination URLs to prevent open redirects.Time Constraints (Unchanged):
if ($assertion->getConditions()->getNotOnOrAfter() < now()) {
throw new \RuntimeException('Expired assertion');
}
Metadata (Unchanged):
Issuer vs. EntityID.getByEntityId() safely (see #91).Breaking Changes in v5:
LiteSaml\Context\Context is now immutable. Use LiteSaml\Context\ImmutableContext instead.LiteSaml\Helper::loadCertificate() is deprecated. Use PHP’s openssl_x509_read() or libraries like webmozart/assert.XsdValidator is stricter by default. Disable with:
$validator = new \LiteSaml\Validator\XsdValidator(['strict' => false]);
XML Dumping (Unchanged):
echo $request->toXml(); // Debug raw XML
XSW Validation Errors:
LiteSaml\Validator\SignatureXmlValidator:
\LiteSaml\Validator\SignatureXmlValidator::setDebugMode(true);
SecurityException: XML Signature Wrapping attack detected. Signature ID 'sig-1' does not match parent element ID.
Validation Errors (Updated):
LiteSaml\Validator\CompositeValidator to chain validators:
$validator = new \LiteSaml\Validator\CompositeValidator([
new \LiteSaml\Validator\XsdValidator(),
new \LiteSaml\Validator\SignatureXmlValidator(),
]);
$validator->validate($response->toXml());
Certificate Handling (Updated):
$cert = openssl_x509_read(file_get_contents('cert.pem'));
if ($cert === false) {
throw new \RuntimeException('Invalid certificate');
}
Custom Bindings (Unchanged):
Extend LiteSaml\Binding\AbstractBinding for custom protocols.
Validation Rules (Updated for v5):
Override LiteSaml\Validator\AbstractValidator with immutable context:
use LiteSaml\Context\ImmutableContext;
class CustomValidator extends AbstractValidator {
public function validate($xml, ImmutableContext $context) {
// Your logic here
}
}
3
How can I help you explore Laravel packages today?