bybrand/oauth2-teamwork

Teamwork Provider for PHP League's OAuth 2.0 Client


Keywords
php, authorization, api, Authentication, oauth2, teamwork
License
MIT

Documentation

Teamwork Provider for PHP OAuth 2.0 Client

This package provides Teamwork OAuth 2.0 support for the PHP League's OAuth 2.0 Client. Work with Teamwork API (v2). Initially, this module was used for the integration of Bybrand with Teamwork Desk and is in production.

Full documentation, can be see in Teamwork documentation. Also, you can read the App Login Flow.

Installation

composer require bybrand/oauth2-teamwork

Usage

This is a instruction base to get the token, and in then, to save in your database to future request.

use Bybrand\OAuth2\Client\Provider\Teamwork as ProviderTeamwork;
use Bybrand\OAuth2\Client\OptionProvider\JsonAuthOptionProvider;

$params = $_GET;

$provider = new ProviderTeamwork([
    'clientId'     => 'key-id',
    'clientSecret' => 'secret-key',
    'redirectUri'  => 'your-url-redirect'
]);

// Need to Json authentication and not the default x-www-form-urlencoded
$provider->setOptionProvider(new JsonAuthOptionProvider)

if (!isset($params['code']) or empty($params['code'])) {
    // If we don't have an authorization code then get one
    $authorizationUrl = $provider->getAuthorizationUrl();

    // Get state and store it to the session
    $_SESSION['oauth2state'] = $provider->getState();

    header('Location: '.$authorizationUrl);
    exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($params['state']) || ($params['state'] !== $_SESSION['oauth2state'])) {
    unset($_SESSION['oauth2state']);

    // Set error and redirect.
    echo 'Invalid stage';
} else {
    try {
        // Try to get an access token (using the authorization code grant)
        $token = $provider->getAccessToken('client_credentials', [
            'code' => $params['code']
        ]);
    } catch (\Exception $e) {
        // Error, make redirect or message.
    }

    // Use this to interact with an API on the users behalf
    echo $token->getToken();

    // Basic information about the installation.
    $installation = $token->getValues()['installation'];
}

Please, for more information see the PHP League's general usage examples.

Refreshing a Token

Teamwork tokens do not expire and do not need to be refreshed.

Testing

bash
$ ./vendor/bin/phpunit

License

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