├── .gitignore ├── README.md ├── composer.json └── src ├── Artdarek └── OAuth │ ├── Facade │ └── OAuth.php │ ├── OAuth.php │ └── OAuthServiceProvider.php └── config ├── .gitkeep └── config.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OAuth wrapper for Laravel 4 2 | 3 | oauth-4-laravel is a simple laravel 4 service provider (wrapper) for [Lusitanian/PHPoAuthLib](https://github.com/Lusitanian/PHPoAuthLib) 4 | which provides oAuth support in PHP 5.3+ and is very easy to integrate with any project which requires an oAuth client. 5 | 6 | --- 7 | 8 | - [Supported services](#supported-services) 9 | - [Installation](#installation) 10 | - [Registering the Package](#registering-the-package) 11 | - [Configuration](#configuration) 12 | - [Usage](#usage) 13 | - [Basic usage](#basic-usage) 14 | - [More usage examples](#more-usage-examples) 15 | 16 | ## Supported services 17 | 18 | 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. 19 | 20 | Included service implementations: 21 | 22 | - OAuth1 23 | - BitBucket 24 | - Etsy 25 | - FitBit 26 | - Flickr 27 | - Scoop.it! 28 | - Tumblr 29 | - Twitter 30 | - Xing 31 | - Yahoo 32 | - OAuth2 33 | - Amazon 34 | - BitLy 35 | - Box 36 | - Dailymotion 37 | - Dropbox 38 | - Facebook 39 | - Foursquare 40 | - GitHub 41 | - Google 42 | - Harvest 43 | - Heroku 44 | - Instagram 45 | - LinkedIn 46 | - Mailchimp 47 | - Microsoft 48 | - PayPal 49 | - Pocket 50 | - Reddit 51 | - RunKeeper 52 | - SoundCloud 53 | - Vkontakte 54 | - Yammer 55 | - more to come! 56 | 57 | To learn more about Lusitanian/PHPoAuthLib go [here](https://github.com/Lusitanian/PHPoAuthLib) 58 | 59 | ## Installation 60 | 61 | Use [composer](http://getcomposer.org) to install this package. 62 | 63 | ``` 64 | $ composer require artdarek/oauth-4-laravel:dev-master 65 | ``` 66 | 67 | ### Registering the Package 68 | 69 | Register the service provider within the ```providers``` array found in ```app/config/app.php```: 70 | 71 | ```php 72 | 'providers' => array( 73 | // ... 74 | 75 | 'Artdarek\OAuth\OAuthServiceProvider' 76 | ) 77 | ``` 78 | 79 | Add an alias within the ```aliases``` array found in ```app/config/app.php```: 80 | 81 | 82 | ```php 83 | 'aliases' => array( 84 | // ... 85 | 86 | 'OAuth' => 'Artdarek\OAuth\Facade\OAuth', 87 | ) 88 | ``` 89 | 90 | ## Configuration 91 | 92 | There are two ways to configure oauth-4-laravel. 93 | You can choose the most convenient way for you. 94 | You can use package config file which can be 95 | generated through command line by artisan (option 1) or 96 | you can simply create a config file called ``oauth-4-laravel.php`` in 97 | your ``app\config\`` directory (option 2). 98 | 99 | #### Option 1 100 | 101 | Create configuration file for package using artisan command 102 | 103 | ``` 104 | $ php artisan config:publish artdarek/oauth-4-laravel 105 | ``` 106 | 107 | #### Option 2 108 | 109 | Create configuration file manually in config directory ``app/config/oauth-4-laravel.php`` and put there code from below. 110 | 111 | ```php 112 | 'Session', 125 | 126 | /** 127 | * Consumers 128 | */ 129 | 'consumers' => array( 130 | 131 | /** 132 | * Facebook 133 | */ 134 | 'Facebook' => array( 135 | 'client_id' => '', 136 | 'client_secret' => '', 137 | 'scope' => array(), 138 | ), 139 | 140 | ) 141 | 142 | ); 143 | ``` 144 | 145 | ### Credentials 146 | 147 | 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) 148 | 149 | 150 | The `Storage` attribute is optional and defaults to `Session`. 151 | Other [options](https://github.com/Lusitanian/PHPoAuthLib/tree/master/src/OAuth/Common/Storage). 152 | 153 | ## Usage 154 | 155 | ### Basic usage 156 | 157 | 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: 158 | 159 | ```php 160 | $fb = OAuth::consumer('Facebook'); 161 | ``` 162 | 163 | Optionally, add a second parameter with the URL which the service needs to redirect to, otherwise it will redirect to the current URL. 164 | 165 | ```php 166 | $fb = OAuth::consumer('Facebook','http://url.to.redirect.to'); 167 | ``` 168 | 169 | ## Usage examples 170 | 171 | ### Facebook: 172 | 173 | Configuration: 174 | Add your Facebook credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` 175 | 176 | ```php 177 | 'Facebook' => array( 178 | 'client_id' => 'Your Facebook client ID', 179 | 'client_secret' => 'Your Facebook Client Secret', 180 | 'scope' => array('email','read_friendlists','user_online_presence'), 181 | ), 182 | ``` 183 | In your Controller use the following code: 184 | 185 | ```php 186 | /** 187 | * Login user with facebook 188 | * 189 | * @return void 190 | */ 191 | 192 | public function loginWithFacebook() { 193 | 194 | // get data from input 195 | $code = Input::get( 'code' ); 196 | 197 | // get fb service 198 | $fb = OAuth::consumer( 'Facebook' ); 199 | 200 | // check if code is valid 201 | 202 | // if code is provided get user data and sign in 203 | if ( !empty( $code ) ) { 204 | 205 | // This was a callback request from facebook, get the token 206 | $token = $fb->requestAccessToken( $code ); 207 | 208 | // Send a request with it 209 | $result = json_decode( $fb->request( '/me' ), true ); 210 | 211 | $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name']; 212 | echo $message. "
"; 213 | 214 | //Var_dump 215 | //display whole array(). 216 | dd($result); 217 | 218 | } 219 | // if not ask for permission first 220 | else { 221 | // get fb authorization 222 | $url = $fb->getAuthorizationUri(); 223 | 224 | // return to facebook login url 225 | return Redirect::to( (string)$url ); 226 | } 227 | 228 | } 229 | ``` 230 | ### Google: 231 | 232 | Configuration: 233 | Add your Google credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` 234 | 235 | ```php 236 | 'Google' => array( 237 | 'client_id' => 'Your Google client ID', 238 | 'client_secret' => 'Your Google Client Secret', 239 | 'scope' => array('userinfo_email', 'userinfo_profile'), 240 | ), 241 | ``` 242 | In your Controller use the following code: 243 | 244 | ```php 245 | public function loginWithGoogle() { 246 | 247 | // get data from input 248 | $code = Input::get( 'code' ); 249 | 250 | // get google service 251 | $googleService = OAuth::consumer( 'Google' ); 252 | 253 | // check if code is valid 254 | 255 | // if code is provided get user data and sign in 256 | if ( !empty( $code ) ) { 257 | 258 | // This was a callback request from google, get the token 259 | $token = $googleService->requestAccessToken( $code ); 260 | 261 | // Send a request with it 262 | $result = json_decode( $googleService->request( 'https://www.googleapis.com/oauth2/v1/userinfo' ), true ); 263 | 264 | $message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name']; 265 | echo $message. "
"; 266 | 267 | //Var_dump 268 | //display whole array(). 269 | dd($result); 270 | 271 | } 272 | // if not ask for permission first 273 | else { 274 | // get googleService authorization 275 | $url = $googleService->getAuthorizationUri(); 276 | 277 | // return to google login url 278 | return Redirect::to( (string)$url ); 279 | } 280 | } 281 | ``` 282 | 283 | 284 | ### Twitter: 285 | 286 | Configuration: 287 | Add your Twitter credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` 288 | 289 | ```php 290 | 'Twitter' => array( 291 | 'client_id' => 'Your Twitter client ID', 292 | 'client_secret' => 'Your Twitter Client Secret', 293 | // No scope - oauth1 doesn't need scope 294 | ), 295 | ``` 296 | In your Controller use the following code: 297 | 298 | ```php 299 | public function loginWithTwitter() { 300 | 301 | // get data from input 302 | $token = Input::get( 'oauth_token' ); 303 | $verify = Input::get( 'oauth_verifier' ); 304 | 305 | // get twitter service 306 | $tw = OAuth::consumer( 'Twitter' ); 307 | 308 | // check if code is valid 309 | 310 | // if code is provided get user data and sign in 311 | if ( !empty( $token ) && !empty( $verify ) ) { 312 | 313 | // This was a callback request from twitter, get the token 314 | $token = $tw->requestAccessToken( $token, $verify ); 315 | 316 | // Send a request with it 317 | $result = json_decode( $tw->request( 'account/verify_credentials.json' ), true ); 318 | 319 | $message = 'Your unique Twitter user id is: ' . $result['id'] . ' and your name is ' . $result['name']; 320 | echo $message. "
"; 321 | 322 | //Var_dump 323 | //display whole array(). 324 | dd($result); 325 | 326 | } 327 | // if not ask for permission first 328 | else { 329 | // get request token 330 | $reqToken = $tw->requestRequestToken(); 331 | 332 | // get Authorization Uri sending the request token 333 | $url = $tw->getAuthorizationUri(array('oauth_token' => $reqToken->getRequestToken())); 334 | 335 | // return to twitter login url 336 | return Redirect::to( (string)$url ); 337 | } 338 | } 339 | ``` 340 | 341 | 342 | 343 | ### Linkedin: 344 | 345 | Configuration: 346 | Add your Linkedin credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` 347 | 348 | ```php 349 | 'Linkedin' => array( 350 | 'client_id' => 'Your Linkedin API ID', 351 | 'client_secret' => 'Your Linkedin API Secret', 352 | ), 353 | ``` 354 | In your Controller use the following code: 355 | 356 | ```php 357 | 358 | public function loginWithLinkedin() { 359 | 360 | // get data from input 361 | $code = Input::get( 'code' ); 362 | 363 | $linkedinService = OAuth::consumer( 'Linkedin' ); 364 | 365 | 366 | if ( !empty( $code ) ) { 367 | 368 | // This was a callback request from linkedin, get the token 369 | $token = $linkedinService->requestAccessToken( $code ); 370 | // Send a request with it. Please note that XML is the default format. 371 | $result = json_decode($linkedinService->request('/people/~?format=json'), true); 372 | 373 | // Show some of the resultant data 374 | echo 'Your linkedin first name is ' . $result['firstName'] . ' and your last name is ' . $result['lastName']; 375 | 376 | 377 | //Var_dump 378 | //display whole array(). 379 | dd($result); 380 | 381 | }// if not ask for permission first 382 | else { 383 | // get linkedinService authorization 384 | $url = $linkedinService->getAuthorizationUri(array('state'=>'DCEEFWF45453sdffef424')); 385 | 386 | // return to linkedin login url 387 | return Redirect::to( (string)$url ); 388 | } 389 | 390 | 391 | } 392 | 393 | ``` 394 | ### Yahoo: 395 | 396 | Configuration: 397 | Add your Yahoo credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` 398 | 399 | ```php 400 | 'Yahoo' => array( 401 | 'client_id' => 'Your Yahoo API KEY', 402 | 'client_secret' => 'Your Yahoo API Secret', 403 | ), 404 | ``` 405 | In your Controller use the following code: 406 | 407 | ```php 408 | 409 | public function loginWithYahoo() { 410 | // get data from input 411 | $token = Input::get( 'oauth_token' ); 412 | $verify = Input::get( 'oauth_verifier' ); 413 | // get yahoo service 414 | $yh = OAuth::consumer( 'Yahoo' ); 415 | 416 | // if code is provided get user data and sign in 417 | if ( !empty( $token ) && !empty( $verify ) ) { 418 | // This was a callback request from yahoo, get the token 419 | $token = $yh->requestAccessToken( $token, $verify ); 420 | $xid = array($token->getExtraParams()); 421 | $result = json_decode( $yh->request( 'https://social.yahooapis.com/v1/user/'.$xid[0]['xoauth_yahoo_guid'].'/profile?format=json' ), true ); 422 | 423 | dd($result); 424 | } 425 | // if not ask for permission first 426 | else { 427 | // get request token 428 | $reqToken = $yh->requestRequestToken(); 429 | // get Authorization Uri sending the request token 430 | $url = $yh->getAuthorizationUri(array('oauth_token' => $reqToken->getRequestToken())); 431 | // return to yahoo login url 432 | return Redirect::to( (string)$url ); 433 | } 434 | } 435 | 436 | ``` 437 | ### More usage examples: 438 | 439 | For examples go [here](https://github.com/Lusitanian/PHPoAuthLib/tree/master/examples) 440 | 441 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "artdarek/oauth-4-laravel", 3 | "type": "library", 4 | "description": "OAuth Service Provider for Laravel 4", 5 | "keywords": ["OAuth", "Lusitanian", "laravel", "php"], 6 | "homepage": "https://github.com/artdarek/oauth-4-laravel", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Artdarek", 11 | "email": "artdarek@gmail.com", 12 | "role": "Developer" 13 | }, 14 | { 15 | "name": "Alejandro Escobedo", 16 | "email": "aeg0204@gmail.com", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "php": ">=5.3", 22 | "lusitanian/oauth": "dev-master" 23 | }, 24 | "require-dev": { 25 | "illuminate/support": "~4" 26 | }, 27 | "require-all": true, 28 | "autoload": { 29 | "psr-0": { 30 | "Artdarek\\OAuth": "src/" 31 | } 32 | }, 33 | "minimum-stability": "dev" 34 | } 35 | -------------------------------------------------------------------------------- /src/Artdarek/OAuth/Facade/OAuth.php: -------------------------------------------------------------------------------- 1 | 4 | * @copyright Copyright (c) 2013 5 | * @license http://www.opensource.org/licenses/mit-license.html MIT License 6 | */ 7 | 8 | namespace Artdarek\OAuth\Facade; 9 | 10 | use Illuminate\Support\Facades\Facade; 11 | 12 | class OAuth extends Facade 13 | { 14 | 15 | /** 16 | * Get the registered name of the component. 17 | * 18 | * @return string 19 | */ 20 | protected static function getFacadeAccessor() { return 'oauth'; } 21 | 22 | } -------------------------------------------------------------------------------- /src/Artdarek/OAuth/OAuth.php: -------------------------------------------------------------------------------- 1 | 4 | * @copyright Copyright (c) 2013 5 | * @license http://www.opensource.org/licenses/mit-license.html MIT License 6 | */ 7 | 8 | namespace Artdarek\OAuth; 9 | 10 | use Illuminate\Support\ServiceProvider; 11 | 12 | use \Config; 13 | use \URL; 14 | 15 | use \OAuth\ServiceFactory; 16 | use \OAuth\Common\Consumer\Credentials; 17 | 18 | class OAuth 19 | { 20 | /** 21 | * @var ServiceFactory 22 | */ 23 | private $_serviceFactory; 24 | 25 | /** 26 | * Storege name from config 27 | * @var string 28 | */ 29 | private $_storage_name = 'Session'; 30 | 31 | /** 32 | * Client ID from config 33 | * @var string 34 | */ 35 | private $_client_id; 36 | 37 | /** 38 | * Client secret from config 39 | * @var string 40 | */ 41 | private $_client_secret; 42 | 43 | /** 44 | * Scope from config 45 | * @var array 46 | */ 47 | private $_scope = array(); 48 | 49 | /** 50 | * Constructor 51 | * 52 | * @param ServiceFactory $serviceFactory - (Dependency injection) If not provided, a ServiceFactory instance will be constructed. 53 | */ 54 | public function __construct(ServiceFactory $serviceFactory = null) 55 | { 56 | if (null === $serviceFactory) { 57 | // Create the service factory 58 | $serviceFactory = new ServiceFactory(); 59 | } 60 | $this->_serviceFactory = $serviceFactory; 61 | } 62 | 63 | /** 64 | * Detect config and set data from it 65 | * 66 | * @param string $service 67 | */ 68 | public function setConfig( $service ) 69 | { 70 | // if config/oauth-4-laravel.php exists use this one 71 | if ( Config::get('oauth-4-laravel.consumers') != null ) { 72 | 73 | $this->_storage_name = Config::get('oauth-4-laravel.storage', 'Session'); 74 | $this->_client_id = Config::get("oauth-4-laravel.consumers.$service.client_id"); 75 | $this->_client_secret = Config::get("oauth-4-laravel.consumers.$service.client_secret"); 76 | $this->_scope = Config::get("oauth-4-laravel.consumers.$service.scope", array() ); 77 | 78 | // esle try to find config in packages configs 79 | } else { 80 | $this->_storage_name = Config::get('oauth-4-laravel::storage', 'Session'); 81 | $this->_client_id = Config::get("oauth-4-laravel::consumers.$service.client_id"); 82 | $this->_client_secret = Config::get("oauth-4-laravel::consumers.$service.client_secret"); 83 | $this->_scope = Config::get("oauth-4-laravel::consumers.$service.scope", array() ); 84 | } 85 | } 86 | 87 | /** 88 | * Create storage instance 89 | * 90 | * @param string $storageName 91 | * @return OAuth\Common\\Storage 92 | */ 93 | public function createStorageInstance($storageName) 94 | { 95 | $storageClass = "\\OAuth\\Common\\Storage\\$storageName"; 96 | $storage = new $storageClass(); 97 | 98 | return $storage; 99 | } 100 | 101 | /** 102 | * Set the http client object 103 | * 104 | * @param string $httpClientName 105 | * @return void 106 | */ 107 | public function setHttpClient($httpClientName) 108 | { 109 | $httpClientClass = "\\OAuth\\Common\\Http\\Client\\$httpClientName"; 110 | $this->_serviceFactory->setHttpClient(new $httpClientClass()); 111 | } 112 | 113 | /** 114 | * @param string $service 115 | * @param string $url 116 | * @param array $scope 117 | * @return \OAuth\Common\Service\AbstractService 118 | */ 119 | public function consumer( $service, $url = null, $scope = null ) 120 | { 121 | // get config 122 | $this->setConfig( $service ); 123 | 124 | // get storage object 125 | $storage = $this->createStorageInstance( $this->_storage_name ); 126 | 127 | // create credentials object 128 | $credentials = new Credentials( 129 | $this->_client_id, 130 | $this->_client_secret, 131 | $url ?: URL::current() 132 | ); 133 | 134 | // check if scopes were provided 135 | if (is_null($scope)) 136 | { 137 | // get scope from config (default to empty array) 138 | $scope = $this->_scope; 139 | } 140 | 141 | // return the service consumer object 142 | return $this->_serviceFactory->createService($service, $credentials, $storage, $scope); 143 | 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/Artdarek/OAuth/OAuthServiceProvider.php: -------------------------------------------------------------------------------- 1 | 4 | * @copyright Copyright (c) 2013 5 | * @license http://www.opensource.org/licenses/mit-license.html MIT License 6 | */ 7 | 8 | namespace Artdarek\OAuth; 9 | 10 | use Illuminate\Support\ServiceProvider; 11 | 12 | class OAuthServiceProvider extends ServiceProvider 13 | { 14 | 15 | /** 16 | * Indicates if loading of the provider is deferred. 17 | * 18 | * @var bool 19 | */ 20 | protected $defer = false; 21 | 22 | /** 23 | * Bootstrap the application events. 24 | * 25 | * @return void 26 | */ 27 | public function boot() 28 | { 29 | $this->package('artdarek/oauth-4-laravel'); 30 | } 31 | 32 | /** 33 | * Register the service provider. 34 | * 35 | * @return void 36 | */ 37 | public function register() 38 | { 39 | // Register 'oauth' 40 | $this->app['oauth'] = $this->app->share(function($app) 41 | { 42 | // create oAuth instance 43 | $oauth = new OAuth(); 44 | // return oAuth instance 45 | return $oauth; 46 | }); 47 | } 48 | 49 | /** 50 | * Get the services provided by the provider. 51 | * 52 | * @return array 53 | */ 54 | public function provides() 55 | { 56 | return array(); 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /src/config/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/artdarek/oauth-4-laravel/6c37c4571caa695ee080aa60f01ef25ec19147de/src/config/.gitkeep -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | 'Session', 15 | 16 | /** 17 | * Consumers 18 | */ 19 | 'consumers' => array( 20 | 21 | /** 22 | * Facebook 23 | */ 24 | 'Facebook' => array( 25 | 'client_id' => '', 26 | 'client_secret' => '', 27 | 'scope' => array(), 28 | ), 29 | 30 | ) 31 | 32 | ); --------------------------------------------------------------------------------