Repository: artdarek/oauth-4-laravel Branch: master Commit: 6c37c4571caa Files: 8 Total size: 17.3 KB Directory structure: gitextract_1qnhzpxp/ ├── .gitignore ├── README.md ├── composer.json └── src/ ├── Artdarek/ │ └── OAuth/ │ ├── Facade/ │ │ └── OAuth.php │ ├── OAuth.php │ └── OAuthServiceProvider.php └── config/ ├── .gitkeep └── config.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ /vendor composer.phar composer.lock .DS_Store ================================================ FILE: README.md ================================================ # OAuth wrapper for Laravel 4 oauth-4-laravel is a simple laravel 4 service provider (wrapper) for [Lusitanian/PHPoAuthLib](https://github.com/Lusitanian/PHPoAuthLib) which provides oAuth support in PHP 5.3+ and is very easy to integrate with any project which requires an oAuth client. --- - [Supported services](#supported-services) - [Installation](#installation) - [Registering the Package](#registering-the-package) - [Configuration](#configuration) - [Usage](#usage) - [Basic usage](#basic-usage) - [More usage examples](#more-usage-examples) ## Supported services The library supports both oAuth 1.x and oAuth 2.0 compliant services. A list of currently implemented services can be found below. More services will be implemented soon. Included service implementations: - OAuth1 - BitBucket - Etsy - FitBit - Flickr - Scoop.it! - Tumblr - Twitter - Xing - Yahoo - OAuth2 - Amazon - BitLy - Box - Dailymotion - Dropbox - Facebook - Foursquare - GitHub - Google - Harvest - Heroku - Instagram - LinkedIn - Mailchimp - Microsoft - PayPal - Pocket - Reddit - RunKeeper - SoundCloud - Vkontakte - Yammer - more to come! To learn more about Lusitanian/PHPoAuthLib go [here](https://github.com/Lusitanian/PHPoAuthLib) ## Installation Use [composer](http://getcomposer.org) to install this package. ``` $ composer require artdarek/oauth-4-laravel:dev-master ``` ### Registering the Package Register the service provider within the ```providers``` array found in ```app/config/app.php```: ```php 'providers' => array( // ... 'Artdarek\OAuth\OAuthServiceProvider' ) ``` Add an alias within the ```aliases``` array found in ```app/config/app.php```: ```php 'aliases' => array( // ... 'OAuth' => 'Artdarek\OAuth\Facade\OAuth', ) ``` ## Configuration There are two ways to configure oauth-4-laravel. You can choose the most convenient way for you. You can use package config file which can be generated through command line by artisan (option 1) or you can simply create a config file called ``oauth-4-laravel.php`` in your ``app\config\`` directory (option 2). #### Option 1 Create configuration file for package using artisan command ``` $ php artisan config:publish artdarek/oauth-4-laravel ``` #### Option 2 Create configuration file manually in config directory ``app/config/oauth-4-laravel.php`` and put there code from below. ```php 'Session', /** * Consumers */ 'consumers' => array( /** * Facebook */ 'Facebook' => array( 'client_id' => '', 'client_secret' => '', 'scope' => array(), ), ) ); ``` ### Credentials Add your credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` or ``app/config/oauth-4-laravel.php`` (depending on which option of configuration you choose) The `Storage` attribute is optional and defaults to `Session`. Other [options](https://github.com/Lusitanian/PHPoAuthLib/tree/master/src/OAuth/Common/Storage). ## Usage ### Basic usage Just follow the steps below and you will be able to get a [service class object](https://github.com/Lusitanian/PHPoAuthLib/tree/master/src/OAuth/OAuth2/Service) with this one rule: ```php $fb = OAuth::consumer('Facebook'); ``` Optionally, add a second parameter with the URL which the service needs to redirect to, otherwise it will redirect to the current URL. ```php $fb = OAuth::consumer('Facebook','http://url.to.redirect.to'); ``` ## Usage examples ### Facebook: Configuration: Add your Facebook credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` ```php 'Facebook' => array( 'client_id' => 'Your Facebook client ID', 'client_secret' => 'Your Facebook Client Secret', 'scope' => array('email','read_friendlists','user_online_presence'), ), ``` In your Controller use the following code: ```php /** * Login user with facebook * * @return void */ public function loginWithFacebook() { // get data from input $code = Input::get( 'code' ); // get fb service $fb = OAuth::consumer( 'Facebook' ); // check if code is valid // if code is provided get user data and sign in if ( !empty( $code ) ) { // This was a callback request from facebook, get the token $token = $fb->requestAccessToken( $code ); // Send a request with it $result = json_decode( $fb->request( '/me' ), true ); $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name']; echo $message. "
"; //Var_dump //display whole array(). dd($result); } // if not ask for permission first else { // get fb authorization $url = $fb->getAuthorizationUri(); // return to facebook login url return Redirect::to( (string)$url ); } } ``` ### Google: Configuration: Add your Google credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` ```php 'Google' => array( 'client_id' => 'Your Google client ID', 'client_secret' => 'Your Google Client Secret', 'scope' => array('userinfo_email', 'userinfo_profile'), ), ``` In your Controller use the following code: ```php public function loginWithGoogle() { // get data from input $code = Input::get( 'code' ); // get google service $googleService = OAuth::consumer( 'Google' ); // check if code is valid // if code is provided get user data and sign in if ( !empty( $code ) ) { // This was a callback request from google, get the token $token = $googleService->requestAccessToken( $code ); // Send a request with it $result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true ); $message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name']; echo $message. "
"; //Var_dump //display whole array(). dd($result); } // if not ask for permission first else { // get googleService authorization $url = $googleService->getAuthorizationUri(); // return to google login url return Redirect::to( (string)$url ); } } ``` ### Twitter: Configuration: Add your Twitter credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` ```php 'Twitter' => array( 'client_id' => 'Your Twitter client ID', 'client_secret' => 'Your Twitter Client Secret', // No scope - oauth1 doesn't need scope ), ``` In your Controller use the following code: ```php public function loginWithTwitter() { // get data from input $token = Input::get( 'oauth_token' ); $verify = Input::get( 'oauth_verifier' ); // get twitter service $tw = OAuth::consumer( 'Twitter' ); // check if code is valid // if code is provided get user data and sign in if ( !empty( $token ) && !empty( $verify ) ) { // This was a callback request from twitter, get the token $token = $tw->requestAccessToken( $token, $verify ); // Send a request with it $result = json_decode( $tw->request( 'account/verify_credentials.json' ), true ); $message = 'Your unique Twitter user id is: ' . $result['id'] . ' and your name is ' . $result['name']; echo $message. "
"; //Var_dump //display whole array(). dd($result); } // if not ask for permission first else { // get request token $reqToken = $tw->requestRequestToken(); // get Authorization Uri sending the request token $url = $tw->getAuthorizationUri(array('oauth_token' => $reqToken->getRequestToken())); // return to twitter login url return Redirect::to( (string)$url ); } } ``` ### Linkedin: Configuration: Add your Linkedin credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` ```php 'Linkedin' => array( 'client_id' => 'Your Linkedin API ID', 'client_secret' => 'Your Linkedin API Secret', ), ``` In your Controller use the following code: ```php public function loginWithLinkedin() { // get data from input $code = Input::get( 'code' ); $linkedinService = OAuth::consumer( 'Linkedin' ); if ( !empty( $code ) ) { // This was a callback request from linkedin, get the token $token = $linkedinService->requestAccessToken( $code ); // Send a request with it. Please note that XML is the default format. $result = json_decode($linkedinService->request('/people/~?format=json'), true); // Show some of the resultant data echo 'Your linkedin first name is ' . $result['firstName'] . ' and your last name is ' . $result['lastName']; //Var_dump //display whole array(). dd($result); }// if not ask for permission first else { // get linkedinService authorization $url = $linkedinService->getAuthorizationUri(array('state'=>'DCEEFWF45453sdffef424')); // return to linkedin login url return Redirect::to( (string)$url ); } } ``` ### Yahoo: Configuration: Add your Yahoo credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` ```php 'Yahoo' => array( 'client_id' => 'Your Yahoo API KEY', 'client_secret' => 'Your Yahoo API Secret', ), ``` In your Controller use the following code: ```php public function loginWithYahoo() { // get data from input $token = Input::get( 'oauth_token' ); $verify = Input::get( 'oauth_verifier' ); // get yahoo service $yh = OAuth::consumer( 'Yahoo' ); // if code is provided get user data and sign in if ( !empty( $token ) && !empty( $verify ) ) { // This was a callback request from yahoo, get the token $token = $yh->requestAccessToken( $token, $verify ); $xid = array($token->getExtraParams()); $result = json_decode( $yh->request( 'https://social.yahooapis.com/v1/user/'.$xid[0]['xoauth_yahoo_guid'].'/profile?format=json' ), true ); dd($result); } // if not ask for permission first else { // get request token $reqToken = $yh->requestRequestToken(); // get Authorization Uri sending the request token $url = $yh->getAuthorizationUri(array('oauth_token' => $reqToken->getRequestToken())); // return to yahoo login url return Redirect::to( (string)$url ); } } ``` ### More usage examples: For examples go [here](https://github.com/Lusitanian/PHPoAuthLib/tree/master/examples) ================================================ FILE: composer.json ================================================ { "name": "artdarek/oauth-4-laravel", "type": "library", "description": "OAuth Service Provider for Laravel 4", "keywords": ["OAuth", "Lusitanian", "laravel", "php"], "homepage": "https://github.com/artdarek/oauth-4-laravel", "license": "MIT", "authors": [ { "name": "Artdarek", "email": "artdarek@gmail.com", "role": "Developer" }, { "name": "Alejandro Escobedo", "email": "aeg0204@gmail.com", "role": "Developer" } ], "require": { "php": ">=5.3", "lusitanian/oauth": "dev-master" }, "require-dev": { "illuminate/support": "~4" }, "require-all": true, "autoload": { "psr-0": { "Artdarek\\OAuth": "src/" } }, "minimum-stability": "dev" } ================================================ FILE: src/Artdarek/OAuth/Facade/OAuth.php ================================================ * @copyright Copyright (c) 2013 * @license http://www.opensource.org/licenses/mit-license.html MIT License */ namespace Artdarek\OAuth\Facade; use Illuminate\Support\Facades\Facade; class OAuth extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'oauth'; } } ================================================ FILE: src/Artdarek/OAuth/OAuth.php ================================================ * @copyright Copyright (c) 2013 * @license http://www.opensource.org/licenses/mit-license.html MIT License */ namespace Artdarek\OAuth; use Illuminate\Support\ServiceProvider; use \Config; use \URL; use \OAuth\ServiceFactory; use \OAuth\Common\Consumer\Credentials; class OAuth { /** * @var ServiceFactory */ private $_serviceFactory; /** * Storege name from config * @var string */ private $_storage_name = 'Session'; /** * Client ID from config * @var string */ private $_client_id; /** * Client secret from config * @var string */ private $_client_secret; /** * Scope from config * @var array */ private $_scope = array(); /** * Constructor * * @param ServiceFactory $serviceFactory - (Dependency injection) If not provided, a ServiceFactory instance will be constructed. */ public function __construct(ServiceFactory $serviceFactory = null) { if (null === $serviceFactory) { // Create the service factory $serviceFactory = new ServiceFactory(); } $this->_serviceFactory = $serviceFactory; } /** * Detect config and set data from it * * @param string $service */ public function setConfig( $service ) { // if config/oauth-4-laravel.php exists use this one if ( Config::get('oauth-4-laravel.consumers') != null ) { $this->_storage_name = Config::get('oauth-4-laravel.storage', 'Session'); $this->_client_id = Config::get("oauth-4-laravel.consumers.$service.client_id"); $this->_client_secret = Config::get("oauth-4-laravel.consumers.$service.client_secret"); $this->_scope = Config::get("oauth-4-laravel.consumers.$service.scope", array() ); // esle try to find config in packages configs } else { $this->_storage_name = Config::get('oauth-4-laravel::storage', 'Session'); $this->_client_id = Config::get("oauth-4-laravel::consumers.$service.client_id"); $this->_client_secret = Config::get("oauth-4-laravel::consumers.$service.client_secret"); $this->_scope = Config::get("oauth-4-laravel::consumers.$service.scope", array() ); } } /** * Create storage instance * * @param string $storageName * @return OAuth\Common\\Storage */ public function createStorageInstance($storageName) { $storageClass = "\\OAuth\\Common\\Storage\\$storageName"; $storage = new $storageClass(); return $storage; } /** * Set the http client object * * @param string $httpClientName * @return void */ public function setHttpClient($httpClientName) { $httpClientClass = "\\OAuth\\Common\\Http\\Client\\$httpClientName"; $this->_serviceFactory->setHttpClient(new $httpClientClass()); } /** * @param string $service * @param string $url * @param array $scope * @return \OAuth\Common\Service\AbstractService */ public function consumer( $service, $url = null, $scope = null ) { // get config $this->setConfig( $service ); // get storage object $storage = $this->createStorageInstance( $this->_storage_name ); // create credentials object $credentials = new Credentials( $this->_client_id, $this->_client_secret, $url ?: URL::current() ); // check if scopes were provided if (is_null($scope)) { // get scope from config (default to empty array) $scope = $this->_scope; } // return the service consumer object return $this->_serviceFactory->createService($service, $credentials, $storage, $scope); } } ================================================ FILE: src/Artdarek/OAuth/OAuthServiceProvider.php ================================================ * @copyright Copyright (c) 2013 * @license http://www.opensource.org/licenses/mit-license.html MIT License */ namespace Artdarek\OAuth; use Illuminate\Support\ServiceProvider; class OAuthServiceProvider extends ServiceProvider { /** * Indicates if loading of the provider is deferred. * * @var bool */ protected $defer = false; /** * Bootstrap the application events. * * @return void */ public function boot() { $this->package('artdarek/oauth-4-laravel'); } /** * Register the service provider. * * @return void */ public function register() { // Register 'oauth' $this->app['oauth'] = $this->app->share(function($app) { // create oAuth instance $oauth = new OAuth(); // return oAuth instance return $oauth; }); } /** * Get the services provided by the provider. * * @return array */ public function provides() { return array(); } } ================================================ FILE: src/config/.gitkeep ================================================ ================================================ FILE: src/config/config.php ================================================ 'Session', /** * Consumers */ 'consumers' => array( /** * Facebook */ 'Facebook' => array( 'client_id' => '', 'client_secret' => '', 'scope' => array(), ), ) );