【问题标题】:Verify Firebase ID Token with Firebase-PHP使用 Firebase-PHP 验证 Firebase ID 令牌
【发布时间】:2018-07-23 11:58:09
【问题描述】:

我正在使用 Firebase-Auth 在我的用 PHP 编码的网络应用上授权用户。授权本身是使用 Javascript 进行的,它在 Ajax-Request 上执行以验证用户是否已登录。

要在服务器上使用 Firebase-Admin,我已经实现了 Firebase-PHP

现在,在每个 AJX 请求中,我都会获得登录的用户 ID 和 ID 令牌,我想在 PHP 中验证它们,就像在文档中写的 here 一样。

验证本身工作正常。如果令牌存在,我会得到一个“IDToken”-对象。但是我怎样才能再次从该对象中获取用户 ID 以验证该令牌是否适合该用户?

$idTokenString = '...';

try {
    $verifiedIdToken = $firebase->getAuth()->verifyIdToken($idTokenString);
    // This is the Token-Object where I cant't find the uid-get-Function
} catch (InvalidIdToken $e) {
    echo $e->getMessage();
}

我在文档或搜索的类中找不到方法。

【问题讨论】:

    标签: php firebase firebase-authentication token


    【解决方案1】:

    这里是库的维护者 - 文档确实缺少该信息,但现在已包含该信息,并且整个页面已被大修:

    https://firebase-php.readthedocs.io/en/latest/authentication.html#verify-a-firebase-id-token

    您可以从 ID 令牌的 sub 声明中检索 UID:

    try {
        $verifiedIdToken = $firebase->getAuth()->verifyIdToken($idToken);
    } catch (InvalidToken $e) {
        echo $e->getMessage();
    }
        
    $uid = $verifiedIdToken->getClaim('sub'); // lcobucci/jwt:^3.0
    // or 
    $uid = $verifiedIdToken->claims()->get('sub'); // lcobucci/jwt:^4.0
    
    $user = $firebase->getAuth()->getUser($uid);
    

    【讨论】:

    • 我的 $verifiedIdToken 已成功验证,但当 getClaim('sub') 被调用时,我正在调用未定义的方法 Lcobucci\JWT\Token\Plain::getClaim() 我正在使用“lcobucci /jwt": "^4.0" in composer json 请问我知道应该是什么问题,google了很多但没有找到解决方案
    • @ShriramPanchal 我用lcobucci/jwt:^4.0 的用法和过时的文档更新了答案,谢谢你告诉我!
    • 感谢更新保存了很多,如果可能的话,SDK初始化中的一个更新,如 $factory = (new Factory)->withServiceAccount('google-service-account.json');将为即将到来的用户节省大量搜索工作
    猜你喜欢
    • 2017-01-24
    • 2017-06-25
    • 1970-01-01
    • 2019-06-28
    • 2021-12-04
    • 2019-04-09
    • 2018-05-28
    • 2021-04-02
    • 2021-05-04
    相关资源
    最近更新 更多