2017 © Pedro Peláez
 

library oauth2-client

Official OAuth2 Client for Preview Technologies

image

previewtechs/oauth2-client

Official OAuth2 Client for Preview Technologies

  • Monday, April 16, 2018
  • by shahariaazam
  • Repository
  • 1 Watchers
  • 0 Stars
  • 207 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 12 Versions
  • 10 % Grown

The README.md

Previewtechs Provider for OAuth 2.0 Client

This package provides Preview Technologies Limited OAuth 2.0 support for the PHP League's OAuth 2.0 Client., (*1)

Latest Stable Version License Build Status Code Coverage Code Quality, (*2)

Installation

To install, use composer:, (*3)

composer require previewtechs/oauth2-client

Usage

Usage is the same as The League's OAuth client, using \Previewtechs\Oauth2\Client\Provider as the provider., (*4)

Authorization Code Flow

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',   // The client secret assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

For further usage of this package please refer to the core package documentation on "Authorization Code Grant"., (*5)

To get a full list of scopes, please go to List of available OAuth2.0 scopes, (*6)

Refreshing a Token

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',   // The client secret assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

$existingAccessToken = getAccessTokenFromYourDataStore();

if ($existingAccessToken->hasExpired()) {
    $newAccessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $existingAccessToken->getRefreshToken()
    ]);

    //Remove old and save new access token in your database
}

For further usage of this package please refer to the core package documentation on "Refreshing a Token"., (*7)

Complete Usage

<?php
require "vendor/autoload.php";

session_start();

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',
    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',
    // The client password assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

// 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']) || ($_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());

        // We 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(
            'GET',
            'https://myaccount.previewtechs.com/api/v1/identity/user-info',
            $accessToken
        );

        $client = new \GuzzleHttp\Client();
        $result = $client->send($request);

        var_export(json_decode($result->getBody()->getContents(), true));

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

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

    }

}

Contributing

Please see CONTRIBUTING page for details., (*8)

Credits

License

The MIT License (MIT), (*9)

Notes: heavily inspired from The PHP League Third Party Provider's libraries, (*10)

The Versions

16/04 2018

dev-master

9999999-dev

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

05/03 2018

v2.4

2.4.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

05/03 2018

dev-php5-compatible

dev-php5-compatible

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

05/03 2018

v1.4

1.4.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

11/11 2017

v2.3

2.3.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

11/11 2017

v1.3

1.3.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

20/08 2017

v2.2

2.2.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

20/08 2017

v1.2

1.2.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

22/04 2017

v1.1

1.1.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

22/04 2017

v2.1

2.1.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

09/04 2017

v2.0

2.0.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs

09/04 2017

v1.0

1.0.0.0

Official OAuth2 Client for Preview Technologies

  Sources   Download

MIT

The Requires

 

The Development Requires

authorization oauth client oauth2 authorisation previewtechnologies previewtechs