1

我正在开发一个沙盒集群和一个由我在 MongoDB Stitch 中创建的新应用程序。

我需要了解 MongoDB Stitch App 中的“有效负载签名验证”。可以说,我需要创建一个 REST GET API,它将获取我的产品列表,但是这个 API 调用必须经过身份验证,即。只有注册/经过身份验证的用户才能拨打此电话。MongoDB Stitch 建议如下:

https://docs.mongodb.com/stitch/services/webhook-requests-and-responses/#webhook-verify-payload-signature

但是,我需要明白:

图像1

(1) 在哪里添加这个 BODY & SECRET ?据我所知,它必须保存在缝合应用程序中,因为您不得在客户端 JavaScript 中公开您的任何密钥。

(2) { "message":"MESSAGE" } 这是可配置的吗?如果是,我们应该在这里添加什么值?

图2

此功能必须在 MongoDB Stitch App 中编码。这很清楚。此函数根据您在前面步骤中传递的“body”和“secret”返回“hash”。

现在,您必须在 API 请求中传递此哈希:

图3

现在,问题是:

您可以轻松地在开发人员工具中看到任何正在传递给服务器的请求,任何人都可以轻松地复制它并通过 POSTMAN 传递它。所以:

-> 如何保护我的请求?(仅供参考:我还添加了“规则”,表示只有当域名包含 www.mysite.com 时才能执行此请求。但我能够从 localhost 成功执行请求。)

-> 如果,任何人都可以在 POSTMAN 中复制并粘贴我的请求并运行它。那么,生成该 HASH 有什么用?

-> 我如何让我的请求令牌在有限的时间内保持有效/有效,假设请求仅在接下来的 5 分钟内有效?(我的意思是我如何在 Stitch APP 中执行此操作?那个选项在哪里?)

-> 我如何获得刷新令牌?&即使我以某种方式得到它,我如何将它重新传递给请求?

所有此类查询在 MongoDB Stich 文档中都是 UN_ANSWERED:https ://docs.mongodb.com/stitch/

基本上我想了解 MongoDB Stitch App / Stitch REST API 的任何 GET/POST/PUT/PATCH/DELETE 请求的完整生命周期。

如果有人使用过 MongoDB Stich,请解释一下。

4

1 回答 1

1

我不知道您的具体用例,但我在创建经过身份验证的 HTTP REST API 时也遇到了问题。我的想法是:我已经在 Stitch 中定义了所有安全规则和模式,现在我想通过 HTTP 访问数据,仍然使用 Stitch 中定义的逻辑,而不是重写所有内容。

我无法使用 Stitch 函数和 Webhooks 创建这样的 API,尽管我在(字面上)1 小时内使用 NodeJS Koa(express 或任何其他框架都可以)和Stitch 服务器 SDK创建了一个 API 服务器:

// app.js
const Koa = require('koa')
const app = module.exports = new Koa()

const auth = require('./auth')
const router = require('./router')

app.use(auth())
app.use(router.routes())
app.use(router.allowedMethods())

// listen
if (!module.parent) {
  app.listen(3000)
}
// auth.js
const { loginWithApiKey } = require('./stitch')

function auth () {
  return async function auth (ctx, next) {
    const apiKey = ctx.query.api_key

    try {
      await loginWithApiKey(apiKey)
    } catch (e) {
      ctx.throw(401, 'Not Authorized')
    }

    await next()
  }
}

module.exports = auth
// router.js
const router = require('koa-router')()
const { BSON } = require('mongodb-stitch-server-sdk')

const { db } = require('./stitch')

router.get('/', async (ctx) => {
  ctx.body = { message: 'Nothing to see, but you\'re good!' }
})

const COLLECTIONS_WHITELIST = [
  'activities',
  'expenses',
  'projects',
  'resources'
]

// List
router.get('/:collection', async (ctx) => {
  const collection = ctx.params.collection

  isValidCollection(ctx, collection)

  ctx.body = await db
    .collection(collection)
    .find()
    .toArray()
})

function isValidCollection (ctx, collection) {
  // check if the collection is allowed in the API
  if (!COLLECTIONS_WHITELIST.includes(collection)) {
    ctx.throw(404, `Unknown API entity ${collection}`)
  }
}

module.exports = router

我希望它有帮助

于 2019-10-13T15:26:35.497 回答