Code Monkey home page Code Monkey logo

codeigniter-oauth's Introduction

CodeIgniter OAuth

An implementation of the OAuth protocol with drivers to work with different providers such as Twitter, Google, etc.

This is based on the wonderful Kohana OAuth package but has been adapted to work with a wider range of providers.

Note that this Spark ONLY provides the authorization mechanism. You will need to implement the example code below so you can save this information to make API requests on the users behalf.

Providers

  • Dropbox
  • Flickr
  • Google
  • LinkedIn
  • Tumblr
  • Twitter

Installation

Install with Sparks

$ php tools/spark install -v0.3.1 oauth

Usage Example

This example will need the user to go to a certain URL, which will support multiple providers. I like to set a controller to handle it and either have one single "session" method - or have another method for callbacks if you want to separate out the code even more.

Here you'll see we have the provider passed in as a URI segment of "twitter" which can be used to find config in a database, or in a config multi-dimensional array. If you want to hard code it all then that is just fine too.

Send your user to http://example.com/auth/oauth/twitter where Auth is the name of the controller. This will also be the address of the "Callback URL" which will be required by many OAuth providers.

class Auth extends CI_Controller
{
	public function oauth($provider)
	{
		$this->load->helper('url');
		
		$this->load->spark('oauth/0.3.1');
	
		// Create an consumer from the config
		$consumer = $this->oauth->consumer(array(
			'key' => $config['key'],
			'secret' => $config['secret'],
		));

		// Load the provider
		$provider = $this->oauth->provider($provider);
		
		// Create the URL to return the user to
		$callback = site_url('account/services/callback/'.$provider->name);

		if ( ! $this->input->get_post('oauth_token'))
		{
			// Add the callback URL to the consumer
			$consumer->callback($callback);	

			// Get a request token for the consumer
			$token = $provider->request_token($consumer);

			// Store the token
			$this->session->set_userdata('oauth_token', base64_encode(serialize($token)));

			// Get the URL to the twitter login page
			$url = $provider->authorize($token, array(
				'oauth_callback' => $callback,
			));

			// Send the user off to login
			redirect($url);
		}
		else
		{
			if ($this->session->userdata('oauth_token'))
			{
				// Get the token from storage
				$token = unserialize(base64_decode($this->session->userdata('oauth_token')));
			}

			if ( ! empty($token) AND $token->access_token !== $this->input->get_post('oauth_token'))
			{	
				// Delete the token, it is not valid
				$this->session->unset_userdata('oauth_token');

				// Send the user back to the beginning
				exit('invalid token after coming back to site');
			}

			// Get the verifier
			$verifier = $this->input->get_post('oauth_verifier');

			// Store the verifier in the token
			$token->verifier($verifier);

			// Exchange the request token for an access token
			$token = $provider->access_token($consumer, $token);
		
			// We got the token, let's get some user data
			$user = $provider->get_user_info($consumer, $token);
		
			// Here you should use this information to A) look for a user B) help a new user sign up with existing data.
			// If you store it all in a cookie and redirect to a registration page this is crazy-simple.
			echo "<pre>Tokens: ";
			var_dump($token).PHP_EOL.PHP_EOL;
			
			echo "User Info: ";
			var_dump($user);
		}
	}
}

If all goes well you should see a dump of user data and have $token available. If all does not go well you'll likely have a bunch of errors on your screen.

codeigniter-oauth's People

Contributors

it-can avatar joeauty avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.