waytohealth / oauth2-omron
Omron OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Installs: 4 226
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 5
Requires
- php: >=5.6.0
- league/oauth2-client: 2.6.*
Requires (Dev)
- friendsofphp/php-cs-fixer: ~1.0
- jakub-onderka/php-parallel-lint: ~0.9
- mockery/mockery: ~0.9
- phpunit/phpunit: ^5.0
- squizlabs/php_codesniffer: ^2.0
- dev-master
- v3.0.0
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.0.2
- v1.0.1
- v1.0.0
- v0.0.1-alpha
- dev-api-update
- dev-remove-unused-dependency
- dev-dependabot/composer/mockery/mockery-approx-1.3
- dev-dependabot/composer/friendsofphp/php-cs-fixer-approx-2.19
- dev-dependabot/composer/squizlabs/php_codesniffer-tw-3.6
- dev-dependabot/composer/jakub-onderka/php-parallel-lint-approx-1.0
- dev-dependabot/composer/league/oauth2-client-2.6.star
- dev-dependabot/add-v2-config-file
- dev-lock-client-version
- dev-dev
This package is auto-updated.
Last update: 2024-11-03 16:27:58 UTC
README
This package provides Omron OAuth 2.0 support for the PHP League's OAuth 2.0 Client.
This package is compliant with PSR-1, PSR-2, PSR-4, and PSR-7. If you notice compliance oversights, please send a patch via pull request.
Requirements
The following versions of PHP are supported.
- PHP 5.6
- PHP 7.0
- PHP 7.1
- HHVM
Installation
To install, use composer:
composer require waytohealth/oauth2-omron
Usage
Authorization Code Grant
use waytohealth\OAuth2\Client\Provider\Omron; $provider = new Omron([ 'clientId' => '{omron-oauth2-client-id}', 'clientSecret' => '{omron-client-secret}', 'redirectUri' => 'https://example.com/callback-url' ]); // start the session session_start(); // If we don't have an authorization code then get one if (!isset($_GET['code'])) { // Fetch the authorization URL from the provider; this returns the // urlAuthorize option and generates and applies any necessary parameters // (e.g. state). $authorizationUrl = $provider->getAuthorizationUrl(); // Get the state generated for you and store it to the session. $_SESSION['oauth2state'] = $provider->getState(); // Redirect the user to the authorization URL. header('Location: ' . $authorizationUrl); exit; // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || array_key_exists('oauth2state', $_SESSION) && ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { try { // Try to get an access token using the authorization code grant. $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // We have an access token, which we may use in authenticated // requests against the service provider's API. echo $accessToken->getToken() . "\n"; echo $accessToken->getRefreshToken() . "\n"; echo $accessToken->getExpires() . "\n"; echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n"; // Using the access token, we may look up details about the // resource owner. $resourceOwner = $provider->getResourceOwner($accessToken); var_export($resourceOwner->toArray()); // The provider provides a way to get an authenticated API request for // the service, using the access token; it returns an object conforming // to Psr\Http\Message\RequestInterface. $request = $provider->getAuthenticatedRequest( Withings::METHOD_GET, Withings::BASE_WITHINGS_API_URL . '/v2/user?action=getdevice', $accessToken, ['headers' => [Withings::HEADER_ACCEPT_LANG => 'en_US'], [Withings::HEADER_ACCEPT_LOCALE => 'en_US']] // Fitbit uses the Accept-Language for setting the unit system used // and setting Accept-Locale will return a translated response if available. // https://dev.fitbit.com/docs/basics/#localization ); // Make the authenticated API request and get the parsed response. $response = $provider->getParsedResponse($request); // If you would like to get the response headers in addition to the response body, use: //$response = $provider->getResponse($request); //$headers = $response->getHeaders(); //$parsedResponse = $provider->parseResponse($response); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token or user details. exit($e->getMessage()); } }
Refreshing a Token
Once your application is authorized, you can refresh an expired token using a refresh token rather than going through the entire process of obtaining a brand new token. To do so, simply reuse this refresh token from your data store to request a refresh.
$provider = new waytohealth\OAuth2\Client\Provider\Omron([ 'clientId' => '{omron-oauth2-client-id}', 'clientSecret' => '{omron-client-secret}', 'redirectUri' => 'https://example.com/callback-url', 'authHostname' => 'https://oauth.omronwellness.com', 'apiUrl' => 'https://api.omronwellness.com/api/measurement' ]); $existingAccessToken = getAccessTokenFromYourDataStore(); if ($existingAccessToken->hasExpired()) { $newAccessToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $existingAccessToken->getRefreshToken() ]); // Purge old access token and store new access token to your data store. }
Testing
$ ./vendor/bin/phpunit
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.