【问题标题】:How to request user info with openid connect如何使用 openid connect 请求用户信息
【发布时间】:2022-06-11 00:14:47
【问题描述】:

我正在尝试通过我们的 OpenID Connect Keycloak 服务器请求用户信息。当请求访问令牌时,一切都会好起来的。但只要我请求用户信息,我就会收到错误 400。

require __DIR__ . '/vendor/autoload.php';

use Jumbojett\OpenIDConnectClient;

    $username = $_REQUEST["Username"];
    $password = $_REQUEST["Password"];

$oidc = new OpenIDConnectClient(
    'xxx',
    'yyy',
    'zzz'
);
$oidc->providerConfigParam(array('userinfo_endpoint'=>'xxx/userinfo_endpoint'));

$oidc->addScope('openid');
$oidc->addScope('roles');
$oidc->addScope('profile');

//Add username and password
$oidc->addAuthParam(array('username'=>$username));
$oidc->addAuthParam(array('password'=>$password));

//Perform the auth and return the token (to validate check if the access_token property is there and a valid JWT) :
session_start();
$_SESSION['access_token'] = $oidc->requestResourceOwnerToken(TRUE)->access_token;
// $token = $oidc->requestResourceOwnerToken(TRUE)->access_token;

try {
    $name = $oidc->requestUserInfo();
} catch (\Jumbojett\OpenIDConnectClientException $e) {
    echo $e;
}

这给出了以下错误代码:

 The communication to retrieve user data has failed with status code 400

我知道这与格式或类似问题有关,但我无法弄清楚究竟是什么。

有人可以指点我大致的方向吗?

【问题讨论】:

    标签: php keycloak openid-connect http-status-code-400


    【解决方案1】:

    我的猜测是您的用户信息端点xxx/userinfo_endpoint 不是正确的端点:

    array('userinfo_endpoint'=>'xxx/userinfo_endpoint')
    

    访问您的 Keycloak 发现网址,例如https://<keycloak-host>/auth/realms/<realm>/.well-known/openid-configuration 并从那里获取正确的用户信息端点。

    【讨论】:

    • 嗯,我从那个配置中得到了我的端点。我直接从那里复制了它,甚至在没有设置该参数的情况下尝试了它。因为据我了解,它应该能够从颁发者那里配置自己的正确端点。
    【解决方案2】:

    解决方案:

    好的,所以我会回答我自己的问题,以防有人遇到和我一样的错误。

    如果您输入发行人,通常所有端点都会通过https://<keycloak-host>/auth/realms/<realm>/.well-known/openid-configuration 自动找到,或者至少我是这么认为的。

    原来我错过了jwks_uri端点。

    所以我进入了端点,瞧。我能够请求所有信息。

    <?php
    include 'session.php';
    require __DIR__ . '/vendor/autoload.php';
    
    use Jumbojett\OpenIDConnectClient;
    
        $oidc = new OpenIDConnectClient(
            'https://<keycloak-host>/auth/realms/Infra',
            'xxx',
            'yyy'
        );
        $oidc->setRedirectURL('https://<redirect-url>');
        $oidc->providerConfigParam(array('authorization_endpoint'=>'https://<keycloak-host>/auth/realms/Infra/protocol/openid-connect/auth'));
        $oidc->providerConfigParam(array('userinfo_endpoint'=>'https://<keycloak-host>/auth/realms/Infra/protocol/openid-connect/userinfo'));
        $oidc->providerConfigParam(array('token_endpoint'=>'https://<keycloak-host>/auth/realms/Infra/protocol/openid-connect/token'));
        $oidc->providerConfigParam(array('jwks_uri'=>'https://<keycloak-host>/auth/realms/Infra/protocol/openid-connect/certs'));
        $oidc->addScope('email');
        $oidc->addScope('profile');
        $oidc->addScope('openid');
        $oidc->getAuthParams();    
        
        
        $_SESSION['oidc'] = $oidc;
        $oidc->authenticate();
        
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 2018-12-28
      • 2019-01-09
      • 2012-06-07
      相关资源
      最近更新 更多