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.6 API

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