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

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