/** * Verify the Facebook credentials. * * @throws Kohana_Exception * @param string the service name * @return boolean */ public function verify($service = MMI_API::SERVICE_FACEBOOK) { $access_token = NULL; if (!array_key_exists('fragment', $_GET)) { $this->_convert_fragment_to_parameter(); } else { $fragment = urldecode(Security::xss_clean($_GET['fragment'])); parse_str($fragment, $parms); $access_token = Arr::get($parms, 'access_token'); unset($parms); } // Ensure the access token is set if (empty($access_token)) { MMI_Log::log_error(__METHOD__, __LINE__, 'Access token parameter missing'); throw new Kohana_Exception('Access token parameter missing in :method.', array(':method' => __METHOD__)); } // Load existing data from the database $auth_config = $this->_auth_config; $username = Arr::get($auth_config, 'username'); $model; if (!empty($username)) { $model = Model_MMI_API_Tokens::select_by_service_and_username($service, $username, FALSE); } else { $consumer_key = Arr::get($auth_config, 'api_key'); $model = Model_MMI_API_Tokens::select_by_service_and_consumer_key($service, $consumer_key, FALSE); } $success = FALSE; $previously_verified = FALSE; if ($model->loaded()) { // Check if the credentials were previously verified $previously_verified = $model->verified; $success = $previously_verified; } if (!$previously_verified) { // Create an access token $token = new OAuthToken($access_token, $service . '-' . time()); // Update the token credentials in the database $svc = MMI_API::factory($service); if (isset($token) and $svc->is_valid_token($token)) { $encrypt = Encrypt::instance(); $model->service = $service; $model->consumer_key = 'consumer-' . $service; $model->consumer_secret = $encrypt->encode($service . '-' . time()); $model->token_key = $token->key; $model->token_secret = $encrypt->encode($token->secret); unset($encrypt); $model->verified = 1; $model->verification_code = $service . '-' . time(); $model->username = $username; if (array_key_exists('expires_in', $_GET)) { $model->attributes = array('expires_in' => urldecode(Security::xss_clean($_GET['expires_in']))); } $success = MMI_Jelly::save($model, $errors); if (!$success and $this->_debug) { MMI_Debug::dead($errors); } } } return $success; }
/** * Verify the Flickr credentials. * * @throws Kohana_Exception * @return boolean */ public function verify() { // Set the service $service = $this->_service; if (empty($service)) { MMI_Log::log_error(__METHOD__, __LINE__, 'Service not set'); throw new Kohana_Exception('Service not set in :method.', array(':method' => __METHOD__)); } // Ensure the frob is set $frob = NULL; if (array_key_exists('frob', $_GET)) { $frob = urldecode(Security::xss_clean($_GET['frob'])); } if (empty($frob)) { MMI_Log::log_error(__METHOD__, __LINE__, 'Frob parameter missing'); throw new Kohana_Exception('Frob parameter missing in :method.', array(':method' => __METHOD__)); } // Load existing data from the database $auth_config = $this->_auth_config; $username = Arr::get($auth_config, 'username'); $model; if (!empty($username)) { $model = Model_MMI_API_Tokens::select_by_service_and_username($service, $username, FALSE); } else { $model = Jelly::factory('MMI_API_Tokens'); } $success = FALSE; if ($model->loaded()) { // Check if the credentials were previously verified $previously_verified = $model->verified; if ($previously_verified) { $success = TRUE; } else { // Create a dummy verification code $verification_code = $service . '-' . time(); } // Do database update if (!$previously_verified) { // Get an access token $svc = MMI_API::factory($service); $token = $svc->get_access_token($verification_code, array('token_key' => $frob, 'token_secret' => $service . '-' . time())); // Update the token credentials in the database if (isset($token) and $svc->is_valid_token($token)) { $model->token_key = $token->key; $model->token_secret = Encrypt::instance()->encode($token->secret); $model->verified = 1; $model->verification_code = $verification_code; if (!empty($token->attributes)) { $model->attributes = $token->attributes; } $success = MMI_Jelly::save($model, $errors); if (!$success and $this->_debug) { MMI_Debug::dead($errors); } } } } return $success; }
/** * Ensure the request token has been verified and an access token received. * * @throws Kohana_Exception * @return void */ protected function _check_token() { if (!$this->is_valid_token()) { $service = $this->_service; MMI_Log::log_error(__METHOD__, __LINE__, 'Request token not valid for ' . $service); throw new Kohana_Exception('Request token not valid for :service in :method.', array(':service' => $service, ':method' => __METHOD__)); } }
/** * Create a custom verification instance. * * @throws Kohana_Exception * @param string the name of the service * @return MMI_API_Verify_Custom */ public static function factory($driver) { $class = 'MMI_API_Verify_Custom_' . ucfirst($driver); if (!class_exists($class)) { MMI_Log::log_error(__METHOD__, __LINE__, $class . ' class does not exist'); throw new Kohana_Exception(':class class does not exist in :method.', array(':class' => $class, ':method' => __METHOD__)); } return new $class(); }
/** * After obtaining a new request token, return the authorization URL. * * @throws Kohana_Exception * @param object the token object * @return string */ public function get_auth_redirect($token = NULL) { $redirect = NULL; // Get a new request token if (!isset($token)) { $token = $this->get_request_token(); } if (isset($token) and $this->is_valid_token($token)) { $success = $this->_update_token($token); } else { $service = $this->_service; MMI_Log::log_error(__METHOD__, __LINE__, 'Invalid token for ' . $service); throw new Kohana_Exception('Invalid token for :service in :method.', array(':service' => $service, ':method' => __METHOD__)); } // Get the API key $api_key = $this->_api_key; $this->_ensure_parm('API key', $api_key); // Build the redirect URL $redirect = $this->authenticate_url(); if (empty($redirect)) { $redirect = $this->authorize_url(); } $parms = array('api_key' => $api_key, 'frob' => $this->_token->key, 'perms' => 'delete'); $parms['api_sig'] = $this->_get_signature($parms); return $redirect . '?' . http_build_query($parms); }