/** * Sends a JSON-RPC request * @param Tivoka_Request $request A Tivoka request * @return Tivoka_Request if sent as a batch request the BatchRequest object will be returned */ public function send($request) { if (func_num_args() > 1) { $request = func_get_args(); } if (is_array($request)) { $request = Tivoka::createBatch($request); } if (!$request instanceof Tivoka_Request) { throw new Tivoka_Exception('Invalid data type to be sent to server'); } $parse = parse_url($this->target); // preparing connection... $context = stream_context_create(array($parse['scheme'] => array('content' => (string) $request, 'header' => "Content-Type: application/json\r\n" . "Connection: Close\r\n", 'method' => 'POST', 'timeout' => 10.0))); //sending... if ($this->oauth_consumer) { $oauth = new OAuth_Request($this->oauth_consumer, 'POST', $this->target, array(), (string) $request); $response = $oauth->request($context); } else { $response = @file_get_contents($this->target, false, $context); } if ($response === FALSE) { throw new Tivoka_Exception('Connection to "' . $this->target . '" failed', Tivoka::ERR_CONNECTION_FAILED); } $request->setResponse($response); return $request; }
/** * Generate a signed hash of the base string using the consumer and token * as the signing key. * * $sig = $signature->sign($request, $consumer, $token); * * [!!] This method implements [OAuth 1.0 Spec 9.2.1](http://oauth.net/core/1.0/#rfc.section.9.2.1). * * @param Request request * @param Consumer consumer * @param Token token * @return string * @uses Signature::key * @uses Request::base_string */ public function sign(OAuth_Request $request, OAuth_Consumer $consumer, OAuth_Token $token = NULL) { // Get the signing key $key = $this->key($consumer, $token); // Get the base string for the signature $base_string = $request->base_string(); // Sign the base string using the key return base64_encode(hash_hmac('sha1', $base_string, $key, TRUE)); }
public function action_authorize() { if ($this->token and $this->token->token !== Arr::get($_GET, 'oauth_token')) { // Delete the token, it is not valid Cookie::delete($this->cookie); // Send the user back to the beginning Request::instance()->redirect($this->request->uri(array('action' => 'index'))); } // Get the verifier $verifier = Arr::get($_GET, 'oauth_verifier'); // Store the verifier in the token $this->token->verifier($verifier); // Exchange the request token for an access token $this->token = $this->provider->access_token($this->consumer, $this->token); // Store the access token Cookie::set($this->cookie, serialize($this->token)); // At this point, we need to retrieve a unique twitter id for the user. $response = OAuth_Request::factory('resource', 'GET', 'http://api.twitter.com/1/account/verify_credentials.json')->param('oauth_consumer_key', Kohana::config('oauth.twitter.key'))->param('oauth_token', $this->token)->sign(OAuth_Signature::factory('HMAC-SHA1'), $this->consumer, $this->token)->execute(); $response = json_decode($response); $twitter_id = $response->screen_name; $user = ORM::factory('user')->where('username', '=', $twitter_id)->find(); !$user->id and Request::instance()->redirect('/auth/confirm?id=' . $twitter_id); Auth::instance()->force_login($user); Session::instance()->set('notification', 'Succesfully logged in.'); Request::instance()->redirect('/'); }
/** * Verify the login result and do whatever is needed to access the user data from this provider. * @return bool */ public function verify() { // create token $request_token = OAuth_Token::factory('request', array('token' => Session::instance()->get('oauth_token'), 'secret' => Session::instance()->get('oauth_token_secret'))); // Store the verifier in the token $verifier = Arr::get($_REQUEST, 'oauth_verifier'); if (empty($verifier)) { return false; } $request_token->verifier($verifier); // Exchange the request token for an access token $access_token = $this->provider->access_token($this->consumer, $request_token); if ($access_token and $access_token->name === 'access') { $request = OAuth_Request::factory('resource', 'GET', 'https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,email-address)?format=json', array('oauth_consumer_key' => $this->consumer->key, 'oauth_signature_method' => "HMAC-SHA1", 'oauth_token' => $access_token->token)); // Sign the request using only the consumer, no token is available yet $request->sign(new OAuth_Signature_HMAC_SHA1(), $this->consumer, $access_token); // decode and store data $data = json_decode($request->execute(), true); $this->uid = $data['id']; $this->data = $data; return true; } else { return false; } }
public function action_complete() { if ($this->token AND $this->token->token !== Arr::get($_GET, 'oauth_token')) { // Delete the token, it is not valid Cookie::delete($this->cookie); // Send the user back to the beginning $this->request->redirect($this->request->uri(array('action' => 'index'))); } // Get the verifier $verifier = Arr::get($_GET, 'oauth_verifier'); // Store the verifier in the token $this->token->verifier($verifier); // Exchange the request token for an access token $this->token = $this->provider->access_token($this->consumer, $this->token); // Store the access token Cookie::set($this->cookie, serialize($this->token)); // At this point, we need to retrieve a unique twitter id for the user. // http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-account%C2%A0verify_credentials // @todo try/catch? $response = OAuth_Request::factory('resource', 'GET', 'http://api.twitter.com/1/account/verify_credentials.json') ->param('oauth_consumer_key', Kohana::config('oauth.twitter.key')) ->param('oauth_token', $this->token) ->sign(OAuth_Signature::factory('HMAC-SHA1'), $this->consumer, $this->token) ->execute(); $response = json_decode($response); if ( ! $twitter_id = (int) $response->id) exit('error'); // Check whether that id exists in our users table (twitter_id field). $user = ORM::factory('user')->where('twitter_id', '=', $twitter_id)->find(); // If not, store the new twitter_id (as a new user). Also ask for signup info like email? if ( ! $user->loaded()) { // Add user $user->twitter_id = $twitter_id; $user->save(); // Give user the "login" and "user" role $user->add('roles', ORM::factory('role', array('name' => 'login'))); $user->add('roles', ORM::factory('role', array('name' => 'user'))); // @todo postpone give "user" role until after user completes the email field in his profile? } // If yes, log the user in and give him a normal auth session. Auth::instance()->force_login($user); $this->request->redirect(''); }
public function get_user_info(OAuth_Consumer $consumer, OAuth_Token $token) { // Create a new GET request with the required parameters $request = OAuth_Request::forge('resource', 'GET', 'https://api.dropbox.com/1/account/info', array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->access_token)); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); $user = json_decode($request->execute()); // Create a response from the request return array('uid' => $token->uid, 'name' => $user->display_name, 'email' => $user->email, 'location' => $user->country); }
public function get_user_info(OAuth_Consumer $consumer, OAuth_Token $token) { // Create a new GET request with the required parameters $request = OAuth_Request::forge('resource', 'GET', 'http://api.flickr.com/services/rest', array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->access_token, 'nojsoncallback' => 1, 'format' => 'json', 'method' => 'flickr.test.login')); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); $response = json_decode($request->execute(), true); // Create a response from the request return array('uid' => $response['user']['id'], 'name' => $response['user']['username']['_content'], 'nickname' => $response['user']['username']['_content']); }
/** * Revoke a valid token. * * Blogger::factory('user')->revoke_token($consumer, $token); * * @param OAuth_Consumer consumer * @param OAuth_Token token * @return mixed */ public function revoke_token(OAuth_Consumer $consumer, OAuth_Token $token) { // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'GET', $this->url_authsub('AuthSubRevokeToken'), array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token)); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); return $response; }
public function get_user_info(OAuth_Consumer $consumer, OAuth_Token $token) { // Create a new GET request with the required parameters $request = OAuth_Request::forge('resource', 'GET', 'http://api.twitter.com/1.1/users/lookup.json', array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->access_token, 'user_id' => $token->uid)); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); $user = current(json_decode($request->execute())); // Create a response from the request return array('uid' => $token->uid, 'nickname' => $user->screen_name, 'name' => $user->name ? $user->name : $user->screen_name, 'location' => $user->location, 'image' => $user->profile_image_url, 'description' => $user->description, 'urls' => array('Website' => $user->url, 'Twitter' => 'http://twitter.com/' . $user->screen_name)); }
public function get_user_info(OAuth_Consumer $consumer, OAuth_Token $token) { // Create a new GET request with the required parameters $request = OAuth_Request::forge('resource', 'GET', 'http://api.flickr.com/services/rest', array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->access_token, 'nojsoncallback' => 1, 'format' => 'json', 'user_id' => $token->uid, 'method' => 'flickr.people.getInfo')); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); $response = json_decode($request->execute(), true); $user = $response['person']; // Create a response from the request return array('uid' => $user['nsid'], 'name' => isset($user['realname']['_content']) ? $user['realname']['_content'] : $user['username']['_content'], 'nickname' => $user['username']['_content'], 'location' => isset($user['location']['_content']) ? $user['location']['_content'] : NULL, 'image' => $user['iconserver'] ? "http://farm{$user['iconfarm']}.staticflickr.com/{$user['iconserver']}/buddyicons/{$user['nsid']}.jpg" : NULL, 'urls' => array('photos' => $user['photosurl']['_content'], 'profile' => $user['profileurl']['_content'])); }
public function get_user_info(OAuth_Consumer $consumer, OAuth_Token $token) { // Create a new GET request with the required parameters $url = 'https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,member-url-resources,picture-url,location,public-profile-url)'; $request = OAuth_Request::forge('resource', 'GET', $url, array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->access_token)); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); $user = OAuth_Format::factory($request->execute(), 'xml')->to_array(); // Create a response from the request return array('uid' => $user['id'], 'name' => $user['first-name'] . ' ' . $user['last-name'], 'nickname' => end(explode('/', $user['public-profile-url'])), 'description' => $user['headline'], 'location' => isset($user['location']['name']) ? $user['location']['name'] : null, 'urls' => array('Linked In' => $user['public-profile-url'])); }
public function get_user_info(OAuth_Consumer $consumer, OAuth_Token $token) { // Create a new GET request with the required parameters $request = OAuth_Request::forge('resource', 'GET', 'http://api.tumblr.com/v2/user/info', array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->access_token)); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); $response = json_decode($request->execute()); $status = current($response); $response = next($response); $user = $response->user; // Create a response from the request return array('uid' => $user->name, 'name' => $user->name, 'likes' => $user->likes, 'following' => $user->following, 'default_post_format' => $user->default_post_format); }
public function end_session(OAuth_Consumer $consumer, OAuth_Token $token, array $params = NULL) { // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'POST', $this->url('account/end_session'), array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token)); if ($params) { // Load user parameters $request->params($params); } // Sign the request using only the consumer, no token is available yet $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); return $this->parse($response); }
/** * Retrieve list of blogs. * * Blogger::factory('user')->blogs($consumer, $token, $profile_id); * * @param OAuth_Consumer consumer * @param OAuth_Token token * @param string blog ID * @param string profile ID, if set to 'default' the currently authenticated user's profile ID is used * @return mixed */ public function blogs(OAuth_Consumer $consumer, OAuth_Token $token, $profile_id = 'default') { // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'GET', $this->url($profile_id, 'blogs'), array( 'oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token, )); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); return $this->parse($response); }
/** * Returns full contact list of authenticated user * * @param OAuth_Consumer $consumer Consumer object * @param OAuth_Token $token Token object * @param array|null $params Call parameters * @param string $contact_email Leave as default for authenticated user, specify Email otherwise * @return mixed */ public function full(OAuth_Consumer $consumer, OAuth_Token $token, array $params = NULL, $contact_email = 'default') { // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'GET', $this->url("contacts/{$contact_email}/full"), array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token)); // Set format, can be xml or json if ($this->format == 'json') { $params['alt'] = 'json'; } if ($params) { // Load user parameters $request->params($params); } // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); return $this->parse($response); }
public function show(OAuth_Consumer $consumer, OAuth_Token $token = NULL, array $params = NULL) { if (!isset($params['user_id']) and !isset($params['screen_name'])) { throw new Kohana_OAuth_Exception('Required parameter not passed: user_id or screen_name must be provided'); } // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'GET', $this->url('users/show'), array('oauth_consumer_key' => $consumer->key))->required('oauth_token', FALSE); if ($token) { // Include the access token $params['oauth_token'] = $token->token; } // Load user parameters $request->params($params); // Sign the request using only the consumer, no token is available yet $request->sign($this->signature, $consumer); // Create a response from the request $response = $request->execute(); return $this->parse($response); }
/** * Verify the login result and do whatever is needed to access the user data from this provider. * @return bool */ public function verify() { // create token $request_token = OAuth_Token::factory('request', array('token' => Session::instance()->get('oauth_token'), 'secret' => Session::instance()->get('oauth_token_secret'))); // Store the verifier in the token $request_token->verifier($_REQUEST['oauth_verifier']); // Exchange the request token for an access token $access_token = $this->provider->access_token($this->consumer, $request_token); if ($access_token and $access_token->name === 'access') { // @link http://dev.twitter.com/doc/get/account/verify_credentials $request = OAuth_Request::factory('resource', 'GET', 'http://api.twitter.com/1/account/verify_credentials.json', array('oauth_consumer_key' => $this->consumer->key, 'oauth_token' => $access_token->token)); // Sign the request using only the consumer, no token is available yet $request->sign(new OAuth_Signature_HMAC_SHA1(), $this->consumer, $access_token); // decode and store data $data = json_decode($request->execute(), true); $this->uid = $data['id']; $this->data = $data; return true; } else { return false; } }
/** * Return profile of current user * * @param OAuth_Consumer $consumer Consumer object * @param OAuth_Token $token Token object * @param array|null $params Call parameters * @param bool $public_profile Set TRUE to request the public profile * @return mixed */ public function current_user(OAuth_Consumer $consumer, OAuth_Token $token, array $params = NULL, $public_profile = FALSE) { $fields = ''; if (isset($params['fields'])) { $fields = ':(' . implode(',', $params['fields']) . ')'; } // Fields must not be in query parameters unset($params['fields']); // Set key for public profile if requested $public = $public_profile ? ':public' : ''; // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'GET', $this->url("people/~{$public}{$fields}"), array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token)); // Set format, can be xml or json $params['format'] = $this->format; if ($params) { // Load user parameters $request->params($params); } // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); return $this->parse($response); }
public function execute(array $options = NULL) { return new OAuth_Response(parent::execute($options)); }
/** * @link http://dev.twitter.com/doc/get/statuses/destroy/:id */ public function destroy(OAuth_Consumer $consumer, OAuth_Token $token, array $params = NULL) { if ( ! isset($params['id'])) { throw new Kohana_OAuth_Exception('Required parameter not passed: :param', array( ':param' => 'id', )); } // Remove the "id" parameter, it is used in the URL $id = Arr::get($params, 'id'); // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'POST', $this->url("statuses/destroy/{$id}"), array( 'oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token, )); // Load user parameters $request->params($params); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); return $this->parse($response); }
public function execute(array $options = NULL) { return OAuth_Response::factory(parent::execute($options)); }
/** * Exchange the request token for an access token. * * $token = $provider->access_token($consumer, $token); * * @param OAuth_Consumer consumer * @param OAuth_Token_Request token * @param array additional request parameters * @return OAuth_Token_Access */ public function access_token(OAuth_Consumer $consumer, OAuth_Token_Request $token, array $params = NULL) { // Create a new GET request for a request token with the required parameters $request = OAuth_Request::factory('access', 'GET', $this->url_access_token(), array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token, 'oauth_verifier' => $token->verifier)); if ($params) { // Load user parameters $request->params($params); } // Sign the request using only the consumer, no token is available yet $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); // Store this token somewhere useful return OAuth_Token::factory('access', array('token' => $response->param('oauth_token'), 'secret' => $response->param('oauth_token_secret'))); }
/** * Delete a post. * * Blogger::factory('posts')->delete($consumer, $token, $blog_id, $post_id); * * @param OAuth_Consumer consumer * @param OAuth_Token token * @param string blog ID * @param string post ID * @return mixed */ public function delete(OAuth_Consumer $consumer, OAuth_Token $token, $blog_id, $post_id) { // Create a new POST request with the required parameters // Some firewalls do not allow DELETE, so POST is used and X-HTTP-Method-Override: DELETE is set in headers $request = OAuth_Request::factory('resource', 'POST', $this->url($blog_id, "posts/default/{$post_id}"), array( 'oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token, )); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(array( CURLOPT_HTTPHEADER => array( "Content-Type: {$this->format}", "GData-Version: {$this->version}", 'X-HTTP-Method-Override: DELETE', ), )); return $this->parse($response); }
/** * Unfollow a blog. * * Tumblr::factory('blog')->unfollow($consumer, $token); * * @param OAuth_Consumer $consumer * @param OAuth_Token $token * @param array $params * @return mixed * @link http://www.tumblr.com/docs/en/api/v2#user-methods */ public function unfollow(OAuth_Consumer $consumer, OAuth_Token $token, array $params = NULL) { if (!isset($params['url'])) { // Throw exception throw new Kohana_OAuth_Exception('Required parameter not passed: url must be provided'); } // Create a new POST request with the required parameters $request = OAuth_Request::factory('resource', 'POST', $this->url('/user/unfollow'), array('oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token)); if ($params) { // Load user parameters $request->params($params); } // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); return $this->parse($response); }
/** * @link http://dev.twitter.com/doc/get/users/profile_image */ public function profile_image(OAuth_Consumer $consumer, OAuth_Token $token = NULL, array $params = NULL) { if ( ! isset($params['screen_name'])) { throw new Kohana_OAuth_Exception('Required parameter not passed: :param', array( ':param' => 'screen_name', )); } // Get the "screen_name" parameter, it is used in the URL $screen_name = Arr::get($params, 'screen_name'); // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'GET', $this->url("users/profile_image/{$screen_name}"), array( 'oauth_consumer_key' => $consumer->key, )) ->required('oauth_token', FALSE); if ($token) { // Include the access token $params['oauth_token'] = $token->token; } // Load user parameters $request->params($params); // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute(); return $this->parse($response); }
/** * @link http://dev.twitter.com/doc/get/account/update_profile_image */ public function update_profile_image(OAuth_Consumer $consumer, OAuth_Token $token, array $params = NULL) { // Create a new GET request with the required parameters $request = OAuth_Request::factory('resource', 'POST', $this->url('account/update_profile_image'), array( 'oauth_consumer_key' => $consumer->key, 'oauth_token' => $token->token, )) ->required('image', TRUE); // CURL options $options = array(); if (isset($params['image'])) { // Upload the image $request->upload('image', $params['image']); // Do not pass "image" as a normal parameter unset($params['image']); // This will probably take longer time than normal because of uploading $options[CURLOPT_TIMEOUT] = 60; $options[CURLOPT_HTTPHEADER] = array( // Overload the "Expect" header to bypass CURL oddity, see // http://code.google.com/p/twitter-api/issues/detail?id=697 'Expect:', ); } if ($params) { // Load user parameters $request->params($params); } // Sign the request using the consumer and token $request->sign($this->signature, $consumer, $token); // Create a response from the request $response = $request->execute($options); return $this->parse($response); }