Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dyweb/oauth2-jaccount

jAccount OAuth 2.0 support for the PHP League's OAuth 2.0 Client
https://github.com/dyweb/oauth2-jaccount

jaccount oauth2 sjtu

Last synced: about 2 months ago
JSON representation

jAccount OAuth 2.0 support for the PHP League's OAuth 2.0 Client

Awesome Lists containing this project

README

        

# JAccount Provider for OAuth 2.0 Client

This package provides JAccount OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).

## Installation

```
composer require dyweb/oauth2-jaccount
```

## Usage

```php
$provider = new \Dyweb\OAuth2\Client\Provider\JAccount([
'clientId' => 'yourId', // The client ID assigned to you by NIC
'clientSecret' => 'yourSecret', // The client password assigned to you by NIC
'redirectUri' => 'yourUrl' // The return URL you specified for your app on NIC
]);

// 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']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {

if (isset($_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 'Access Token: ' . $accessToken->getToken() . "
";
echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "
";
echo 'Expired in: ' . $accessToken->getExpires() . "
";
echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "
";

// Using the access token, we may look up details about the
// resource owner.
$resourceOwner = $provider->getResourceOwner($accessToken);

var_export($resourceOwner->toArray());

} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

// Failed to get the access token or user details.
exit($e->getMessage());

}

}
```

For more information see the PHP League's general usage examples.

## License

The MIT License (MIT). Please see [License File](https://github.com/michaelKaefer/oauth2-amazon/blob/master/LICENSE) for more information.