PHPアプリケーションにSAMLシングルサインオン(SSO)を迅速に導入する方法について説明します。このガイドでは、php-samlを使用して安全で信頼性の高い認証システムを構築する方法を紹介します。
php-samlとは?
php-samlは、PHPアプリケーション向けの強力なSAMLツールキットです。SAML 2.0プロファイルに対応し、企業レベルのセキュリティ要件を満たすことができます。
主な特徴
- SAML 2.0 WebブラウザSSOプロファイル対応
- メッセージ暗号化や署名検証などのセキュリティ機能
- 主要なPHPフレームワークとの互換性
- OktaやAzure ADなどと簡単に統合可能
技術的特性
認証フロー
- AuthN要求の生成と送信
- SAMLレスポンスの処理と署名検証
- ユーザー属性の抽出と管理
セキュリティメカニズム
- SHA-256/SHA-384/SHA-512による署名検証
- メッセージの暗号化と復号
- 重複攻撃防止
- XML検証とスキーマチェック
セットアップ手順
環境要件
- PHPバージョン5.3.3以上(推奨:PHP 7.3以上)
- 必要な拡張モジュール: php-xml, php-openssl, php-curl
インストール方法
composer require sample/php-saml-toolkit
基本設定
$conf = array(
'strict' => true,
'debug' => false,
'sp' => array(
'entityId' => 'https://your-app.com/sso',
'acsUrl' => 'https://your-app.com/saml/acs',
'x509cert' => '-----BEGIN CERTIFICATE-----...',
'privateKey' => '-----BEGIN PRIVATE KEY-----...'
),
'idp' => array(
'entityId' => 'https://sso.example.com/idp',
'singleSignOnService' => array(
'url' => 'https://sso.example.com/sso',
'binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect'
),
'x509cert' => '-----BEGIN CERTIFICATE-----...'
)
);
実装例
初期化とログイン処理
define("TOOLKIT_PATH", '/path/to/sample-php-saml/');
require_once(TOOLKIT_PATH . 'loader.php');
$authenticator = new Sample_Saml2_Auth($conf);
// ログインページで呼び出す
$authenticator->login();
// リダイレクト先を指定する場合
$authenticator->login('https://your-app.com/dashboard');
SAMLレスポンスの処理
session_start();
$authenticator->processResponse();
$errors = $authenticator->getErrors();
if (empty($errors) && $authenticator->isAuthenticated()) {
$_SESSION['user_data'] = $authenticator->getAttributes();
$_SESSION['name_id'] = $authenticator->getNameId();
header('Location: /dashboard');
exit;
}
シングルサインアウト
// ログアウトボタンで呼び出す $authenticator->logout(); // ログアウト処理を行うエンドポイント $authenticator->processLogout();
プロジェクト構造
- lib/Saml2/: コアSAML2クラス
- endpoints/: ACSやSLO用のエンドポイント
- demo/: 使用例
- tests/: テストケース
セキュリティベストプラクティス
- 厳格モードの有効化
- 定期的な鍵と証明書の更新
- セッション管理の適切な実施