intercom/oauth2-intercom

PHP League OAuth2-Client 的 Intercom OAuth 2.0 客户端提供者

2.0.1 2020-01-23 11:55 UTC

This package is auto-updated.

Last update: 2024-09-21 20:50:50 UTC


README

Build Status

此包为 PHP League 的 OAuth 2.0 客户端 提供Intercom OAuth 2.0 支持。

安装

要安装,请使用 composer

composer require intercom/oauth2-intercom

用法

用法与 The League 的 OAuth 客户端相同,使用 \Intercom\OAuth2\Client\Provider\Intercom 作为提供者。

授权码流程

$provider = new Intercom\OAuth2\Client\Provider\Intercom([
    'clientId'          => '{intercom-client-id}',
    'clientSecret'      => '{intercom-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');

} else {

    // Get access token using the authorization code grant
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    // Optional: Now you have 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 user details');
    }

    echo $token;

}

默认情况下,Intercom 提供者拒绝具有未验证电子邮件地址的用户。在这种情况下,用户信息不会被填充。要禁用此检查,请在配置中添加 verifyEmail 设置为 false。

$provider = new Intercom\OAuth2\Client\Provider\Intercom([
    'clientId'          => '{intercom-client-id}',
    'clientSecret'      => '{intercom-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
    'verifyEmail'       => false
]);

刷新令牌

Intercom 的 OAuth 实现不使用刷新令牌。访问令牌在用户手动撤销访问或应用自身取消授权之前有效。

测试

$ ./vendor/bin/phpunit

许可

The Apache 2 License. 请参阅许可文件以获取更多信息。