CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • BasicAuthentication
  • DigestAuthentication
  • HttpResponse
  • HttpSocket
  • HttpSocketResponse
  1: <?php
  2: /**
  3:  * Digest authentication
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * For full copyright and license information, please see the LICENSE.txt
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @package       Cake.Network.Http
 17:  * @since         CakePHP(tm) v 2.0.0
 18:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 19:  */
 20: 
 21: /**
 22:  * Digest authentication
 23:  *
 24:  * @package       Cake.Network.Http
 25:  */
 26: class DigestAuthentication {
 27: 
 28: /**
 29:  * Authentication
 30:  *
 31:  * @param HttpSocket $http
 32:  * @param array $authInfo
 33:  * @return void
 34:  * @link http://www.ietf.org/rfc/rfc2617.txt
 35:  */
 36:     public static function authentication(HttpSocket $http, &$authInfo) {
 37:         if (isset($authInfo['user'], $authInfo['pass'])) {
 38:             if (!isset($authInfo['realm']) && !self::_getServerInformation($http, $authInfo)) {
 39:                 return;
 40:             }
 41:             $http->request['header']['Authorization'] = self::_generateHeader($http, $authInfo);
 42:         }
 43:     }
 44: 
 45: /**
 46:  * Retrieve information about the authentication
 47:  *
 48:  * @param HttpSocket $http
 49:  * @param array $authInfo
 50:  * @return boolean
 51:  */
 52:     protected static function _getServerInformation(HttpSocket $http, &$authInfo) {
 53:         $originalRequest = $http->request;
 54:         $http->configAuth(false);
 55:         $http->request($http->request);
 56:         $http->request = $originalRequest;
 57:         $http->configAuth('Digest', $authInfo);
 58: 
 59:         if (empty($http->response['header']['WWW-Authenticate'])) {
 60:             return false;
 61:         }
 62:         preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $http->response['header']['WWW-Authenticate'], $matches, PREG_SET_ORDER);
 63:         foreach ($matches as $match) {
 64:             $authInfo[$match[1]] = $match[2];
 65:         }
 66:         if (!empty($authInfo['qop']) && empty($authInfo['nc'])) {
 67:             $authInfo['nc'] = 1;
 68:         }
 69:         return true;
 70:     }
 71: 
 72: /**
 73:  * Generate the header Authorization
 74:  *
 75:  * @param HttpSocket $http
 76:  * @param array $authInfo
 77:  * @return string
 78:  */
 79:     protected static function _generateHeader(HttpSocket $http, &$authInfo) {
 80:         $a1 = md5($authInfo['user'] . ':' . $authInfo['realm'] . ':' . $authInfo['pass']);
 81:         $a2 = md5($http->request['method'] . ':' . $http->request['uri']['path']);
 82: 
 83:         if (empty($authInfo['qop'])) {
 84:             $response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $a2);
 85:         } else {
 86:             $authInfo['cnonce'] = uniqid();
 87:             $nc = sprintf('%08x', $authInfo['nc']++);
 88:             $response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $nc . ':' . $authInfo['cnonce'] . ':auth:' . $a2);
 89:         }
 90: 
 91:         $authHeader = 'Digest ';
 92:         $authHeader .= 'username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $authInfo['user']) . '", ';
 93:         $authHeader .= 'realm="' . $authInfo['realm'] . '", ';
 94:         $authHeader .= 'nonce="' . $authInfo['nonce'] . '", ';
 95:         $authHeader .= 'uri="' . $http->request['uri']['path'] . '", ';
 96:         $authHeader .= 'response="' . $response . '"';
 97:         if (!empty($authInfo['opaque'])) {
 98:             $authHeader .= ', opaque="' . $authInfo['opaque'] . '"';
 99:         }
100:         if (!empty($authInfo['qop'])) {
101:             $authHeader .= ', qop="auth", nc=' . $nc . ', cnonce="' . $authInfo['cnonce'] . '"';
102:         }
103:         return $authHeader;
104:     }
105: 
106: }
107: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs