mamaclub/oauth2

Mamaclub OAuth2 SDK for PHP

This package's canonical repository appears to be gone and the package has been frozen as a result.

dev-master / 1.x-dev 2019-05-21 09:13 UTC

This package is auto-updated.

Last update: 2019-10-21 10:09:08 UTC


README

This package makes it simple to integrate your application with OAuth 2.0 service providers.

Gitter Chat Source Code Latest Version Software License Build Status Scrutinizer Coverage Status Total Downloads

We are all used to seeing those "Connect with Facebook/Google/etc." buttons around the internet, and social network integration is an important feature of most web applications these days. Many of these sites use an authentication and authorization standard called OAuth 2.0 (RFC 6749).

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. If you're interesting in contributing to this library, please take a look at our contributing guidelines.

Requirements

The following versions of PHP are supported.

  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • PHP 7.2
  • PHP 7.3

Installation

The Facebook PHP SDK can be installed with Composer. Run this command:

composer require mamaclub/oauth2

Usage

Simple GET example of a user's profile.

$mamaclub = new MamaclubOAuth([
    'clientId' => 'your client id',
    'clientSecret' => 'your client secret',
    'redirectUri' => 'your redirect uri'
]);


// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $mamaclub->getAuthorizationUrl(['state' => 'mmc_login']);

    // Redirect the user to the authorization URL.
    header('Location: ' . $authorizationUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} else {

    try {

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

        // We have an access token, which we may use in authenticated
        // requests against the service provider's API.
        echo 'Access Token: ' . $accessToken->getToken() . "<br>";
        echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
        echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
        echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";

        // Using the access token, we may look up details about the
        // resource owner.
        $resourceOwner = $mamaclub->getResourceOwner($accessToken);


    } catch (\Mamaclub\Exception\Exception $e) {

        // Failed to get the access token or user details.
        exit($e->getMessage());

    }

}

Refreshing a Token

$newAccessToken = $mamaclub->getAccessToken('refresh_token', [
    'refresh_token' => $accessToken->getRefreshToken()
]);