23 require_once
CORE_PATH .
'libs/auth/auth_interface.php';
45 private $adapter_object = null;
52 private $active_session =
false;
58 private $expire_time = 3600;
64 private $extra_args = array();
68 private $sleep_time = 0;
74 private static $is_valid = null;
80 private static $active_identity = array();
90 if (isset($extra_args[0])) {
91 $adapter = $extra_args[0];
92 unset($extra_args[0]);
102 public function set_adapter($adapter, $auth = null, $extra_args = array())
104 if (!in_array($adapter, array(
'digest',
'http',
'model',
'kerberos5',
'radius'))) {
105 throw new kumbiaException(
"Adaptador de autenticación '$adapter' no soportado");
108 require_once
CORE_PATH .
"libs/auth/adapters/{$adapter}_auth.php";
109 $adapter_class = $this->adapter .
'Auth';
110 $this->extra_args = $extra_args;
111 $this->adapter_object =
new $adapter_class($auth, $extra_args);
120 return $this->adapter;
130 $result = $this->adapter_object->authenticate();
134 if ($result && $this->active_session) {
135 $user_hash = md5(serialize($this->extra_args));
136 $filename =
APP_PATH .
'temp/cache/' . base64_encode(
'auth');
137 if (file_exists($filename)) {
138 $fp = fopen($filename,
'r');
141 $user = explode(
':', $line);
142 if ($user_hash == $user[0]) {
143 if ($user[1] + $user[2] > time()) {
144 if ($this->sleep_time) {
145 sleep($this->sleep_time);
147 self::$active_identity = array();
148 self::$is_valid =
false;
153 file_put_contents($filename, $user_hash .
':' . time() .
':' . $this->expire_time .
"\n");
158 $fp = fopen($filename,
'a');
159 fputs($fp, $user_hash .
':' . time() .
':' . $this->expire_time .
"\n");
162 file_put_contents($filename, $user_hash .
':' . time() .
':' . $this->expire_time .
"\n");
166 if ($this->sleep_time) {
167 sleep($this->sleep_time);
170 $_SESSION[
'KUMBIA_AUTH_IDENTITY'][
Config::get(
'config.application.namespace_auth')] = $this->adapter_object->get_identity();
171 self::$active_identity = $this->adapter_object->get_identity();
172 $_SESSION[
'KUMBIA_AUTH_VALID'][
Config::get(
'config.application.namespace_auth')] = $result;
173 self::$is_valid = $result;
184 if (!$_SERVER[
'PHP_AUTH_USER']) {
185 header(
'WWW-Authenticate: Basic realm="basic"');
186 header(
'HTTP/1.0 401 Unauthorized');
189 $options = array(
"username" => $_SERVER[
'PHP_AUTH_USER'],
"password" => $_SERVER[
'PHP_AUTH_PW']);
190 $this->adapter_object->set_params($options);
202 return $this->adapter_object->get_identity();
212 $this->active_session = $value;
213 $this->expire_time = $time;
222 $user_hash = md5(serialize($this->extra_args));
223 $filename =
APP_PATH .
'temp/cache/' . base64_encode(
'auth');
224 $lines = file($filename);
225 $lines_out = array();
226 foreach ($lines as $line) {
227 if (substr($line, 0, 32) != $user_hash) {
228 $lines_out[] = $line;
231 file_put_contents($filename, join(
"\n", $lines_out));
241 return $this->adapter_object;
257 $this->sleep_time = $time;
259 $this->sleep_time = 0;
270 if (!is_null(self::$is_valid)) {
271 return self::$is_valid;
273 self::$is_valid = isset($_SESSION[
'KUMBIA_AUTH_VALID'][
Config::get(
'config.application.namespace_auth')]) ? $_SESSION[
'KUMBIA_AUTH_VALID'][
Config::get(
'config.application.namespace_auth')] : null;
274 return self::$is_valid;
285 if (count(self::$active_identity)) {
286 return self::$active_identity;
288 self::$active_identity = $_SESSION[
'KUMBIA_AUTH_IDENTITY'][
Config::get(
'config.application.namespace_auth')];
289 return self::$active_identity;
299 public static function get($var = null)
302 return $_SESSION[
'KUMBIA_AUTH_IDENTITY'][
Config::get(
'config.application.namespace_auth')][$var];
312 self::$is_valid = null;
313 unset($_SESSION[
'KUMBIA_AUTH_VALID'][
Config::get(
'config.application.namespace_auth')]);
314 self::$active_identity = null;
315 unset($_SESSION[
'KUMBIA_AUTH_IDENTITY'][
Config::get(
'config.application.namespace_auth')]);