如何在后端检查 auth0 google JWT-token 的真实性?

问题描述 投票:0回答:1

我的 Android/IOS 应用程序访问 Auth0 Google 服务器,并在用户许可的情况下接收 JWT 令牌。应用程序将其传递到我的后端,在那里我将其解析为标头、有效负载、签名,并且仅检查有效负载是否存在 asp 字段,该字段应等于 clientID。

$clientId = '....apps.googleusercontent.com';
// $idToken we receive from the application
    
$data = explode('.', $idToken);
list($header, $payload, $signature) = array_map('base64_decode', $data);

// this is the only authentication check
$jwt = json_decode($payload, true);
if ($jwt['azp'] !== $clientId) { die('Invalid client ID ');}

// checking token expiration date
$currentTime = time();
if ($jwt['exp'] < $currentTime) {  die('Token has expired'); } 

$client_info = $jwt;

我知道该方案很容易受到攻击,因为如果您知道 clientID,您就可以使用虚假用户数据传递虚假 JWT 令牌。

我认为我应该检查标题和签名,但我不明白如何检查。请指教!

auth0
1个回答
0
投票

检查文档:https://auth0.com/docs/libraries/auth0-php

简而言之,你需要做你正在做的所有事情,然后你需要询问 auth0“这个令牌上的签名是否有效” - 即我刚刚验证的所有声明实际上都可以信任吗?

另外,因为我注意到,在大多数情况下,您不应该将 ID 令牌传递给您的 BE,您应该使用访问令牌。

© www.soinside.com 2019 - 2024. All rights reserved.