<?php

ob_start();
include __DIR__ . '/../../common.php';
use Gaia\Http\AuthDigest;
use Gaia\Store\KVP;
// instead of a container object you should use a persistent storage option like
// Store\DBA; This just shows proof of concept.
$storage = new KVP();
// instantiate hte digest
$auth = new AuthDigest($realm = 'Restricted Area', $domain = '/');
$username = '******';
$password = '******';
$ttl = 0;
// store forever.
// normally you would pre-load your usernames and passwords into your storage.
// using the auth object to hash the password.
// hashing the password is as simple as doing:
// md5( $username . ':' . $realm . ':' . $password );
// don't need the authdigest object to do it technically.
// but it is more convenient.
// not super encrypted, but it is a 1 way hash and unlikely that a dictionary attack
// will work to be able to reverse a list of passwords from the hashed password.
$storage->set($username, $auth->hashPassword($username, $password), $ttl);
// can also store username and password in clear text.
// only store for 1 hr
$storage->set('bazz', 'quux', $ttl = 3600);
// if not authenticated, send the usual unauthorized header, along with
// a challenge header.
if (!($is_authenticated = $auth->authenticate($storage))) {
    header('HTTP/1.1 401 Unauthorized');
Exemplo n.º 2
0
<?php

include_once __DIR__ . '/../common.php';
use Gaia\Test\Tap;
use Gaia\Store\KVP;
Tap::plan(6);
$c = new KVP();
foreach (array('result_set', 'result_get', 'result_isset', 'result_unset') as $key) {
    ${$key} = array();
}
if (!isset($input) || !is_array($input)) {
    $input = array();
}
foreach ($input as $k => $v) {
    $result_set[$k] = $c->{$k} = $v;
    $result_isset[$k] = isset($c->{$k});
    $result_get[$k] = $c->{$k};
    unset($c->{$k});
    $result_unset[$k] = $c->{$k};
}
Tap::is($input, $result_set, 'set works properly');
Tap::is($input, $result_get, 'get works properly');
Tap::is(array_fill_keys(array_keys($input), TRUE), $result_isset, 'isset works properly');
Tap::is(array_fill_keys(array_keys($input), NULL), $result_unset, 'unset works properly');
Tap::is($c->non_existent, NULL, 'non-existent variables are null');
$c->load($input);
Tap::is($c->get(array_keys($input)), $input, 'multi-get works properly');