PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/core/libs/auth/auth.php

https://bitbucket.org/yickson/evento
PHP | 307 lines | 151 code | 18 blank | 138 comment | 28 complexity | 4a850966ba2ace7277ce7e4bc23d9271 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * KumbiaPHP web & app Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://wiki.kumbiaphp.com/Licencia
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@kumbiaphp.com so we can send you a copy immediately.
  14. *
  15. * @category extensions
  16. * @package Auth
  17. * @copyright Copyright (c) 2005 - 2017 Kumbia Team (http://www.kumbiaphp.com)
  18. * @license http://wiki.kumbiaphp.com/Licencia New BSD License
  19. */
  20. /**
  21. * @see AuthInterface
  22. */
  23. require_once __DIR__ .'/auth_interface.php';
  24. // Evita problemas al actualizar de la beta2
  25. if (session_status() !== PHP_SESSION_ACTIVE) {session_start();}
  26. /**
  27. * Esta clase permite autenticar usuarios
  28. *
  29. * @category extensions
  30. * @package Auth
  31. * @deprecated 0.9 use KumbiaAuth
  32. */
  33. class Auth {
  34. /**
  35. * Nombre del adaptador usado para autenticar
  36. *
  37. * @var string
  38. */
  39. private $adapter;
  40. /**
  41. * Objeto Adaptador actual
  42. *
  43. * @var mixed
  44. */
  45. private $adapter_object = null;
  46. /**
  47. * Indica si un usuario debe loguearse solo una vez en el sistema desde
  48. * cualquier parte
  49. *
  50. * @var boolean
  51. */
  52. private $active_session = false;
  53. /**
  54. * Tiempo en que expirara la sesion en caso de que no se termine con destroy_active_session
  55. *
  56. * @var integer
  57. */
  58. private $expire_time = 3600;
  59. /**
  60. * Argumentos extra enviados al Adaptador
  61. *
  62. * @var array
  63. */
  64. private $extra_args = array();
  65. /**
  66. * Tiempo que duerme la aplicacion cuando falla la autenticacion
  67. */
  68. private $sleep_time = 0;
  69. /**
  70. * Indica si el ultimo llamado a authenticate tuvo exito o no (persistente en sesion)
  71. *
  72. * @var boolean|null
  73. */
  74. private static $is_valid = null;
  75. /**
  76. * Ultima identidad obtenida por Authenticate (persistente en sesion)
  77. *
  78. * @var array
  79. */
  80. private static $active_identity = array();
  81. /**
  82. * Constructor del Autenticador
  83. *
  84. *
  85. */
  86. public function __construct() {
  87. $extra_args = Util::getParams(func_get_args());
  88. if (isset($extra_args[0])) {
  89. $adapter = $extra_args[0];
  90. unset($extra_args[0]);
  91. } else {
  92. $adapter = 'model';
  93. }
  94. $this->set_adapter($adapter, $this, $extra_args);
  95. }
  96. /**
  97. * @param Auth $auth
  98. */
  99. public function set_adapter($adapter, $auth = '', $extra_args = array()) {
  100. if (!in_array($adapter, array('digest', 'http', 'model', 'kerberos5', 'radius'))) {
  101. throw new kumbiaException("Adaptador de autenticación '$adapter' no soportado");
  102. }
  103. $this->adapter = Util::camelcase($adapter);
  104. require_once __DIR__ ."/adapters/{$adapter}_auth.php";
  105. $adapter_class = $this->adapter.'Auth';
  106. $this->extra_args = $extra_args;
  107. $this->adapter_object = new $adapter_class($auth, $extra_args);
  108. }
  109. /**
  110. * Obtiene el nombre del adaptador actual
  111. * @return string
  112. */
  113. public function get_adapter_name() {
  114. return $this->adapter;
  115. }
  116. /**
  117. * Realiza el proceso de autenticación
  118. *
  119. * @return array|bool
  120. */
  121. public function authenticate() {
  122. $result = $this->adapter_object->authenticate();
  123. /**
  124. * Si es una sesion activa maneja un archivo persistente para control
  125. */
  126. if ($result && $this->active_session) {
  127. $user_hash = md5(serialize($this->extra_args));
  128. $filename = APP_PATH.'temp/cache/'.base64_encode('auth');
  129. if (file_exists($filename)) {
  130. $fp = fopen($filename, 'r');
  131. while (!feof($fp)) {
  132. $line = fgets($fp);
  133. $user = explode(':', $line);
  134. if ($user_hash == $user[0]) {
  135. if ($user[1]+$user[2] > time()) {
  136. if ($this->sleep_time) {
  137. sleep($this->sleep_time);
  138. }
  139. self::$active_identity = array();
  140. self::$is_valid = false;
  141. return false;
  142. } else {
  143. fclose($fp);
  144. $this->destroy_active_session();
  145. file_put_contents($filename, $user_hash.':'.time().':'.$this->expire_time."\n");
  146. }
  147. }
  148. }
  149. fclose($fp);
  150. $fp = fopen($filename, 'a');
  151. fputs($fp, $user_hash.':'.time().':'.$this->expire_time."\n");
  152. fclose($fp);
  153. } else {
  154. file_put_contents($filename, $user_hash.':'.time().':'.$this->expire_time."\n");
  155. }
  156. }
  157. if (!$result) {
  158. if ($this->sleep_time) {
  159. sleep($this->sleep_time);
  160. }
  161. }
  162. $_SESSION['KUMBIA_AUTH_IDENTITY'][Config::get('config.application.namespace_auth')] = $this->adapter_object->get_identity();
  163. self::$active_identity = $this->adapter_object->get_identity();
  164. $_SESSION['KUMBIA_AUTH_VALID'][Config::get('config.application.namespace_auth')] = $result;
  165. self::$is_valid = $result;
  166. return $result;
  167. }
  168. /**
  169. * Realiza el proceso de autenticaci&oacute;n usando HTTP
  170. *
  171. * @return array
  172. */
  173. public function authenticate_with_http() {
  174. if (!$_SERVER['PHP_AUTH_USER']) {
  175. header('WWW-Authenticate: Basic realm="basic"');
  176. header('HTTP/1.0 401 Unauthorized');
  177. return false;
  178. } else {
  179. $options = array("username" => $_SERVER['PHP_AUTH_USER'], "password" => $_SERVER['PHP_AUTH_PW']);
  180. $this->adapter_object->set_params($options);
  181. return $this->authenticate();
  182. }
  183. }
  184. /**
  185. * Devuelve la identidad encontrada en caso de exito
  186. *
  187. * @return array
  188. */
  189. public function get_identity() {
  190. return $this->adapter_object->get_identity();
  191. }
  192. /**
  193. * Permite controlar que usuario no se loguee mas de una vez en el sistema desde cualquier parte
  194. *
  195. * @bool string $value
  196. */
  197. public function set_active_session($value, $time = 3600) {
  198. $this->active_session = $value;
  199. $this->expire_time = $time;
  200. }
  201. /**
  202. * Destruir sesion activa del usuario autenticado
  203. *
  204. */
  205. public function destroy_active_session() {
  206. $user_hash = md5(serialize($this->extra_args));
  207. $filename = APP_PATH.'temp/cache/'.base64_encode('auth');
  208. $lines = file($filename);
  209. $lines_out = array();
  210. foreach ($lines as $line) {
  211. if (substr($line, 0, 32) != $user_hash) {
  212. $lines_out[] = $line;
  213. }
  214. }
  215. file_put_contents($filename, join("\n", $lines_out));
  216. }
  217. /**
  218. * Devuelve la instancia del adaptador
  219. *
  220. * @return string
  221. */
  222. public function get_adapter_instance() {
  223. return $this->adapter_object;
  224. }
  225. /**
  226. * Determinar si debe dormir la aplicacion cuando falle la autenticacion y cuanto tiempo en segundos
  227. *
  228. * @param boolean $value
  229. * @param integer $time
  230. */
  231. public function sleep_on_fail($value, $time = 2) {
  232. $time = (int) $time;
  233. if ($time < 0) {
  234. $time = 0;
  235. }
  236. if ($value) {
  237. $this->sleep_time = $time;
  238. } else {
  239. $this->sleep_time = 0;
  240. }
  241. }
  242. /**
  243. * Devuelve el resultado del ultimo llamado a authenticate desde el ultimo objeto Auth instanciado
  244. *
  245. * @return boolean
  246. */
  247. static public function is_valid() {
  248. if (!is_null(self::$is_valid)) {
  249. return self::$is_valid;
  250. } else {
  251. 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;
  252. return self::$is_valid;
  253. }
  254. }
  255. /**
  256. * Devuelve el resultado de la ultima identidad obtenida en authenticate desde el ultimo objeto Auth instanciado
  257. *
  258. * @return array
  259. */
  260. static public function get_active_identity() {
  261. if (count(self::$active_identity)) {
  262. return self::$active_identity;
  263. } else {
  264. self::$active_identity = $_SESSION['KUMBIA_AUTH_IDENTITY'][Config::get('config.application.namespace_auth')];
  265. return self::$active_identity;
  266. }
  267. }
  268. /**
  269. * Obtiene un valor de la identidad actual
  270. *
  271. * @param string $var
  272. * @return string
  273. */
  274. public static function get($var = '') {
  275. if ($var) {
  276. return $_SESSION['KUMBIA_AUTH_IDENTITY'][Config::get('config.application.namespace_auth')][$var];
  277. }
  278. }
  279. /**
  280. * Anula la identidad actual
  281. *
  282. */
  283. static public function destroy_identity() {
  284. self::$is_valid = null;
  285. unset($_SESSION['KUMBIA_AUTH_VALID'][Config::get('config.application.namespace_auth')]);
  286. self::$active_identity = array();
  287. unset($_SESSION['KUMBIA_AUTH_IDENTITY'][Config::get('config.application.namespace_auth')]);
  288. }
  289. }