downtownwork/oauth2-pingfederate

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

Installs: 261

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/downtownwork/oauth2-pingfederate

dev-master 2024-07-08 12:43 UTC

This package is auto-updated.

Last update: 2026-01-08 15:53:44 UTC


README

Latest Version Software License Codecov Total Downloads

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

Installation

To install, use composer:

composer require downtownwork/oauth2-pingfederate

Usage

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

Use serverUrl to specify the PingFederate server URL. You can lookup the correct value from the PingFederate client installer JSON under server-url, eg. http://localhost:9031.

Authorization Code Flow

$provider = new DownTownWork\OAuth2\Client\Provider\PingFederate([
    'serverUrl'    => '{pingfederate-server-url}',
    'clientId'     => '{pingfederate-client-id}',
    'clientSecret' => '{pingfederate-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();
    header('Location: '.$authUrl);
    exit;

// 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, make sure HTTP sessions are enabled.');

} else {

    // Try to get an access token (using the authorization coe grant)
    try {
        $token = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);
    } catch (Exception $e) {
        exit('Failed to get access token: '.$e->getMessage());
    }

    // 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 user's details
        $user = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $user->getName());

    } catch (Exception $e) {
        exit('Failed to get resource owner: '.$e->getMessage());
    }

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

Refreshing a Token

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

$token = $provider->getAccessToken('refresh_token', ['refresh_token' => $token->getRefreshToken()]);