【问题标题】:EmberJS Rails API securityEmberJS Rails API 安全性
【发布时间】:2013-05-05 08:36:22
【问题描述】:

Setup 是一个 Ember 前端,带有一个使用 JSON api 的 rails 后端。

一切正常,但确实出现了一些问题:

如何确保只有 emberjs 应用程序使用 api?我不希望脚本编写者编写应用程序来使用后端 api。

这一切似乎都很不安全,因为 EmberJS 应用程序会以 .js 文件的形式提供给客户端。

如果每个人都可以访问 JS 控制台,我将如何确保用户确实是那个用户?

【问题讨论】:

    标签: javascript ruby-on-rails ruby-on-rails-3 ember.js ember-data


    【解决方案1】:

    您可以扩展 RESTAdapter 并覆盖 ajax 方法以将您的身份验证令牌包含在哈希中,并且您需要确保您的控制器验证该令牌。

    在我的环境 (.NET) 中,我在应用程序呈现的文档的隐藏字段中有身份验证令牌,因此我的 ajax 覆盖如下所示:

    App.Adapter = DS.RESTAdapter.extend({
      ajax: function(url, type, hash, dataType) {
          hash.url = url;
          hash.type = type;
          hash.dataType = dataType || 'json';
          hash.contentType = 'application/json; charset=utf-8';
          hash.context = this;
          if (hash.data && type !== 'GET') {
            hash.data = JSON.stringify(hash.data);
          }
          var antiForgeryToken = $('#antiForgeryTokenHidden').val();
          if (antiForgeryToken) {
              hash = {
                'RequestVerificationToken': antiForgeryToken
              };
          }
          jQuery.ajax(hash);
        }
    });
    

    令牌可以来自 cookie 或您定义的任何内容,只要您能够将其包含在请求标头中并让您的控制器对其进行验证(可能在 before_filter 中),就足够了。

    然后在 Store 中,传递新适配器而不是默认适配器(即 RESTAdapter)

    App.Store = DS.Store.extend({
        revision: 12,
        adapter: App.Adapter.create()
    })
    

    注意: RESTAdapter#ajax 将改为赞成或 Ember.RSVP,从而弃用此覆盖。它必须在下一个版本之后更新,但对于修订版 12 应该没问题。

    【讨论】:

      【解决方案2】:

      我正在使用Ember Simple Auth 对用户身份验证和API 授权有很好的效果。

      我使用 Oauth 2 用户密码授权类型对用户进行身份验证,并通过必须在所有未来 API 请求中发送的不记名令牌来授权应用程序。这意味着用户将他们的用户名/电子邮件和密码输入客户端应用程序,然后通过 HTTPS 发送到服务器以获取授权令牌和可能的刷新令牌。所有请求都必须通过 HTTPS 以保护不记名令牌的泄露。

      我在 app/initializers/auth 中有这个:

      Em.Application.initializer
        name: 'authentication'
        initialize: (container, application) ->
          Em.SimpleAuth.Authenticators.OAuth2.reopen
            serverTokenEndpoint: 'yourserver.com/api/tokens'
          Em.SimpleAuth.setup container, application,
            authorizerFactory: 'authorizer:oauth2-bearer'
            crossOriginWhitelist: ['yourserver.com']
      

      在 app/controllers/login.coffee 中:

      App.LoginController = Em.Controller.extend Em.SimpleAuth.LoginControllerMixin,
        authenticatorFactory: 'ember-simple-auth-authenticator:oauth2-password-grant'
      

      在 app/routes/router.coffee 中:

      App.Router.map ->
        @route 'login'
        # other routes as required...
      

      在 app/routes/application.coffee 中:

      App.ApplicationRoute = App.Route.extend Em.SimpleAuth.ApplicationRouteMixin
      

      在 app/routes/protected.coffee 中:

      App.ProtectedRoute = Ember.Route.extend Em.SimpleAuth.AuthenticatedRouteMixin
      

      在 templates/login.hbs 中(我使用的是 Ember EasyForm):

      {{#form-for controller}}
        {{input identification
                label="User"
                placeholder="you@example.com"
                hint='Enter your email address.'}}
        {{input password
                as="password"
                hint="Enter your password."
                value=password}}
        <button type="submit" {{action 'authenticate' target=controller}}>Login</button>
      {{/form-for}}
      

      为了保护路由,我只是从 App.ProtectedRoute 扩展或使用受保护的路由混合。

      您的服务器将需要在上面配置的服务器令牌端点处处理 Oauth 2 请求和响应。这很容易做到,Section 4.3 of RFC 6749 描述了如果您的服务器端框架没有内置支持 Oauth2 的请求和响应。但是,您将需要在您的服务器上存储、跟踪和过期这些令牌。有一些方法可以避免存储令牌,但这超出了问题的范围:)

      我已经回答了后端问题并提供了用于用户身份验证、API 授权和令牌身份验证的示例 Rails 示例代码here

      【讨论】:

      • 现在还支持流行的 Rails gem 设计 - 这使得与 Rails 应用程序的集成变得非常容易。
      • Devise 移除了对基于令牌的身份验证的支持,并要求使用会话 cookie,这对于 API 来说确实不是已经完成的事情,但 Ember Simple Auth 仍然支持。那些 Devise 人基本上使 Devise 与当代 Web 开发 IMO 无关,但它也反映了 Rails 与 API 越来越无关,因为现在存在更好的 Ruby 替代 API。
      • 我们有文档解释如何自定义设计,使其再次支持令牌身份验证:github.com/simplabs/ember-simple-auth/tree/master/packages/…
      猜你喜欢
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2012-06-16
      相关资源
      最近更新 更多