1

安裝程序是一個帶有使用JSON API的rails後端的Ember前端。EmberJS Rails API安全

一切都很好,但有些問題根本上來:

如何確保只有emberjs應用程序所消耗的API?我不希望腳本編寫應用程序來使用後端api。

這一切似乎都很不安全,因爲EmberJS應用程序會以.js文件形式發送到客戶端。

如果每個人都有權訪問JS控制檯,我將如何確保用戶真的是該用戶?

回答

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),就足夠了。

然後在商店,通過新的適配器,而不是默認的(這是RESTAdapter)

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

注:RESTAdapter#ajax將投票贊成或Ember.RSVP改變,使這個覆蓋過時。它必須在下一個版本後更新,但對於修訂版本12應該是正確的。

0

我使用Ember Simple Auth對用戶身份驗證和API授權有很大的影響。

我使用Oauth 2用戶密碼授權類型進行用戶身份驗證,並通過必須在所有將來的API請求上發送的不記名令牌授權應用程序。這意味着用戶將他們的用戶名/電子郵件和密碼輸入到客戶端應用程序,然後通過HTTPS發送到服務器以獲取授權令牌和可能的刷新令牌。所有請求必須通過HTTPS來保護不記名令牌的披露。

我有此在app /初始化/ 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'] 

在應用程序/控制器/ login.coffee:

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

在應用程序/路由/ router.coffee:

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

在app/routes/application.coffee中:

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

在app /路由/ protected.coffee:

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

在模板/登錄。哈佛商學院(我用灰燼EasyForm):

{{#form-for controller}} 
    {{input identification 
      label="User" 
      placeholder="[email protected]" 
      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認證和令牌認證here

+0

也有支持對於現在流行的Rails gem設計 - 這使得與Rails應用程序的集成非常簡單。 – marcoow 2014-05-22 08:10:48

+0

Devise刪除了對基於令牌的認證的支持,並要求使用會話cookie,這對於API來說確實不是一件好事,但仍受Ember Simple Auth支持。這些設計人員基本上使Devise與當代Web開發IMO無關,但它也反映了Rails越來越不適合API,因爲現在存在更好的API替代品。 – 2014-05-28 09:21:43

+0

我們有解釋如何定製設計的文檔,以便它再次支持令牌認證:https://github.com/simplabs/ember-simple-auth/tree/master/packages/ember-simple-auth-devise#readme – marcoow 2014-05-28 11:35:41