承接 downtownwork/oauth2-pingfederate 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

downtownwork/oauth2-pingfederate

Composer 安装命令:

composer require downtownwork/oauth2-pingfederate

包简介

PingFederate OAuth 2.0 Client Provider for The PHP League OAuth2-Client

README 文档

README

Latest Version Software License Codecov Total Downloads

This package provides PingFederate OAuth 2.0 support for the PHP League's OAuth 2.0 Client.

Installation

To install, use composer:

composer require downtownwork/oauth2-pingfederate

Usage

Usage is the same as The League's OAuth client, using \DownTownWork\OAuth2\Client\Provider\PingFederate as the provider.

Use serverUrl to specify the PingFederate server URL. You can lookup the correct value from the PingFederate client installer JSON under server-url, eg. http://localhost:9031.

Authorization Code Flow

$provider = new DownTownWork\OAuth2\Client\Provider\PingFederate([
    'serverUrl'    => '{pingfederate-server-url}',
    'clientId'     => '{pingfederate-client-id}',
    'clientSecret' => '{pingfederate-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, make sure HTTP sessions are enabled.');

} else {

    // Try to get an access token (using the authorization coe grant)
    try {
        $token = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);
    } catch (Exception $e) {
        exit('Failed to get access token: '.$e->getMessage());
    }

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

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

    } catch (Exception $e) {
        exit('Failed to get resource owner: '.$e->getMessage());
    }

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

Refreshing a Token

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

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

统计信息

  • 总下载量: 260
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-22