Skip to content

geekdevs/oauth2-cronofy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cronofy Provider for OAuth 2.0 Client

Build Status Latest Stable Version

This package provides Cronofy Calendar 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 7.4

Installation

Add the following to your composer.json file.

{
    "require": {
        "geekdevs/oauth2-cronofy": "^2.0"
    }
}

Usage

Authorization Code Flow

session_start();

$provider = new Geekdevs\OAuth2\Client\Provider\Cronofy([
    'clientId'          => '{cronofy-app-id}',
    'clientSecret'      => '{cronofy-app-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([
        'scope' => ['read_account', '...', '...'],
    ]);
    $_SESSION['oauth2state'] = $provider->getState();
    
    echo '<a href="'.$authUrl.'">Log in with Cronofy!</a>';
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    echo 'Invalid state.';
    exit;

}

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

// Optional: Now you have a token you can look up a users profile data
try {

    // We got an access token, let's now get the account's details
    $account = $provider->getResourceOwner($token);

    // Use these details to create a new profile
    printf('Hello %s!', $account->getName());
    
    echo '<pre>';
    var_dump($account);
    # object(League\OAuth2\Client\Provider\CronofyAccount)#10 (1) { ...
    echo '</pre>';

} catch (Exception $e) {

    // Failed to get account details
    exit('Oh dear...');
}

echo '<pre>';
// Use this to interact with an API on the users behalf
var_dump($token->getToken());
# string(217) "CAADAppfn3msBAI7tZBLWg...

// Number of seconds until the access token will expire, and need refreshing
var_dump($token->getExpires());
# int(1436825866)
echo '</pre>';

The CronofyAccount Entity

When using the getResourceOwner() method to obtain the account details, it will be returned as a CronofyAccount entity.

$account = $provider->getResourceOwner($token);

$id = $account->getId();
var_dump($id);
# string(1) "acc_567236000909002"

$name = $account->getName();
var_dump($name);
# string(15) "Pavel Dubinin"

$email = $account->getEmail();
var_dump($email);
# string(15) "geekevs@gmail.com"

$timezone = $account->getDefaultTimezone();
var_dump($timezone);
# string(15) "Europe/London"

You can also get all the data from the Account node as a plain-old PHP array with toArray().

$accountData = $account->toArray();

Testing

$ ./vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Cronofy Provider for the OAuth 2.0 Client

Resources

License

Stars

Watchers

Forks

Packages

No packages published