流星RESTful身份验证。可能吗?

时间:2014-07-06 14:59:22

标签: rest authentication meteor

我已经四处寻找,但无法找到这个问题的满意答案。

我有一个用户登录和创建内容的流星网站。我还想创建一个能够与网站交互的手机应用程序,我希望用户登录手机应用程序并访问网站上的相同内容。非常正常。

我已经创建了一个基本的REST API,用于使用meteorite包HTTP.publish访问集合。它没有任何用户信息(没有auth),但现在我想使用GET方法的userId和集合的Meteor.allow规则来访问当前用户。

因此,我正在努力解决如何在REST请求中告诉meteor,即用户的ID,即使只是在测试时。我以为我可以在浏览器中获取有效用户的Accounts._storedLoginToken并使用它来测试CURL。像

这样的东西

curl -H "X-Auth-Token: asdklfjasldfjlsadkjf" -H "Content-Type: application/json" -d '{"name":"A Name","description":"Testing description"}' http://localhost:3000/api/places

我试过这个,但没有快乐,我得到的403至少是好的。

我的问题是:

  • 是否创建了特定于客户端的令牌(即使用主机URL或其他内容进行散列)?
  • bcrypt是否改变了使用X-Auth-Token的方式?如果不是我在curl命令中做错了什么。
  • DDP是创建有效令牌的唯一方法吗?还是我可以创建一个API调用来在服务器上创建令牌,即使现在只传递纯文本凭据?

例如/api/login?user=shane&pwd=qwerty =>返回token我可以在curl请求中使用。

我真的很困惑,所以任何指向我正确方向的东西都会受到赞赏。我还注意到http.publish尚未创建登录/注销方法,所以可能不是那么容易。

3 个答案:

答案 0 :(得分:3)

几天前,我开始使用一个对身份验证有类似要求的应用。我发现差异的RESTstop2最近在版本0.6.0中升级了它们的身份验证支持,以支持Meteor中新添加的Bcrypt加密。

您只需将用户名和密码作为网址参数或正文发送:

curl --data "password=testpassword&user=test" http://localhost:3000/api/login/

并且服务器将返回以下内容(如果凭据正确):

{ success: true, loginToken: "f2KpRW7KeN9aPmjSZ", userId: fbdpsNf4oHiX79vMJ }

在您对服务器发出的每个请求中,包括loginToken和userId作为标题。

你应该看看:

文档: http://github.differential.io/reststop2/

Github: https://github.com/Differential/reststop2

答案 1 :(得分:1)

另一个选项(除了在其他答案中提到的RESTstop2),您可以使用来自Atmosphere的独立api-password软件包,它可以完全满足您的需求:在服务器端验证REST调用。 它也支持Meteor 0.8.2(带有bcrypt)。

服务器端的示例

  try {
    if (ApiPassword.isPasswordValid(username, password)) {
      console.log('password is valid for this user');
    } else {
      console.log('password is not valid');
    }

  } catch (exc) {
      console.log(exc.message);
      // possible causes: 'User is not found', 'User has no password set'
  }

答案 2 :(得分:1)

我发布了一个用于在Meteor 0.9.0+中编写支持身份验证的REST API的软件包。它现在意味着取代RestStop2(已接受的答案),因为它已被弃用,并且具有类似的API:

https://github.com/krose72205/meteor-restivus

它受到RestStop2的启发,并使用Iron Router的服务器端路由构建。

更新:我只想为找到此内容的任何人提供代码示例。这是GitHub自述文件中的Restivus Quick Start示例:

Items = new Mongo.Collection 'items'

if Meteor.isServer

  # API must be configured and built after startup!
  Meteor.startup ->

    # Global API configuration
    Restivus.configure
      useAuth: true
      prettyJson: true

    # Generates: GET, POST, DELETE on /api/items and GET, PUT, DELETE on
    # /api/items/:id for Items collection
    Restivus.addCollection Items

    # Generates: GET, POST on /api/users and GET, DELETE /api/users/:id for
    # Meteor.users collection
    Restivus.addCollection Meteor.users,
      excludedEndpoints: ['deleteAll', 'put']
      routeOptions:
        authRequired: true
      endpoints:
        post:
          authRequired: false
        delete:
          roleRequired: 'admin'

    # Maps to: /api/posts/:id
    Restivus.addRoute 'posts/:id', authRequired: true,
      get: ->
        post = Posts.findOne @urlParams.id
        if post
          status: 'success', data: post
        else
          statusCode: 404
          body: status: 'fail', message: 'Post not found'
      post:
        roleRequired: ['author', 'admin']
        action: ->
          post = Posts.findOne @urlParams.id
          if post
            status: "success", data: post
          else
            statusCode: 400
            body: status: "fail", message: "Unable to add post"
      delete:
        roleRequired: 'admin'
        action: ->
          if Posts.remove @urlParams.id
            status: "success", data: message: "Item removed"
          else
            statusCode: 404
            body: status: "fail", message: "Item not found"