javis/password-grant-client

A PHP library to easily OAuth2 to your APIs using the Password Grant

v1.1.3 2020-11-04 19:07 UTC

This package is auto-updated.

Last update: 2024-04-05 02:25:12 UTC


README

Build Status Latest Stable Version Total Downloads Quality Score License

A PHP library to easily authorize OAuth 2.0 APIs using the Password Grant. This is a wrapper of the amazing The League of Extraordinary Packages OAuth2 Client, but it automates the process of storing the token in session and refreshing it when necessary

Requirements

  • PHP >= 5.5

Installation

composer require javis/password-grant-client

Usage

Configuring the client

Create a Client Provider with the configuration to the endpoints and then provide the instance to the OAuth2 Client.

$provider = new \League\OAuth2\Client\Provider\GenericProvider([
    'clientId'                => 'demoapp',    // The client ID assigned to you by the provider
    'clientSecret'            => 'demopass',   // The client password assigned to you by the provider
    'urlAccessToken'          => 'http://brentertainment.com/oauth2/lockdin/token'
]);

$client = new \Javis\OAuth2\PasswordGrantClient($provider);

Requesting an access token

When requesting the token, the Client will automatically check for if it was already stored in Session or if it needs to refresh it, and perform all required operations before returning it.

$return = $client->getAccessToken('username', 'password');
echo $token->getToken();

Forgetting stored token

$client->forgetToken();