/** * Auth instance constructor * * @param string $name * @param array $config * @return void */ public function __construct($name, $config = null) { if (is_null($config)) { $config = \CCConfig::create('auth')->get($name); // check for an alias. If you set a string // in your config file we use the config // with the passed key. if (is_string($config)) { $config = \CCConfig::create('auth')->get($config); } } if (!is_array($config)) { throw new Exception("Auth\\Handler::create - Invalid auth handler (" . $name . ")."); } // also don't forget to set the name manager name becaue we need him later. $this->name = $name; // assign defaults and create the configuration object $this->config = \CCDataObject::assign(\CCArr::merge(array('session_manager' => null, 'session_key' => 'user_id', 'user_key' => 'id', 'user_model' => "\\Auth\\User", 'identifiers' => array('email'), 'logins' => array('handler' => null, 'table' => 'auth_logins'), 'restore' => array('id_cookie' => 'ccauth-restore-id', 'token_cookie' => 'ccauth-restore-token', 'lifetime' => \CCDate::months(1))), $config)); // set the session handler $this->session = \CCSession::manager($this->config->session_manager); $user_model = $this->config->user_model; // set a empty default user object to avoid // on a non object errors $this->user = new $user_model(); // do we already have a user id means are we // logged in? if (!is_null($session_key = $this->session_user_id())) { if ($user = $user_model::find($this->config->user_key, $session_key)) { $this->user = $user; return $this->authenticated = true; } } else { $restore_id_cookie = $this->config->get('restore.id_cookie'); $restore_token_cookie = $this->config->get('restore.token_cookie'); if (CCCookie::has($restore_id_cookie) && CCCookie::has($restore_token_cookie)) { // get the restore cookies $restore_id = CCCookie::get($restore_id_cookie); $restore_token = CCCookie::get($restore_token_cookie); // get the restore login $login = $this->select_logins()->where('restore_id', $restore_id)->where('restore_token', $restore_token)->limit(1); // if no login found kill the cookies and return if (!($login = $login->run())) { $this->kill_restore(); return $this->authenticated = false; } // Invalid user? kill the cookies and return if (!($user = $user_model::find($this->config->user_key, $restore_id))) { $this->kill_restore(); return $this->authenticated = false; } // validate the restore key if invalid // once again kill the cookies and return if ($login->restore_token != $this->restore_key($user)) { $this->kill_restore(); return $this->authenticated = false; } // If everything is fine sign the user in and // update the restore keys $this->sign_in($user, true); return $this->authenticated = true; } } return $this->authenticated = false; }
/** * Garbage collection, delete all outdated sessions * * @return void */ public function gc() { $lifetime = \CCArr::get('lifetime', $this->_config, \CCDate::minutes(5)); if ($lifetime < ($min_lifetime = \CCArr::get('min_lifetime', $this->_config, \CCDate::minutes(5)))) { $lifetime = $min_lifetime; } $this->_driver->gc($lifetime); }
<?php /* *--------------------------------------------------------------- * Auth configuration *--------------------------------------------------------------- */ return array('main' => array('session_manager' => null, 'session_key' => 'user_id', 'user_key' => 'id', 'user_model' => "\\Auth\\User", 'identifiers' => array('email'), 'logins' => array('handler' => null, 'table' => 'auth_logins'), 'restore' => array('id_cookie' => 'ccauth-restore-id', 'token_cookie' => 'ccauth-restore-token', 'lifetime' => \CCDate::months(1))));
<?php /* *--------------------------------------------------------------- * Session configuration *--------------------------------------------------------------- */ return array('main' => array('driver' => 'json', 'lifetime' => 0, 'min_lifetime' => CCDate::minutes(5), 'gc' => array('enabled' => true, 'factor' => 25)));