php - 如何在 php 中向 Google RISC API 注册端点
问题描述
我一直在阅读并尝试实施使用跨帐户保护保护用户帐户的文档中的指示
到目前为止我所做的如下:
JWT::$leeway = 60;
$key = file_get_contents('location.json');
$time = time();
$payload = [
"iss" => "account email",
"sub" => "account email",
"aud" => "https://risc.googleapis.com/google.identity.risc.v1beta.RiscManagementService",
"iat" => $time,
"exp" => $time + 3600,
];
/**
* IMPORTANT:
* You must specify supported algorithms for your application. See
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
* for a list of spec-compliant algorithms.
*/
$jwt = JWT::encode($payload, $key);
$decoded = JWT::decode($jwt, $key, ['HS256']);
print_r($jwt);
print_r($decoded);
$client = new Client();
try {
$request = $client->post('https://risc.googleapis.com/v1beta/stream:update', [
'headers' => [
'Authorization' => 'Bearer ' . $jwt,
'Accept' => 'application/json',
],
'form_params' => [
'delivery' => [
'delivery_method' => 'https://schemas.openid.net/secevent/risc/delivery-method/push',
'url' => 'https://test.myapp.com/webhooks/google',
],
'events_requested' => [
'https://schemas.openid.net/secevent/oauth/event-type/tokens-revoked',
],
],
]);
$response = $request->getBody();
dd($response);
} catch (ClientException $exception) {
dd($exception->getResponse()->getBody()->getContents());
}
我面临的问题:
- 从我在文档中阅读的内容中,我不太了解如何使用 JWT,我在实现中做错了什么?
- 这些示例在 JAVA 中,但我在 php 中需要它,我尝试阅读 JAVA 代码,但不明白几件事的来源。
- 从我阅读的内容来看,我认为我将无法在本地环境中测试这些事件?那就是这些事件要在本地触发?或者像 ultrahook 这样的服务会允许这样做吗?否则我将不得不直接在服务器上测试端点。
我从上面的代码中得到的错误是Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
尝试下面建议的编码RS256
会给我一个错误UnexpectedValueException: Algorithm not allowed
,我相信我确实缺乏有关 JWT 的必要知识并且在那里做错了什么。
解决方案
我还在研究如何通过此链接https://developers.google.com/identity/protocols/risc#java_1进行此跨帐户保护我假设您正在谈论这部分(生成授权令牌)https: //developers.google.com/identity/protocols/risc#auth_token
我正在使用 php,但查看页面中的 java 代码,它在您的代码中使用 RS256 而不是 HS256。如果您使用 php,那么您可以尝试使用 firebase php,它们有一个简单的 JWT 类可供您使用。https://github.com/firebase/php-jwt您可以只使用示例并将有效负载替换为您的,然后更改为 RS256。这就是我要尝试的方法,我可以让你知道它是否有效。
推荐阅读
- python - 如何使用 MRJob 在 Hadoop 集群中运行 Python 库?
- ios - FIRInstanceID 已弃用,正在寻找可以在没有 GoogleService-Info.plist 的情况下工作的替代方案
- openstack-heat - 有没有办法根据 HOT 中的输入参数计算资源价值?
- javascript - 自适应模式为真时可滚动不起作用
- c++ - 排列数组中的所有负数和正数时如何避免交换负数
- docker - docker compose exec 在 Github Actions 中意外地将非零退出代码回显到 stderr
- jquery - 使用 JQuery 对话框时将一个特定元素置于顶部
- dacpac - DACPAC 动态变量
- css - 如何从 vue 写入 className 并将文件更快地响应到 less/css 文件?
- swift - 基于枚举返回泛型类型