learningpool/oauth2-signonotron

PHP League OAuth2-Client 的 Sign-On-O-Tron OAuth 2.0 客户端提供者

dev-master 2016-09-15 10:08 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:31:39 UTC


README

Build Status Latest Stable Version

本包为 PHP League 的 OAuth 2.0 客户端 提供了 Sign-On-O-Tron OAuth 2.0 支持。

安装

要安装,请使用 composer

composer require rlafferty05/oauth2-signonotron

用法

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

Sign-On-O-Tron URL

与其他许多 SSO 提供者不同,Sign-On-O-Tron 没有一个中央位置。不同的组织可以设置自己的具有不同 URL 的 Sign-On-O-Tron 连接,因此我们需要包含正确的 URL。这可以通过将 src/Provider 文件夹中的 signonotron.ini.dist 重命名为 signonotron.ini 并在 ini 文件中更改 site_url 变量以指向正确位置来实现。

授权代码流

$provider = new Learningpool\OAuth2\Client\Provider\Signonotron([
    'clientId'          => '{signonotron-client-id}',
    'clientSecret'      => '{signonotron-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->state;
    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 {

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

    // 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
        $userDetails = $provider->getUserDetails($token);

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

    } catch (Exception $e) {

        // Failed to get user details
        exit('Oh dear...');
    }

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

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件