使用 Firebase-PHP 验证 Firebase ID 令牌

Pie*_*her 4 php token firebase firebase-authentication

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

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

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

验证本身工作正常。如果令牌存在,我会得到一个“IDToken”对象。但是我怎样才能再次从该对象中获取 userID 来验证该令牌对于该用户来说是正确的呢?

$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();
}
Run Code Online (Sandbox Code Playgroud)

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

jer*_*mez 6

此处库的维护者 - 文档确实缺少该信息,但现在已包含该信息,并且整个页面已进行了彻底修改:

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

sub您可以从ID 令牌的声明中检索 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);
Run Code Online (Sandbox Code Playgroud)