2017 © Pedro Peláez
 

library oauth2-envato

Envato OAuth 2.0 Client Provider for The PHP League OAuth2-Client

image

smachi/oauth2-envato

Envato OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  • Tuesday, February 9, 2016
  • by smachi
  • Repository
  • 1 Watchers
  • 2 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Envato Provider for PHP League OAuth 2.0 Client

Installation

To install, use composer:, (*1)

composer require smachi/envato-oauth2-provider, (*2)

Authorization Code Flow

$provider = new \Smachi\OAuth2\Client\Provider\Envato([
    'clientId'          => '{envato-client-id}',
    'clientSecret'      => '{envato-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
]);

if ( ! isset( $_GET['code'] ) ) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();

    return new RedirectResponse( $authUrl );

}
// 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');
}

// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken( 'authorization_code', [
    'code' => $_GET['code']
] );

try {

    // We got an access token, let's now get the user's details
    $owner          = $provider->getResourceOwner( $token, 'username' );
    $ownerEmail     = $provider->getResourceOwner( $token, 'email' );
    $ownerPurchases = $provider->getResourceOwner( $token, 'purchases', [ 'filter_by' => 'wordpress-themes' ] );

    $username   = preg_replace( '/[^a-z0-9-_]/i', '', $owner->getUsername() );
    $email      = $ownerEmail->getEmail();
    $purchases  = $ownerPurchases->getPurchases();
    $authorName = 'YourEnvatoAuthorUserName';

    if ( empty( $purchases ) ) {
        throw new \Exception(
            "Only current buyers have access to <strong>$authorName</strong> support forums.",
            401
        );
    }
    else{

        // Check for item support validity
        $maybePurchaseFromAuthor = FALSE;

        foreach($purchases as $item){

            if ( $authorName == $item['item']['author_username'] ){

                $maybePurchaseFromAuthor = TRUE;
                if ( strtotime( $item['supported_until'] ) > time() ){
                    // The support license is still valid
                    $itemUrl = $item['item']['url'];
                    break;
                }

            }

        }

        // Support expired
        if ( $maybePurchaseFromAuthor ){

            if ( ! $itemUrl ) {
                throw new \Exception(
                    'Your support license has expired.<br>Please <a href="' . $itemUrl . '" target="_blank">renew it</a> and come back again to get access.',
                    901
                );
            }

        }
        // Did not purchase any item from author
        else{
            throw new \Exception(
                "Only current buyers have access to <strong>$authorName</strong> support forums.",
                401
            );
        }


    }

} catch (\Exception $e){
    die( $e->getMessage() );
}

Some Auth User Data

$provider->getResourceOwner( $token, 'username' )

Object:, (*3)

$user->getUsername()

$provider->getResourceOwner( $token, 'email' )

Object:, (*4)

$user->getEmail()

$provider->getResourceOwner( $token, 'purchases', [ 'filter_by' => 'NULL | wordpress-themes | wordpress-plugins' ] )

Object:, (*5)

$user->getPurchases()

$user->getPurchasesCount()

The Versions

09/02 2016

dev-master

9999999-dev

Envato OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  Sources   Download

MIT

The Requires

 

authorization oauth client oauth2 authorisation envato

09/02 2016

v0.1.1-beta.4

0.1.1.0-beta4

Envato OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  Sources   Download

MIT

The Requires

 

authorization oauth client oauth2 authorisation envato

06/02 2016

v0.1.1-beta.3

0.1.1.0-beta3

Envato OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  Sources   Download

MIT

The Requires

 

authorization oauth client oauth2 authorisation envato

05/02 2016

v0.1.1-beta.2

0.1.1.0-beta2

Envato OAuth 2.0 Client Provider for The PHP League OAuth2-Client

  Sources   Download

MIT

The Requires

 

authorization oauth client oauth2 authorisation envato