LoginSignup
11
12

More than 3 years have passed since last update.

CakePHP4 の認証処理 cakephp/authentication のメモ

Last updated at Posted at 2020-04-26

CakePHP4 でプラグインとして提供されている cakephp/authentication のメモです。
認証したいだけなんですが、 cakephp/authentication は登場するクラスが多いです。

すべて以下の Cookbook に記載されている内容です。

登場クラス

主要な登場クラスとその概要です。

クラス 概要
AuthenticationMiddleware リクエストごとに認証処理を行う PSR の Middleware
AuthenticationService 認証処理の 一般的な Facade
AuthenticationComponent 認証処理の CakePHP の Component
Identity ログインユーザー情報 (AuthenticationService->getIdentiy(), AuthenticationService->buildIdentiy() で生成される)
Authenticator 認証処理
Identifier ログインユーザー情報の取得処理
Identifier\Resolver ユーザー情報取得処理

こちらはサブ的な登場クラスとその概要です。

クラス 概要
PasswordHasher パスワードハッシュ処理
UrlChecker URLチェック処理

カスタマイズするとき

Auth0やSAML2.0等の独自の認証処理を追加する場合は、独自の Authenticator を作れば良いです。

ログインユーザー情報を DB 以外から取得する場合(例えば、DynamoDB から取得する等)は、 Identifier もしくは Identifier\Resolver を作れば良いです。

Authenticator Identifier の補足

Authenticator Identifier は複数指定することができます。

複数指定した場合、設定した順番に処理がされ、成功すると、後続で指定された Authenticator Identifier は呼び出されません。

通常の認証の場合、Session情報にログインユーザー情報があるか確認し、ない場合は Form情報としてログインユーザー情報が渡っているかを確認します。

そのときは Authenticator を以下のような感じで、2つ設定します。

src/Application.php
        // Load the authenticators, you want session first
        $authenticationService->loadAuthenticator('Authentication.Session');
        // Configure form data check to pick email and password
        $authenticationService->loadAuthenticator('Authentication.Form', [
            'fields' => [
                'username' => 'email',
                'password' => 'password',
            ],
            'loginUrl' => '/users/login',
        ]);
11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12