php使用jwt 验证 firebase的令牌ID

firebase

谷歌的一个开源的数据平台,我们可以自定义我们的信息存储在平台上。最近因为游戏需要对接谷歌,里面获取用户登录信息需要对接到firebase。文档看的迷离八正。
在这里插入图片描述
最关键的点就是使用jwt进行令牌验证的公钥和私钥的获取。最开始以为是合作方提供,后面找遍了各种文档也没有发现公钥和私钥,客户端也不知道自己从SDK获取到的令牌是用什么方式加密的,因此各种尝试,最终在文档中找到了一个固定网址获取到公钥,
https://www.googleapis.com/robot/v1/metadata/x509/[email protected]
这个网址不能直接访问,在外面,可以借助三方工具出去。
在这里插入图片描述
里面对应的两个key有时间限制的,顺序也是在变化, 那我们用哪一个呢,最开始我是循环去使用,后面发现网址中有可能会出现两个或者三个,挨个尝试无疑很浪费时间,看了下
jwt的decode方法,找到了解决方法,原来,可以根据token 反解出 对应的key 。
在这里插入图片描述

在这里插入图片描述

下载php-jwt

composer require firebase/php-jwt

github仓库地址:
php-jwt

验证tokenID

$config=$this->get_online_key();//这里的online_key就是网址中获取到的两个token值,因为不知道是哪个就两个都尝试一下,
$privateKey = $config[0];
$privateKey1 = $config[1];
try {
    
    
     try {
    
    
          $decoded = JWT::decode($sign, $privateKey, ['RS256']);
     } catch (\Exception $exception) {
    
    
                $decoded = JWT::decode($sign, $privateKey1, ['RS256']);
            }
            $array = $this->std_class_object_to_array($decoded);
            if (!empty($array)) {
    
    
                if (isset($array['sub']) && $array['sub'] == $userId) {
    
    
                    return $userId;
                }
            }
        } catch (\Exception $exception) {
    
    
            throw new SdkException("50001", $exception->getMessage(), $exception->getMessage());
        }






猜你喜欢

转载自blog.csdn.net/abc8125/article/details/116060139
今日推荐