繁体   English   中英

使用授权载体请求时,在Firebase函数中获取令牌

[英]Get Token in Firebase Functions When Requesting With An Authorization Bearer

我目前使用火力地堡功能v1.0.3和火力地堡管理v5.12.1 express NodeJS库似乎运行良好。

问题

但是,当我尝试通过发送授权标头保护请求时:

Authorization: Bearer <token>

它没有显示我的令牌的任何日志。 当我在Firebase Functions index.js文件中使用console.log(request)获取token时,我看不到任何与BearerAuthorization Bearer匹配的单词。

当我使用console.log时,我从req.headers得到的内容如下:

{ 'content-length': '0',
  'accept-language': 'en-US,en;q=0.9',
  'accept-encoding': 'gzip, deflate, br',
  referer: 'http://localhost:5000/admin/',
  accept: '*/*',
  'access-control-request-headers': 'authorization,content-type',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
  origin: 'http://localhost:5000',
  'access-control-request-method': 'GET',
  connection: 'close',
  host: 'localhost:5001' 
}

文档来源

我已经阅读了有关Firebase Function Samples:授权的HTTPS Endpoint示例代码的文档,其中有些似乎已过时。

firebase.auth().currentUser.getToken()

例如现在是:

firebase.auth().currentUser.getIdToken()

其他图书馆

我尝试安装诸如express-authorization-bearerexpress-bearer-token ,但仍然无法捕获idToken

我想知道这是NodeJS问题还是Firebase Functions问题。 我目前正在使用v6.11.4

如何使用Express或使用NodeJS的任何方法在Firebase Functions中捕获令牌?

授权承载有效。 这是当我console.log(req.headers)时的结果:

{ 'accept-language': 'en-US,en;q=0.9',
  'accept-encoding': 'gzip, deflate, br',
  referer: 'http://localhost:5000/admin/',
  accept: '*/*',
  'content-type': 'application/json',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
  origin: 'http://localhost:5000',
  authorization: 'Bearer something',
  connection: 'close',
  host: 'localhost:5001' }

在这种情况下, tokensomething

重要笔记:

  • req.headers.authorization不适用于“ 分配多个 req.headers.authorization ,因为它给出了错误: Cannot set headers after they are sent ,但是通过允许任何原点并将cors作为中间件,它应该可以轻松地工作:

    var cors = require('cors')({origin: true}); app.use(cors);

    无论如何, req.headers.authorization已经在保护它。

  • 不能将req.headers.authorization分配给参数,否则它将变得undefined

  • 使用来自同一文档的授权HTTPS端点 ,它应该只有工作完全corscookiescookie-parser插件,并req.headers.authorization完全启用,发送到firebase-functions端点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM