Beispiel #1
0
 /**
  * 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;
     }
 }
Beispiel #2
0
 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('/');
 }
Beispiel #3
0
	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('');
	}
Beispiel #4
0
 /**
  * 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;
 }
Beispiel #5
0
 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);
 }
Beispiel #6
0
Datei: user.php Projekt: nnc/apis
	/**
	 * 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);
	}
Beispiel #7
0
 /**
  * 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);
 }
Beispiel #8
0
 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);
 }
Beispiel #9
0
 /**
  * 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;
     }
 }
Beispiel #10
0
 /**
  * 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);
 }
Beispiel #11
0
 /**
  * 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')));
 }
Beispiel #12
0
	/**
	 * 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);
	}
Beispiel #13
0
 /**
  * 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);
 }
Beispiel #14
0
	/**
	 * @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);
	}
Beispiel #15
0
Datei: user.php Projekt: nnc/apis
	/**
	 * @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);
	}
Beispiel #16
0
	/**
	 * @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);
	}