简体   繁体   English

使用 Firebase-PHP 验证 Firebase ID 令牌

[英]Verify Firebase ID Token with Firebase-PHP

I am using Firebase-Auth to authorize an user on my web-app coded in PHP.我正在使用 Firebase-Auth 在我用 PHP 编码的网络应用上授权用户。 The Authorization itself is made with Javascript, which is executet on an Ajax-Request to verify, that an user is logged in.授权本身是用 Javascript 完成的,它在 Ajax 请求上执行以验证用户是否已登录。

To use the Firebase-Admin at the server I have implemented Firebase-PHP .为了在服务器上使用 Firebase-Admin,我已经实现了Firebase-PHP

On every AJX-Request I now get the logged in user ID and the ID Token, which I want to verify in PHP like it's been written here in the Docs.在每个AJX请求我现在就登录的用户ID和ID令牌,这是我想在PHP验证像它被写在这里的文档。

The verification itself works fine.验证本身工作正常。 If the token exists I get an "IDToken"-Object.如果令牌存在,我会得到一个“IDToken”-Object。 But how can I get the userID out of that object again to verify, that the token is the right one to that user?但是如何再次从该对象中获取用户 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();
}

I couldn't find a method in the documentation or in the classes I searched.我在文档或我搜索的类中找不到方法。

Maintainer of the library here - the documentation was indeed missing that information, but it is now included and the whole page has been overhauled:图书馆的维护者在这里 - 文档确实缺少该信息,但现在包含在内并且整个页面已经过大修:

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

You can retrieve the UID from the sub claim of the 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM