lambelcebur / oauth2-social-client
Lam extends module from thephpleague/oauth2-client
dev-master
2020-01-10 07:08 UTC
Requires
- php: ^7.1
- league/oauth2-client: 2.*
Suggests
- lambelcebur/doctrine-orm-fast-api: Quickly create an automatic API CRUD with your Doctrine ORM connection
- lambelcebur/doctrine-orm-resources: DoctrineORM Resources to Lam
- lambelcebur/materialize-css-helper: MaterializeCss Helper integration to Lam
- lambelcebur/mongodb-logger: MongoDb Logger integration to Lam
- lambelcebur/mvc-basic-tools: Lam tools for controllers,views,helpers, plugins and more ...
- laminas/laminas-mvc-plugin-flashmessenger: Recommended to display provider response messages
- league/oauth2-facebook: facebook provider
- league/oauth2-github: github provider
- league/oauth2-google: google provider
- league/oauth2-instagram: instagram provider
- league/oauth2-linkedin: linkedin provider
- stevenmaguire/oauth2-microsoft: microsoft provider
This package is not auto-updated.
Last update: 2025-03-05 10:47:32 UTC
README
Lam extends module from thephpleague/oauth2-client
See
- https://packagist.org/explore/?query=lambelcebur
- https://oauth2-client.thephpleague.com/
- https://oauth2-client.thephpleague.com/providers/league/
- https://oauth2-client.thephpleague.com/providers/thirdparty/
Installation
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
composer require lambelcebur/oauth2-social-client
Then add LamBelcebur\OAuth2SocialClient
to your config/application.config.php
and copy config/lambelcebur-oauth2-social.global.php.dist
to your autoload config folder and configure it with your providers.
Callback route format Url or Zend Route
<?php [ 'CUSTOM_PROVIDER_KEY' => [ 'factory-data' => [ 'class' => \League\OAuth2\Client\Provider\Facebook::class, // se others in https://github.com/thephpleague/oauth2-client/blob/master/docs/providers/thirdparty.md // redirectUri -> Url Option 'callbackRoute' => 'https://www.google.es/callback', // redirectUri -> Zend Route Option 'callbackRoute' => [ 'name' => 'belcebur-social', 'params' => [ 'action' => 'facebook' ], ], ], ], ]
How to use?
Get Controller Plugin and Get Callback Response from provider
<?php namespace Application\Controller; use League\OAuth2\Client\Provider\GoogleUser; use League\OAuth2\Client\Token\AccessToken; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; use LamBelcebur\OAuth2SocialClient\Controller\Plugin\OAuth2SocialPlugin; /** * Class IndexController * @package Application\Controller * @method OAuth2SocialPlugin oAuth2Social() */ class IndexController extends AbstractActionController { public function indexAction(): ViewModel { $oAuth2Social = $this->oAuth2Social(); // return \LamBelcebur\OAuth2SocialClient\Controller\Plugin\OAuth2SocialPlugin $oAuth2Social->getOAuth2Social()->getAll(); // Return array with all providers $oAuth2Social->getOAuth2Social()->getProviderByName('CUSTOM_PROVIDER_KEY'); // Return provider; $oAuth2Social->getOAuth2Social()->getAuthorizationUrl('CUSTOM_PROVIDER_KEY', ['extraParam' => 'extraValue']); // Return authorization url from a provider return new ViewModel(); } public function googleCallbackAction(): \Laminas\Http\Response { $state = json_decode($this->params()->fromQuery('state', '{}'), true); $oAuth2Social = $this->oAuth2Social(); // return \LamBelcebur\OAuth2SocialClient\Controller\Plugin\OAuth2SocialPlugin return $oAuth2Social->getProviderResponse($state['provider'] ?? 'google', static function (GoogleUser $googleUser, AccessToken $token) use ($state) { \var_dump($googleUser); \var_dump($token); \var_dump($state); return $this->redirect()->toUrl($state['currentUrl']); }, $oAuth2Social->defaultCallbackError); } }
View Helper
<?php /** @var \LamBelcebur\OAuth2SocialClient\View\Helper\OAuth2SocialViewHelper $oAuth2Social */ $oAuth2Social = $this->oAuth2Social(); //return \Laminas\Router\Http\RouteMatch $oAuth2Social->getRouteMatch(); // return \Laminas\Http\PhpEnvironment\Request $oAuth2Social->getRequest(); // return \Laminas\Router\RouteStackInterface $oAuth2Social->getRouter(); // return ['CUSTOM_PROVIDER_KEY'=>'CUSTOM_PROVIDER_KEY_URL','CUSTOM_PROVIDER_KEY2'=>'CUSTOM_PROVIDER_KEY2_URL'] $oAuth2Social->getAllAuthorizationUrl(['CUSTOM_PROVIDER_KEY'=>['param1'=>'$value1'],'CUSTOM_PROVIDER_KEY_2'=>['param1'=>'$value1','param2'=>'$value2']]); // return 'CUSTOM_PROVIDER_KEY_URL' $oAuth2Social->getAuthorizationUrlByName('CUSTOM_PROVIDER_KEY'); // return \LamBelcebur\OAuth2SocialClient\Service\OAuth2SocialService $oAuth2Social->getOAuth2SocialService(); ?>
Config File
###config/lambelcebur-oauth2-social.global.php.dist
<?php [ __NAMESPACE__ => [ 'facebook' => [ 'factory-data' => [ 'class' => \League\OAuth2\Client\Provider\Facebook::class, // se others in https://github.com/thephpleague/oauth2-client/blob/master/docs/providers/thirdparty.md // redirectUri -> Url Option 'callbackRoute' => 'https://www.google.es/callback', ], 'provider-data' => [ // See provider info in https://github.com/thephpleague/oauth2-client 'clientId' => 'xxx-your-client-id-xxx', 'clientSecret' => 'xxx-your-client-secret-xxx', 'graphApiVersion' => 'v3.0', // other provider data ], 'authorization-url-params' => [ 'approval_prompt' => 'force', //forzar pantalla 'scope' => [ 'public_profile', 'email', 'user_birthday', 'user_gender', 'user_link', 'user_location', 'user_hometown', ], ], ], 'google' => [ 'factory-data' => [ 'class' => \League\OAuth2\Client\Provider\Google::class, 'callbackRoute' => [ 'name' => 'belcebur-social', 'params' => [ 'action' => 'google' ], ], ], 'provider-data' => [ 'clientId' => 'xxx-your-client-id-xxx', 'clientSecret' => 'xxx-your-client-secret-xxx', ], ], 'intranet' => [ 'factory-data' => [ 'class' => \League\OAuth2\Client\Provider\Google::class, 'callbackRoute' => [ 'name' => 'belcebur-social', 'params' => [ 'action' => 'intranet' ], ], ], 'provider-data' => [ 'clientId' => 'xxx-your-client-id-xxx', 'clientSecret' => 'xxx-your-client-secret-xxx', 'hostedDomain' => 'example.com', ], 'authorization-url-params' => [ //'approval_prompt' => 'force', //forzar pantalla 'scope' => [ 'email', 'openid', 'profile', 'https://mail.google.com/', ], ], ], 'linkedin' => [ 'factory-data' => [ 'class' => \League\OAuth2\Client\Provider\LinkedIn::class, 'callbackRoute' => [ 'name' => 'belcebur-social', 'params' => [ 'action' => 'linkedin' ], ], ], 'provider-data' => [ 'clientId' => 'xxx-your-client-id-xxx', 'clientSecret' => 'xxx-your-client-secret-xxx', ], ], 'microsoft' => [ 'factory-data' => [ 'class' => \Stevenmaguire\OAuth2\Client\Provider\Microsoft::class, 'callbackRoute' => [ 'name' => 'belcebur-social', 'params' => [ 'action' => 'microsoft' ], ], ], 'provider-data' => [ 'clientId' => 'xxx-your-client-id-xxx', 'clientSecret' => 'xxx-your-client-secret-xxx', ], 'authorization-url-params' => [ 'scope' => [ 'wl.basic', 'wl.signin', 'wl.emails', ], ], ], ], ];