首页 > 解决方案 > 如何使用 oauth/jwt 保护我的 http 触发的 GCP 功能?

问题描述

使用 AWS Lambda 和 API Gateway,我可以部署一个使用 http 公开的函数。

使用 Cognito 和 API Gateway,我可以使用 oauth2 和 jwt 令牌保护函数。


使用 GCP 函数和 http-trigger 选项,我可以部署一个使用 http 公开的函数。

是否有一个“GCP Cognito 替代方案”我可以使用 oauth2 和 jwt 令牌来保护我的功能?


我尝试过使用“身份平台”。我可以通过以下方式获取(不是 jwt)令牌: https ://accounts.google.com/o/oauth2/v2/auth?client_id =[my client id]&redirect_uri=[my redirect uri]&response_type=token&scope=openid

但这给了我一个 401: curl -L -v -XGET --header "Authorization: Bearer [the not jwt token]" https://europe-west2-[my project].cloudfunctions.net/my-hello-function "

谢谢 !

标签: google-cloud-platformoauth-2.0jwtgoogle-cloud-functions

解决方案


您可以使用IAM 角色管理对 HTTP 云函数的访问。您需要将角色分配给服务帐户,并让调用者在 Authorization 标头中向函数提供 oauth 凭据。roles/cloudfunctions.invoker

最相关的演练可以在这里阅读。


推荐阅读