anglemx/onelogin-azure-saml-bundle
## Getting Started
### Minimal Setup for Azure AD SAML Integration
1. **Install the Bundle**
```bash
composer require anglemx/onelogin-azure-saml-bundle
Enable in config/bundles.php:
Angle\OneLoginAzureSamlBundle\AngleOneLoginAzureSamlBundle::class => ['all' => true],
Configure Azure AD Credentials
Add to config/packages/angle_one_login_azure_saml.yaml:
angle_one_login_azure_saml:
azure_app_id: 'your-azure-app-guid-here'
azure_x509_cert: '-----BEGIN CERTIFICATE-----\n...Azure AD cert...\n-----END CERTIFICATE-----'
app_base_url: 'https://your-app.com' # No trailing slash
Generate SP Metadata Run the built-in command to auto-generate SP settings:
php bin/console angle:onelogin:azure:saml:metadata
This outputs the EntityID and other SP URLs (e.g., https://your-app.com/sp).
Configure Azure AD App Registration
https://your-app.com/saml/acshttps://your-app.com/saml/loginhttps://your-app.com/sp (from the command output).https://your-app.com/saml/metadata) to Azure AD.Secure Routes
Update config/packages/security.yaml:
security:
providers:
saml_provider:
saml:
user_class: App\Entity\User
default_roles: ['ROLE_USER']
firewalls:
main:
saml:
username_attribute: uid # Azure AD attribute for username
use_attribute_friendly_name: false # CRITICAL for Azure
check_path: saml_acs
login_path: saml_login
logout:
path: saml_logout
Test Locally
https://your-app.com/saml/login to trigger Azure AD login.{{ dump(app.user) }} (Symfony Twig).EntityID, AssertionConsumerService) using azure_app_id and app_base_url. Avoid hardcoding these./saml/metadata for Azure AD to consume. Use the command to debug:
php bin/console angle:onelogin:azure:saml:metadata
username_attribute (e.g., uid, email, name). Example:
saml:
username_attribute: user_principal_name # Azure AD's UPN
// src/Security/SamlUserProvider.php
public function loadUserBySamlAttribute(array $attributes)
{
$user = new User();
$user->setEmail($attributes['email'][0] ?? null);
$user->setFirstName($attributes['given_name'][0] ?? null);
return $user;
}
debug: true in the config to log SAML messages to var/log/saml.log.error event listener to customize error responses:
# config/packages/security.yaml
firewalls:
main:
saml:
error_path: saml_error
// src/EventListener/SamlErrorListener.php
public function onSamlError(SamlEvent $event)
{
$event->setResponse(new RedirectResponse('/saml/error?msg=' . $event->getError()));
}
angle_one_login_azure_saml:
trust_proxy: true # For Symfony 5.4+
config/routes.yaml:
saml_login:
path: /auth/azure
controller: Angle\OneLoginAzureSamlBundle\Controller\SamlController::login
onelogin/php-saml library’s test utilities to simulate SAML responses:
// tests/Functional/SamlTest.php
use OneLogin\Saml2\Auth;
public function testSamlLogin()
{
$auth = new Auth();
$auth->setSettings($this->getSamlSettings());
$auth->login(); // Simulate SAML response
}
EntityID Mismatch
EntityID in the SP metadata doesn’t match the registered app.php bin/console angle:onelogin:azure:saml:metadata (e.g., https://your-app.com/sp).use_attribute_friendly_name: false
false. If set to true, login fails silently.security.yaml:
saml:
use_attribute_friendly_name: false
Certificate Expiry
azure_x509_cert expires, SAML fails.angle_one_login_azure_saml:
azure_x509_cert: '-----BEGIN CERTIFICATE-----\n...NEW_CERT...\n-----END CERTIFICATE-----'
Private Key Requirements
privateKey must be a valid PEM-encoded RSA key. Invalid keys cause metadata generation to fail.openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in private_key.pem -out public_key.pem
Then add the private key to the config.Base URL Configuration
app_base_url must not include a trailing slash (e.g., https://app.com not https://app.com/).UrlGeneratorInterface.Debug Mode in Production
debug: true in production exposes sensitive SAML logs.angle_one_login_azure_saml:
debug: '%env(bool:SAML_DEBUG)%'
Then set SAML_DEBUG=false in .env.Attribute Name Conflicts
name vs. displayName).$user->setFullName($attributes['name'][0] ?? $attributes['displayName'][0] ?? null);
Check SAML Logs
var/log/saml.log. Enable with:
angle_one_login_azure_saml:
debug: true
Validate Metadata
Test with Postman
/saml/acs with a SAML response (use Azure AD’s test tools to generate one).Symfony Profiler
// config/packages/dev/security.yaml
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
// src/Security/SamlUserProvider.php
class CustomSamlUserProvider extends SamlUserProvider
{
public function loadUserBySaml
How can I help you explore Laravel packages today?