cilogon/oauth2-cilogon

CILogon OAuth 2.0 客户端提供程序,用于 PHP League OAuth2-Client

1.1.6 2021-04-13 11:09 UTC

This package is auto-updated.

Last update: 2024-09-08 23:12:47 UTC


README

License Travis Scrutinizer Coveralls

本软件包为 PHP League 的 OAuth 2.0 客户端提供 CILogon OAuth 2.0 支持。

CILogon 促进了 CyberInfrastructure (CI) 的联合认证。有关更多信息,请参阅 http://www.cilogon.org/oidc 。请注意,CILogon 主要由 NSF 资助的项目使用。所有客户端注册均经过人工审查和批准。

本软件包符合 PSR-1PSR-2PSR-4。如果您发现符合性疏漏,请通过拉取请求发送补丁。

要求

以下版本的 PHP 受支持。

  • PHP 7.1
  • PHP 7.2
  • PHP 7.3
  • PHP 7.4
  • PHP 8.0
  • PHP 8.1

安装

要安装,请使用 composer

composer require cilogon/oauth2-cilogon

使用方法

授权码流

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

if (!empty($_GET['error'])) {

    // Got an error, probably user denied access
    exit('Got error: ' . $_GET['error'] . 
         'Description: ' . $GET['error_description']);

} elseif (empty($_GET['code'])) {

    // If we don't have an authorization code then get one with all 
    // possible CILogon-specific scopes.
    $authUrl = $provider->getAuthorizationUrl([
        'scope' => ['openid','email','profile','org.cilogon.userinfo']
    ]);
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    // Check given state against previously stored one to mitigate CSRF attack
    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

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

        // Print out the access token, which can be used in 
        // authenticated requests against the service provider's API.
        echo '<xmp>' . "\n";
        echo 'Token                  : ' . $token->getToken() . "\n";
        $expires = $token->getExpires();
        if (!is_null($expires)) {
            echo 'Expires                : ' . $token->getExpires();
            echo ($token->hasExpired() ? ' (expired)' : ' (active)') . "\n";
        }
        echo '</xmp>' . "\n";

        // Using the access token, get the user's details
        $user = $provider->getResourceOwner($token);

        echo '<xmp>' . "\n";
        echo 'User ID                : ' . $user->getId() . "\n";
        echo 'First name             : ' . $user->getGivenName() . "\n";   // or getFirstName()
        echo 'Last name              : ' . $user->getFamilyName() . "\n";  // or getLastName()
        echo 'Full name              : ' . $user->getName() . "\n";
        echo 'Email                  : ' . $user->getEmail() . "\n";
        echo 'eduPersonPrincipalName : ' . $user->getEPPN() . "\n";
        echo 'eduPersonTargetedId    : ' . $user->getEPTID() . "\n";
        echo 'IdP entityId           : ' . $user->getIdP() . "\n";
        echo 'IdP Display Name       : ' . $user->getIdPName() . "\n";
        echo 'Org Unit               : ' . $user->getOU() . "\n";
        echo 'Affiliation            : ' . $user->getAffiliation() . "\n";
        echo '</xmp>';

    } catch (Exception $e) {

        // Failed to get access token or user details
        exit('Something went wrong: ' . $e->getMessage());

    }
}

管理作用域

在创建您的 CILogon 授权 URL 时,您可以指定应用程序可能授权的状态和作用域。

$options = [
    'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
    'scope' => ['openid','email','profile','org.cilogon.userinfo']
];

$authorizationUrl = $provider->getAuthorizationUrl($options);

如果没有定义,提供程序将使用内部默认值。

在编写本文档时,以下作用域可用。

  • openid - 必需/默认 - 提供用户的 CILogon 特定标识符
  • email - 提供用户的电子邮件地址
  • profile - 提供用户的名字(给定的、姓氏和显示名称,如果可用)
  • org.cilogon.userinfo - 提供身份提供者 SAML 属性,例如 ePPN (eduPersonPrincipalName)、ePTID (eduPersonTargetedID)、eduPersonScopedAffiliation、ou (organizationalUnitName)

另外还有两个 CILogon 特定选项 可用。

  • selected_idp - 用户预先选择的身份提供者的 SAML entityId。如果提供,CILogon UI 将向用户展示此 IdP 并请求同意释放信息。请参阅 https://cilogon.org/include/idplist.xml 了解 CILogon 支持的身份提供者列表(标记为 <Whitelisted>)。
  • skin - 一个预定义的自定义 CILogon 界面皮肤,用于更改 CILogon 网站的外观。请联系 help@cilogon.org 请求自定义皮肤。

示例

$options = [
    'scope' => ['openid','email','profile','org.cilogon.userinfo'],
    'selected_idp' => 'urn:mace:incommon:uiuc.edu', // UIUC
    'skin' => 'globusonline' // For globus.org
];

$authorizationUrl = $provider->getAuthorizationUrl($options);

使用 "test" 或 "dev" 服务器

通常,您会使用生产服务器 https://cilogon.org 。但是,您可以在创建提供程序时指定 'server' 参数以使用 "test" 服务器 https://test.cilogon.org 或 "dev" 服务器 https://dev.cilogon.org

// Use the "test" server https://test.cilogon.org

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

刷新令牌

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

许可证

伊利诺伊大学/国家超级计算应用中心开源许可证 (NCSA)。有关更多信息,请参阅 许可证文件