gn-office / oauth2-line
LINE OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Installs: 10 626
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- league/oauth2-client: ^2.0
Requires (Dev)
- mockery/mockery: ~1.3
- phpunit/phpunit: ~8.0
This package is auto-updated.
Last update: 2024-12-12 20:40:00 UTC
README
This package provides LINE OAuth 2.0 support for the PHP League's OAuth 2.0 Client.
Usage
<?php use GNOffice\OAuth2\Client\Provider\Line; session_start(); $provider = new Line([ 'clientId' => '{line-channel-id}', 'clientSecret' => '{line-channel-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); // Get authorization code if (!isset($_GET['code'])) { // Get authorization URL $authorizationUrl = $provider->getAuthorizationUrl(); // Get state and store it to the session. $_SESSION['oauth2state'] = $provider->getState(); // Get nonce and store it to the session. $_SESSION['oauth2nonce'] = $provider->getNonce(); // Get code_verifier and store it to the session. $_SESSION['oauth2code_verifier'] = $provider->getCodeVerifier(); // Redirect the user to the authorization URL. header('Location: ' . $authorizationUrl); exit; // Check for errors } elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) { if (isset($_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); } exit('Invalid state'); } else { // Get access token try { $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'], 'nonce' => $_SESSION['oauth2nonce'] ]); try { $nonce = $_SESSION['oauth2nonce']; $token_values = $accessToken->getValues(); $id_token = $token_values['id_token']; $access_token = $accessToken->getToken(); // Verify ID Token $verify_id_token = $provider->verifyIdToken($id_token, $nonce); // Verify Access Token $verify_token = $provider->verifyAccessToken($access_token); // Get resource owner $user = $provider->getResourceOwner($accessToken); $email = $provider->getEmail($id_token, $nonce); // Use these details to create a new profile printf('Hello %s!', $user->getName()); } catch (GNOffice\OAuth2\Client\Provider\Exception\InvalidTokenException $e) { exit($e->getMessage()); } } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token or user details. exit($e->getMessage()); } // Use this to interact with an API on the users behalf echo $accessToken->getToken(); }
Testing
$ ./vendor/bin/phpunit
License
The MIT License (MIT). Please see License File for more information.