├── .editorconfig ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── public └── .gitkeep ├── src ├── Kalley │ └── LaravelOauthClient │ │ ├── AbstractOAuthClient.php │ │ ├── Console │ │ └── MigrationCommand.php │ │ ├── Facades │ │ ├── BitbucketFacade.php │ │ ├── EventbriteFacade.php │ │ ├── FacebookFacade.php │ │ ├── GithubFacade.php │ │ ├── GoogleFacade.php │ │ ├── InstagramFacade.php │ │ ├── LinkedInFacade.php │ │ ├── MicrosoftFacade.php │ │ ├── TumblrFacade.php │ │ └── TwitterFacade.php │ │ ├── LaravelOauthClientServiceProvider.php │ │ ├── OAuth1Client.php │ │ └── OAuth2Client.php ├── config │ ├── .gitkeep │ └── oauth-client.php ├── controllers │ └── .gitkeep ├── lang │ └── .gitkeep ├── migrations │ ├── .gitkeep │ └── 2014_08_28_160340_create_oauth_client_setup_tables.php └── views │ ├── .gitkeep │ └── generators │ └── migration.blade.php └── tests └── .gitkeep /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | before_script: 11 | - composer self-update 12 | - composer install --prefer-source --no-interaction --dev 13 | 14 | script: phpunit 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Kalley Powell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OAuth 1.0 and 2.0 Client for Laravel 2 | 3 | This package is built upon [The PHP League's](https://github.com/thephpleague) OAuth Client libraries. 4 | 5 | It provides facades for the packaged servers/providers and unites the API across both. 6 | 7 | *I understand that may seem strange, since there is different language associated with each version, but it makes it easier to use both* 8 | 9 | ### Disclaimer 10 | 11 | This has only been tested using authentication and getting user details. I haven't attempted to use these to make further API calls yet. 12 | 13 | ## Requirements 14 | 15 | *from [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client)* 16 | 17 | The following versions of PHP are supported. 18 | 19 | * PHP 5.4 20 | * PHP 5.5 21 | * PHP 5.6 22 | * HHVM 23 | 24 | ## Servers/Providers included 25 | 26 | * OAuth 1.0 27 | * Bitbucket 28 | * Tumblr 29 | * Twitter 30 | * OAuth 2.0 31 | * Eventbrite 32 | * Facebook 33 | * Github 34 | * Google 35 | * Instagram 36 | * LinkedIn 37 | * Microsoft 38 | 39 | ## Package Installation 40 | 41 | Add the following line to your composer.json file: 42 | 43 | ```javascript 44 | "kalley/laravel-oauth-client": "dev-master" 45 | ``` 46 | 47 | or run `composer require kalley/laravel-oauth-client:dev-master` from the command line 48 | 49 | Add this line of code to the ```providers``` array located in your ```app/config/app.php``` file: 50 | ```php 51 | 'Kalley\LaravelOauthClient\LaravelOauthClientServiceProvider', 52 | ``` 53 | 54 | ### Configuration 55 | 56 | In order to use the OAuth Client, publish its configuration first 57 | 58 | ``` 59 | php artisan config:publish kalley/laravel-oauth-client 60 | ``` 61 | 62 | Afterwards edit the file ```app/config/packages/kalley/laravel-oauth-client/oauth-client.php``` to suit your needs. 63 | 64 | You will probably want to go ahead and add Facades for the providers you're planning to use as well. For example, if you were integration Facebook: 65 | 66 | ``` 67 | 'Facebook' => 'Kalley\LaravelOauthClient\Facades\FacebookFacade', 68 | ``` 69 | 70 | and so on. If you don't do this, you can call them using `App::make('oauth-client.facebook')`; 71 | 72 | These will return an instance of the `AbstractOAuthClient` class. 73 | 74 | ### Migrations 75 | 76 | This package comes with all the migrations you need to run a full featured oauth2 server. Run: 77 | 78 | ``` 79 | php artisan oauth-client:migrations 80 | ``` 81 | 82 | ## Usage 83 | 84 | #### User authorization 85 | 86 | This will take care of everything, including the redirection to the service 87 | 88 | ``` 89 | Facebook::authorize(); 90 | ``` 91 | 92 | #### Getting the access token 93 | 94 | For OAuth 1.0: 95 | ``` 96 | $token = Twitter::getAccessToken(['oauth_token' => Input::get('oauth_token'), 'oauth_verifier' => Input::get('oauth_verifier')]); 97 | ``` 98 | 99 | For OAuth 2.0: 100 | ``` 101 | $token = Facebook::getAccessToken(Input::get('code')); 102 | ``` 103 | 104 | #### Getting user details 105 | 106 | __You will need to get the access token first.__ 107 | 108 | ``` 109 | $social_user = Facebook::getUserDetails(); 110 | ``` 111 | 112 | After that, it's pretty much up to you at this point. If you want to get a better idea of what is going on, please take a look at the required packages: 113 | 114 | * [OAuth 1.0 Client](https://github.com/thephpleague/oauth1-client) 115 | * [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client) 116 | 117 | ## Support 118 | 119 | Bugs and feature request are tracked on [GitHub](https://github.com/kalley/laravel-oauth-client/issues) 120 | 121 | ## License 122 | 123 | This package is released under the MIT License. 124 | 125 | ## Credit 126 | 127 | The code on which this package is based is primarily developed and maintained by [Alex Bilbie](https://twitter.com/alexbilbie). -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kalley/laravel-oauth-client", 3 | "description": "OAuth 1.0 and 2.0 Client for Laravel", 4 | "keywords": ["laravel","illuminate","oauth","client"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "kalley", 9 | "email": "kalley.powell@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0", 14 | "illuminate/support": "4.2.*", 15 | "league/oauth2-client": "~0.7", 16 | "league/oauth1-client": "~1.0" 17 | }, 18 | "autoload": { 19 | "classmap": [ 20 | "src/migrations" 21 | ], 22 | "psr-0": { 23 | "Kalley\\LaravelOauthClient\\": "src/" 24 | } 25 | }, 26 | "minimum-stability": "stable" 27 | } 28 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /public/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kalley/laravel-oauth-client/7a38608e84c9e7dd252616d1a11cda5ffb291662/public/.gitkeep -------------------------------------------------------------------------------- /src/Kalley/LaravelOauthClient/AbstractOAuthClient.php: -------------------------------------------------------------------------------- 1 | providerName = strtolower($providerName); 23 | $providerClass = (isset($this->mapped[$this->providerName]) ? $this->namespace . $this->mapped[$this->providerName] : (array_key_exists('providerClass', $config) ? $config['providerClass'] : $this->providerName)); 24 | $this->provider = new $providerClass($config); 25 | } 26 | 27 | abstract public function getAccessToken($code); 28 | 29 | public function setAccessToken(AccessToken $token) { 30 | $this->token = $token; 31 | } 32 | 33 | public function getUserDetails() { 34 | if ( ! $this->token ) { 35 | throw new \Exception('You must first get the access token before getting user details.'); 36 | } 37 | return $this->provider->getUserDetails($this->token); 38 | } 39 | 40 | public function refreshToken() { 41 | return $this->token; 42 | } 43 | 44 | public function __call($method, $args) { 45 | if ( method_exists($this->provider, $method) ) { 46 | switch(count($args)) { 47 | case 0: return $this->provider->$method(); 48 | case 1: return $this->provider->$method($args[0]); 49 | case 2: return $this->provider->$method($args[0], $args[1]); 50 | case 3: return $this->provider->$method($args[0], $args[1], $args[2]); 51 | case 4: return $this->provider->$method($args[0], $args[1], $args[2], $args[3]); 52 | case 5: return $this->provider->$method($args[0], $args[1], $args[2], $args[3], $args[4]); 53 | default: return call_user_func_array($method, $args); 54 | } 55 | } 56 | // Throw an Exception? 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Kalley/LaravelOauthClient/Console/MigrationCommand.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) Kalley Powell 8 | * @licence http://mit-license.org/ 9 | * @link https://github.com/kalley/laravel-oauth-client 10 | */ 11 | 12 | namespace Kalley\LaravelOauthClient\Console; 13 | 14 | use Illuminate\Console\Command; 15 | use Symfony\Component\Console\Input\InputOption; 16 | 17 | class MigrationCommand extends Command { 18 | /** 19 | * The console command name. 20 | * 21 | * @var string 22 | */ 23 | protected $name = 'oauth-client:migrations'; 24 | 25 | /** 26 | * The console command description. 27 | * 28 | * @var string 29 | */ 30 | protected $description = 'Create the migrations needed for OAuth Client'; 31 | 32 | public $app; 33 | 34 | /** 35 | * Create a new command instance. 36 | * 37 | * @param \Illuminate\Foundation\Application $app Laravel application object 38 | * 39 | * @return void 40 | */ 41 | public function __construct($app = null) { 42 | if (!is_array($app)) 43 | parent::__construct(); 44 | 45 | $this->app = $app ?: app(); 46 | } 47 | 48 | /** 49 | * Get the console command options. 50 | * 51 | * @return array 52 | */ 53 | protected function getOptions() { 54 | return array( 55 | array('table', null, InputOption::VALUE_OPTIONAL, 'Table name.', 'users'), 56 | ); 57 | } 58 | 59 | 60 | /** 61 | * Execute the console command. 62 | * 63 | * @return void 64 | */ 65 | public function fire() { 66 | // Prepare variables 67 | $table = lcfirst($this->option('table')); 68 | 69 | $viewVars = compact( 70 | 'table' 71 | ); 72 | 73 | // Prompt 74 | $this->line(''); 75 | $this->info("Table name: $table"); 76 | $this->comment( 77 | "A migration that creates social_sites and user_social_sites tables,". 78 | " referencing the $table table will". 79 | " be created in app/database/migrations directory" 80 | ); 81 | $this->line(''); 82 | 83 | if ($this->confirm("Proceed with the migration creation? [Yes|no]")) { 84 | $this->info("Creating migration..."); 85 | // Generate 86 | $filename = 'database/migrations/' . date('Y_m_d_His') . "_create_oauth_client_setup_tables.php"; 87 | $output = $this->app['view']->make('laravel-oauth-client::generators.migration', $viewVars) 88 | ->render(); 89 | $filename = $this->app['path'] . '/' . trim($filename,'/'); 90 | $directory = dirname($filename); 91 | if (!is_dir($directory)) { 92 | @mkdir($directory, 0755, true); 93 | } 94 | @file_put_contents($filename, $output, FILE_APPEND); 95 | 96 | $this->info("Migration successfully created!"); 97 | } 98 | $this->call('dump-autoload'); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/Kalley/LaravelOauthClient/Facades/BitbucketFacade.php: -------------------------------------------------------------------------------- 1 | app['config']->get('laravel-oauth-client::oauth-client.' . $key); 17 | } 18 | 19 | /** 20 | * Bootstrap the application events. 21 | * 22 | * @return void 23 | */ 24 | public function boot() { 25 | $this->package('kalley/laravel-oauth-client'); 26 | $providers = $this->config('providers'); 27 | foreach ( $providers as $provider => $config ) { 28 | $provider = strtolower($provider); 29 | $this->app->bindShared('oauth-client.' . $provider, function($app) use($provider, $config) { 30 | return AbstractOAuthClient::factory($provider, $config); 31 | }); 32 | } 33 | } 34 | 35 | /** 36 | * Register the service provider. 37 | * 38 | * @return void 39 | */ 40 | public function register() { 41 | $this->registerCommands(); 42 | } 43 | 44 | public function registerCommands() { 45 | $this->app->bindShared('command.oauth-client.migration', function ($app) { 46 | return new MigrationCommand($app); 47 | }); 48 | $this->commands('command.oauth-client.migration'); 49 | } 50 | 51 | /** 52 | * Get the services provided by the provider. 53 | * 54 | * @return array 55 | */ 56 | public function provides() { 57 | return [];//array_map('strtolower', array_keys($this->config('providers'))); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/Kalley/LaravelOauthClient/OAuth1Client.php: -------------------------------------------------------------------------------- 1 | 'Twitter', 11 | 'bitbucket' => 'Bitbucket', 12 | 'tumblr' => 'Tumblr', 13 | ]; 14 | 15 | public function getTemporaryCredentials() { 16 | $temporaryCredentials = $this->provider->getTemporaryCredentials(); 17 | return $temporaryCredentials; 18 | } 19 | 20 | public function authorize($temporaryCredentials = null) { 21 | if ( $temporaryCredentials === null ) { 22 | $temporaryCredentials = $this->getTemporaryCredentials(); 23 | } 24 | Session::put('twitter_temporary_credentials', $temporaryCredentials); 25 | return Redirect::to($this->provider->getAuthorizationUrl($temporaryCredentials)); 26 | } 27 | 28 | public function getAccessToken($code) { 29 | $temporaryCredentials = Session::get('twitter_temporary_credentials'); 30 | $this->token = $this->provider->getTokenCredentials($temporaryCredentials, $code['oauth_token'], $code['oauth_verifier']); 31 | return $this->token; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /src/Kalley/LaravelOauthClient/OAuth2Client.php: -------------------------------------------------------------------------------- 1 | 'Eventbrite', 8 | 'facebook' => 'Facebook', 9 | 'github' => 'Github', 10 | 'google' => 'Google', 11 | 'instagram' => 'Instagram', 12 | 'linkedin' => 'LinkedIn', 13 | 'microsoft' => 'Microsoft', 14 | ]; 15 | 16 | public function authorize() { 17 | return \Redirect::to($this->getAuthorizationUrl()); 18 | } 19 | 20 | public function getAccessToken($code) { 21 | $params = ['code' => $code]; 22 | if ( $this->providerName === 'eventbrite' ) { 23 | $params['grant_type'] = 'authorization_code'; 24 | } 25 | $this->token = $this->provider->getAccessToken('authorization_code', $params); 26 | return $this->token; 27 | } 28 | 29 | public function refreshToken() { 30 | $grant = new \League\OAuth2\Client\Grant\RefreshToken(); 31 | $this->token = $this->provider->getAccessToken($grant, ['refresh_token' => $this->token->refreshToken]); 32 | return $this->token; 33 | } 34 | 35 | public function __get($property) { 36 | switch ( $property ) { 37 | case 'accessToken': return $this->token->accessToken; 38 | case 'refreshToken': return $this->token->refreshToken; 39 | case 'expires': return $this->token->expires; 40 | } 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/config/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kalley/laravel-oauth-client/7a38608e84c9e7dd252616d1a11cda5ffb291662/src/config/.gitkeep -------------------------------------------------------------------------------- /src/config/oauth-client.php: -------------------------------------------------------------------------------- 1 | 'default', 15 | 16 | /* 17 | |-------------------------------------------------------------------------- 18 | | Providers to use 19 | |-------------------------------------------------------------------------- 20 | | 21 | | Available providers: 22 | | 23 | | OAuth 1 server: 24 | | Twitter 25 | | Bitbucket 26 | | Tumblr 27 | | 28 | | OAuth 2: 29 | | Eventbrite 30 | | Facebook 31 | | Github 32 | | Google 33 | | Instagram 34 | | LinkedIn 35 | | Microsoft 36 | | 37 | | Configure like (replace oauth1server with name from above, and oauth2provider with provider from above): 38 | | 39 | | 'providers' => [ 40 | | 'oauth1server' => [ 41 | | 'identifier' => 'your-identifier', 42 | | 'secret' => 'your-secret', 43 | | 'callback_uri' => 'http://your-callback-uri/', 44 | | 'providerClass' => '{Namespace}\{Class}' // this is required when using a custom provider 45 | | ], 46 | | 'oauth2provider' => [ 47 | | 'clientId' => 'XXXXXXXX', 48 | | 'clientSecret' => 'XXXXXXXX', 49 | | 'redirectUri' => 'https://your-registered-redirect-uri/', 50 | | 'providerClass' => '{Namespace}\{Class}' // this is required when using a custom provider 51 | | ] 52 | | ] 53 | | 54 | */ 55 | 'providers' => [ 56 | 57 | ], 58 | ]; 59 | -------------------------------------------------------------------------------- /src/controllers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kalley/laravel-oauth-client/7a38608e84c9e7dd252616d1a11cda5ffb291662/src/controllers/.gitkeep -------------------------------------------------------------------------------- /src/lang/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kalley/laravel-oauth-client/7a38608e84c9e7dd252616d1a11cda5ffb291662/src/lang/.gitkeep -------------------------------------------------------------------------------- /src/migrations/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kalley/laravel-oauth-client/7a38608e84c9e7dd252616d1a11cda5ffb291662/src/migrations/.gitkeep -------------------------------------------------------------------------------- /src/migrations/2014_08_28_160340_create_oauth_client_setup_tables.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('name'); 18 | }); 19 | 20 | Schema::create('user_social_sites', function($table) { 21 | $table->bigIncrements('id'); 22 | $table->integer('user_id')->unsigned(); 23 | $table->integer('social_site_id')->unsigned(); 24 | // No length: http://stackoverflow.com/questions/4408945/what-is-the-length-of-the-access-token-in-facebook-oauth2#answer-16365828 25 | $table->string('access_token');g 26 | $table->string('social_user_id'); 27 | $table->timestamps(); 28 | 29 | $table->foreign('user_id')->references('id')->on('users') 30 | ->onUpdate('cascade')->onDelete('cascade'); 31 | $table->foreign('social_site_id')->references('id')->on('social_sites') 32 | ->onUpdate('cascade')->onDelete('cascade'); 33 | }); 34 | } 35 | 36 | /** 37 | * Reverse the migrations. 38 | * 39 | * @return void 40 | */ 41 | public function down() 42 | { 43 | Schema::drop('user_social_sites'); 44 | Schema::drop('social_sites'); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/views/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kalley/laravel-oauth-client/7a38608e84c9e7dd252616d1a11cda5ffb291662/src/views/.gitkeep -------------------------------------------------------------------------------- /src/views/generators/migration.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | use Illuminate\Database\Schema\Blueprint; 4 | use Illuminate\Database\Migrations\Migration; 5 | 6 | class CreateOauthClientSetupTables extends Migration { 7 | 8 | /** 9 | * Run the migrations. 10 | * 11 | * @return void 12 | */ 13 | public function up() { 14 | Schema::create('social_sites', function($table) { 15 | $table->increments('id'); 16 | $table->string('name'); 17 | }); 18 | 19 | Schema::create('user_social_sites', function($table) { 20 | $table->bigIncrements('id'); 21 | $table->integer('user_id')->unsigned(); 22 | $table->integer('social_site_id')->unsigned(); 23 | // No length: http://stackoverflow.com/questions/4408945/what-is-the-length-of-the-access-token-in-facebook-oauth2#answer-16365828 24 | $table->string('access_token'); 25 | $table->string('social_user_id'); 26 | $table->timestamps(); 27 | 28 | $table->foreign('user_id')->references('id')->on('{{ $table }}') 29 | ->onUpdate('cascade')->onDelete('cascade'); 30 | $table->foreign('social_site_id')->references('id')->on('social_sites') 31 | ->onUpdate('cascade')->onDelete('cascade'); 32 | }); 33 | } 34 | 35 | /** 36 | * Reverse the migrations. 37 | * 38 | * @return void 39 | */ 40 | public function down() { 41 | Schema::drop('user_social_sites'); 42 | Schema::drop('social_sites'); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kalley/laravel-oauth-client/7a38608e84c9e7dd252616d1a11cda5ffb291662/tests/.gitkeep --------------------------------------------------------------------------------