ejimba/oauth2-dhis2

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

1.0.1 2015-11-22 13:41 UTC

This package is auto-updated.

Last update: 2021-09-28 16:09:09 UTC


README

This package provides DHIS2 OAuth 2.0 support for the PHP League's OAuth 2.0 Client.

Installation

To install, use composer:

composer require ejimba/oauth2-dhis2

Usage

Usage is the same as The League's OAuth client, using Ejimba\OAuth2\Client\Provider\DHIS2 as the provider.

Authorization Code Flow

$provider = new Ejimba\OAuth2\Client\Provider\DHIS2(
    array(
        'dhis2ServerUri' => 'https://apps.dhis2.org/dev',
        'clientId' => 'YOUR_CLIENT_ID',
        'clientSecret' => 'YOUR_CLIENT_SECRET',
        'redirectUri' => 'http://your-redirect-uri'
    )
);

if (isset($_GET['code']) && $_GET['code']) {
    
    // Fetch token from server
    $token = $this->provider->getAccessToken('authorizaton_code', array(
        'code' => $_GET['code']
    ));

    // Returns an object of the logged in user
    $resourceOwner = $provider->getResourceOwner($token);
    
    $id = $resourceOwner->getId();
    $email = $resourceOwner->getEmail();
    $name = $resourceOwner->getName();
    $displayName = $resourceOwner->getDisplayName();
    $firstName = $resourceOwner->getFirstName();
    $surname = $resourceOwner->getSurname();
    $phoneNumber = $resourceOwner->getPhoneNumber();

    // Return an array of user object
    $user = $resourceOwner->toArray();
    
}

Refreshing A Token

$provider = new Ejimba\OAuth2\Client\Provider\DHIS2([
    'dhis2ServerUri' => 'https://apps.dhis2.org/dev',
    'clientId' => 'YOUR_CLIENT_ID',
    'clientSecret' => 'YOUR_CLIENT_SECRET',
    'redirectUri' => 'http://your-redirect-uri'
]);

$grant = new \League\OAuth2\Client\Grant\RefreshToken();
$token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);