CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • AbstractPasswordHasher
  • BaseAuthenticate
  • BaseAuthorize
  • BasicAuthenticate
  • ControllerAuthorize
  • DefaultPasswordHasher
  • DigestAuthenticate
  • FallbackPasswordHasher
  • FormAuthenticate
  • PasswordHasherFactory
  • WeakPasswordHasher
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         2.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Auth;
 16: 
 17: use Cake\Http\Exception\UnauthorizedException;
 18: use Cake\Http\Response;
 19: use Cake\Http\ServerRequest;
 20: 
 21: /**
 22:  * Basic Authentication adapter for AuthComponent.
 23:  *
 24:  * Provides Basic HTTP authentication support for AuthComponent. Basic Auth will
 25:  * authenticate users against the configured userModel and verify the username
 26:  * and passwords match.
 27:  *
 28:  * ### Using Basic auth
 29:  *
 30:  * Load `AuthComponent` in your controller's `initialize()` and add 'Basic' in 'authenticate' key
 31:  * ```
 32:  *  $this->loadComponent('Auth', [
 33:  *      'authenticate' => ['Basic']
 34:  *      'storage' => 'Memory',
 35:  *      'unauthorizedRedirect' => false,
 36:  *  ]);
 37:  * ```
 38:  *
 39:  * You should set `storage` to `Memory` to prevent CakePHP from sending a
 40:  * session cookie to the client.
 41:  *
 42:  * You should set `unauthorizedRedirect` to `false`. This causes `AuthComponent` to
 43:  * throw a `ForbiddenException` exception instead of redirecting to another page.
 44:  *
 45:  * Since HTTP Basic Authentication is stateless you don't need call `setUser()`
 46:  * in your controller. The user credentials will be checked on each request. If
 47:  * valid credentials are not provided, required authentication headers will be sent
 48:  * by this authentication provider which triggers the login dialog in the browser/client.
 49:  *
 50:  * @see https://book.cakephp.org/3.0/en/controllers/components/authentication.html
 51:  */
 52: class BasicAuthenticate extends BaseAuthenticate
 53: {
 54:     /**
 55:      * Authenticate a user using HTTP auth. Will use the configured User model and attempt a
 56:      * login using HTTP auth.
 57:      *
 58:      * @param \Cake\Http\ServerRequest $request The request to authenticate with.
 59:      * @param \Cake\Http\Response $response The response to add headers to.
 60:      * @return array|false Either false on failure, or an array of user data on success.
 61:      */
 62:     public function authenticate(ServerRequest $request, Response $response)
 63:     {
 64:         return $this->getUser($request);
 65:     }
 66: 
 67:     /**
 68:      * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
 69:      *
 70:      * @param \Cake\Http\ServerRequest $request Request object.
 71:      * @return array|false Either false or an array of user information
 72:      */
 73:     public function getUser(ServerRequest $request)
 74:     {
 75:         $username = $request->getEnv('PHP_AUTH_USER');
 76:         $pass = $request->getEnv('PHP_AUTH_PW');
 77: 
 78:         if (!is_string($username) || $username === '' || !is_string($pass) || $pass === '') {
 79:             return false;
 80:         }
 81: 
 82:         return $this->_findUser($username, $pass);
 83:     }
 84: 
 85:     /**
 86:      * Handles an unauthenticated access attempt by sending appropriate login headers
 87:      *
 88:      * @param \Cake\Http\ServerRequest $request A request object.
 89:      * @param \Cake\Http\Response $response A response object.
 90:      * @return void
 91:      * @throws \Cake\Http\Exception\UnauthorizedException
 92:      */
 93:     public function unauthenticated(ServerRequest $request, Response $response)
 94:     {
 95:         $Exception = new UnauthorizedException();
 96:         $Exception->responseHeader($this->loginHeaders($request));
 97:         throw $Exception;
 98:     }
 99: 
100:     /**
101:      * Generate the login headers
102:      *
103:      * @param \Cake\Http\ServerRequest $request Request object.
104:      * @return string[] Headers for logging in.
105:      */
106:     public function loginHeaders(ServerRequest $request)
107:     {
108:         $realm = $this->getConfig('realm') ?: $request->getEnv('SERVER_NAME');
109: 
110:         return [
111:             'WWW-Authenticate' => sprintf('Basic realm="%s"', $realm)
112:         ];
113:     }
114: }
115: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs