lithium\net\http\Auth::header PHP Method

header() public static method

Returns the proper header string. Accepts the data from the encode method.
public static header ( array $data ) : string
$data array
return string
    public static function header($data)
    {
        if (empty($data['response'])) {
            return null;
        }
        if (isset($data['nonce'])) {
            $defaults = array('realm' => 'app', 'method' => 'GET', 'uri' => '/', 'username' => null, 'qop' => 'auth', 'nonce' => null, 'opaque' => null, 'cnonce' => md5(time()), 'nc' => static::$nc);
            $data += $defaults;
            $auth = "username=\"{$data['username']}\", response=\"{$data['response']}\", ";
            $auth .= "uri=\"{$data['uri']}\", realm=\"{$data['realm']}\", ";
            $auth .= "qop=\"{$data['qop']}\", nc={$data['nc']}, cnonce=\"{$data['cnonce']}\", ";
            $auth .= "nonce=\"{$data['nonce']}\", opaque=\"{$data['opaque']}\"";
            return "Digest " . $auth;
        }
        return "Basic " . $data['response'];
    }

Usage Example

Example #1
0
 public function testDigestHeader()
 {
     $username = '******';
     $password = '******';
     $nc = '00000001';
     $cnonce = md5(time());
     $user = md5("gwoo:app:li3");
     $nonce = "4bca0fbca7bd0:{$nc}:{$cnonce}:auth";
     $req = md5("GET:/http_auth");
     $hash = md5("{$user}:{$nonce}:{$req}");
     $data = array('realm' => 'app', 'method' => 'GET', 'uri' => '/http_auth', 'qop' => 'auth', 'nonce' => '4bca0fbca7bd0', 'opaque' => 'd3fb67a7aa4d887ec4bf83040a820a46');
     $data = Auth::encode($username, $password, $data);
     $header = Auth::header($data);
     $this->assertPattern('/Digest/', $header);
     preg_match('/response="(.*?)"/', $header, $matches);
     list($match, $response) = $matches;
     $expected = $hash;
     $result = $response;
     $this->assertEqual($expected, $result);
 }