如何配置 testlink 的 oauth 对接

1. 在 目录 /opt/lampp/htdocs/testlink 创建 custom_config.inc.php , 文件内容如下

<?php                                                                                                                                                                                                       
# ngrok http -region eu -subdomain=testlink 80
$tlCfg->OAuthServers[1]['oauth_enabled'] = true;
$tlCfg->OAuthServers[1]['oauth_name'] = 'sso';
$tlCfg->OAuthServers[1]['oauth_icon'] = 'github.png';

$tlCfg->OAuthServers[1]['oauth_client_id'] = 'testlink';
$tlCfg->OAuthServers[1]['oauth_client_secret'] = 'za1qcd8myesfsfwf7qy8hp8rj3';

// Can be authorization_code (by default), client_credentials or password
$tlCfg->OAuthServers[1]['oauth_grant_type'] = 'authorization_code';
$tlCfg->OAuthServers[1]['oauth_url'] = 'http://oauth2.in.zhihu.com/oauth/authorize';

$tlCfg->OAuthServers[1]['token_url'] = 'http://oauth2.in.yourcompany.com/oauth/token';
$tlCfg->OAuthServers[1]['oauth_force_single'] = false;
$tlCfg->OAuthServers[1]['oauth_profile'] = 'http://oauth2.in.zhihu.com/oauth/profile';
$tlCfg->OAuthServers[1]['oauth_scope'] = 'all';

// ngrok http -subdomain=testlink 8888
$tlCfg->OAuthServers[1]['redirect_uri'] = 'http://testenv1.dev.rack.yourcompany.com/testlink/login.php?oauth=sso';

2. 在 目录 /opt/lampp/htdocs/testlink/lib/functions/oauth_providers 下,创建  sso.php, 文件内容如下


<?php
/**
 * TestLink Open Source Project - http://testlink.sourceforge.net/
 * This script is distributed under the GNU General Public License 2 or later.
 *
 * @filesource  github.php
 *
 * Github OAUTH API (authentication)
 *
 * @internal revisions
 * @since 1.9.17
 *
 */

// Get token
function oauth_get_token($authCfg, $code) {
    $result = new stdClass();
    $result->status = array('status' => tl::OK, 'msg' => null);

    // Params to get token
    $oauthParams = array(
        'code'          => $code,
        'client_id'     => $authCfg['oauth_client_id'],
        'client_secret' => $authCfg['oauth_client_secret'],
        'grant_type'    => $authCfg['oauth_grant_type']
    );
    $oauthParams['redirect_uri'] = $authCfg['redirect_uri'];
    if( isset($_SERVER['HTTPS']) ) {
        $oauthParams['redirect_uri'] =
            str_replace('http://', 'https://', $oauthParams['redirect_uri']);
    }

    $curlAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1';
    $curlContentType = array('Content-Type: application/x-www-form-urlencoded','Accept: application/json');

    // Step #1 - Get the token
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $authCfg['token_url']);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($oauthParams));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_COOKIESESSION, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    //exit();
    $result_curl = curl_exec($curl);
    //print($curl);
    print($result_curl);
    if( $result_curl === false ) {
        echo 'Curl error: ' . curl_error($curl);
        echo '<pre>';
        die();
    }
    curl_close($curl);
    $tokenInfo = json_decode($result_curl);

    // If token is received start session
    if (isset($tokenInfo->access_token)) {
        $oauthParams['access_token'] = $tokenInfo->access_token;

        $queryString = http_build_query($tokenInfo);
        $targetURL = array();
        $targetURL['profile'] = $authCfg['oauth_profile'];
        // Get User
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $targetURL['profile']);
        curl_setopt($curl, CURLOPT_USERAGENT, $curlAgent);
        $headerParams = 'Authorization: bearer ' . $oauthParams['access_token'];
        curl_setopt($curl, CURLOPT_HTTPHEADER, array($headerParams));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result_curl = curl_exec($curl);
//        print("result_curl\n");
//        print($result_curl);
        $userInfo = json_decode($result_curl, true);
        curl_close($curl);

        if (!isset($userInfo['user'])) {
            $result->status['msg'] = 'User ID is empty';
            $result->status['status'] = tl::ERROR;
        }

        // Get email


        $result->options = new stdClass();
        $result->options->givenName = $userInfo['user']['name'];
        $result->options->familyName = $userInfo['user']['name'];
        $result->options->user = $userInfo['user']['email'];
        $result->options->auth = 'oauth';

    } else {
        $result->status['msg'] = 'An error occurred during getting token' . $tokenInfo->error;
//        print($tokenInfo);
        $result->status['status'] = tl::ERROR;
    }

    return $result;
                                                                                                                                                                            59,1           69%

猜你喜欢

转载自blog.csdn.net/countofdane/article/details/88954641