我正在努力实现一个Node.js webapp,以部署在GCP App Engine上。
在Node.js Bookshelf App示例之后,我确实设法使用passport-google-oauth20
实现基本用户身份验证流程并检索基本配置文件信息。我基本上只是摆脱了我的目的不需要的东西
我的自定义代码位于:gist.github.com/vdenotaris/3a6dcd713e4c3ee3a973aa00cf0a45b0。
但是,我现在想要使用GCP Cloud Storage API来检索具有已记录标识的给定存储桶中的所有存储对象。这可以通过以下方式实现:
关于post-auth处理程序,文档说:
获取凭据后,您可以存储有关该用户的信息。 Passport.js会自动将用户序列化到会话中。在用户的信息在会话中之后,您可以创建一些中间件功能,以便更轻松地使用身份验证。
// Middleware that requires the user to be logged in. If the user is not logged
// in, it will redirect the user to authorize the application and then return
// them to the original URL they requested.
function authRequired (req, res, next) {
if (!req.user) {
req.session.oauth2return = req.originalUrl;
return res.redirect('/auth/login');
}
next();
}
// Middleware that exposes the user's profile as well as login/logout URLs to
// any templates. These are available as `profile`, `login`, and `logout`.
function addTemplateVariables (req, res, next) {
res.locals.profile = req.user;
res.locals.login = `/auth/login?return=${encodeURIComponent(req.originalUrl)}`;
res.locals.logout = `/auth/logout?return=${encodeURIComponent(req.originalUrl)}`;
next();
}
但我没有看到令牌的存储位置,如何检索它以及如何使用它来使用Web服务(在我的情况下,GCP存储)。
我根本不是一个node.js专家,所以对此更加清晰一点会很好:有人可以解释一下如何使用记录的用户凭据(因此IAM / ACL权限)继续使用REST API吗?
如果您想通过使用OAuth获取的令牌访问云存储,当应用程序需要用户数据时,它将提示同意屏幕,要求用户授权应用获取他们的一些数据。如果用户批准,则生成访问令牌,该令牌可以附加到用户的请求。这更好地解释了here。
如果您计划在Google App Engine中运行您的应用程序,将会有一个使用必要的身份验证信息准备的服务帐户,因此无需进一步设置。您可能需要生成service account credentials(通常为JSON格式),必须将其添加到gcloud中的GOOGLE_APPLICATION_CREDENTIALS环境变量中。
以下是如何使用上一步中获取的令牌对REST API进行身份验证和使用的示例。例如,这将是列出存储在存储桶中的对象的请求:
GET /storage/v1/b/example-bucket/o HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer [YOUR_TOKEN]