PHP code example of john-peterson-g17 / oauth-token-management

1. Go to this page and download the library: Download john-peterson-g17/oauth-token-management library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

john-peterson-g17 / oauth-token-management example snippets


$grant->userId(); // <-- The ID of the user that the grant was issued for (access was granted for)
$grant->accessToken(); // <-- The access token associated with this grant
$grant->refreshToken(); // <-- The refresh token associated with this grant
$grant->expiresIn(); // <-- The number of seconds until the access token associated with this grant expires
$grant->tokenType(); // <-- The token type for the grant. "Bearer" as stated in RFC 6750

use JohnPetersonG17\OAuthTokenManagement\HashingAlgorithm;
use JohnPetersonG17\OAuthTokenManagement\Persistance\Driver;
use JohnPetersonG17\OAuthTokenManagement\Config;
use JohnPetersonG17\OAuthTokenManagement\AuthorizationGate;

// Expects an array of key value objects for configuring the authorization gate. Default values are provided if none are passed in via the array.
$config = new Config(
    [
        'issuer' => 'https://myserver.com',
        'key' => 'someSuperSecretKey1234',
        'hashing_algorithm' => HashingAlgorithm::HS256,
        'access_token_expiration' => 30,
        'refresh_token_expiration' => 60,
        'persistance_driver' => Driver::None
    ]
); 

$gate = new AuthorizationGate($config); // <-- Pass the configuration when creating the AuthorizationGate

$config = new Config(
    [
        'persistance_driver' => 'some_incorrect_driver_type' 
    ]
); // <-- Throws \InvalidArgumentException

use JohnPetersonG17\OAuthTokenManagement\Persistance\Driver;
use JohnPetersonG17\OAuthTokenManagement\Config;

$config = new Config(
    [
        'persistance_driver' => Driver::Redis,
        'redis' => [ // <-- Any options supported by predis can be passed in this array to configure the underlying predis client
            'parameters' => [
                'host' => $this->host,
                'port' => $this->port,
            ]
        ]
    ]
);

$userId = 1234;

// ... Your application code authenticating the user

$grant = $gate->grant($userId); // <-- Authentication successful so lets grant the user some tokens via this method

// ... Your application code sharing the tokens/grant with the client (API Response, etc...)

$accessToken = $grant->accessToken(); // <-- The user must have been issued a grant with an access token previously.

$gate->authorize($accessToken); // <-- Authentication successful so lets grant the user some tokens via this method

// ... Your application code now that the user is authorized access

$accessToken = $grant->accessToken(); // <-- The user must have been issued a grant with an access token previously.

try {
    $gate->authorize($accessToken);
} catch (\JohnPetersonG17\OAuthTokenManagement\Exceptions\TokenExpiredException) {
    // ... Your application code informing the client that the access token has expired
} catch (\JohnPetersonG17\OAuthTokenManagement\Exceptions\NotFoundException) {
    // ... Handle the case where the token does not exist or cannot be found
}

$userId = 1234;

// ... Your application code logging out the user

$grant = $gate->revoke($userId);

$refreshToken = $grant->refreshToken(); // <-- The user must have been issued a grant with an refresh token previously.

$grant = $gate->refresh($refreshToken); // <-- A new grant is issued with a new access token. The refresh token is the same

$userId = 1234;

$grant = $gate->retrieve($userId); // <-- Throws a \JohnPetersonG17\OAuthTokenManagement\Exceptions\NotFoundException if a grant does not exist for the user