2013-09-21 22 views

回答

31

從他們blog

「我們不能消化由TokenAuthenticatable提供的身份驗證令牌,因爲他們往往在令牌被多次使用的API的一部分,因爲可認證令牌的使用可以有很大的不同。在應用程序之間,每個應用程序都需要不同的安全保證,我們決定從Devise中刪除TokenAuthenticatable,允許用戶選擇最佳選項。「

現在由開發人員根據他們對身份驗證令牌的使用情況選擇最合適的方法。

結算這個gist

+0

如果您想要針對特定​​圖書館的建議,我對[devise_token_auth](https://github.com/lynndylanhurley/devise_token_auth)感到滿意。 –

41

我想保持向下兼容性,所以我只是將所有事情都轉移到了關注點以避免警告。這裏是我的代碼和相關規格:

/app/models/concerns/token_authenticatable.rb

module TokenAuthenticatable 
    extend ActiveSupport::Concern 

    module ClassMethods 
    def find_by_authentication_token(authentication_token = nil) 
     if authentication_token 
     where(authentication_token: authentication_token).first 
     end 
    end 
    end 

    def ensure_authentication_token 
    if authentication_token.blank? 
     self.authentication_token = generate_authentication_token 
    end 
    end 

    def reset_authentication_token! 
    self.authentication_token = generate_authentication_token 
    save 
    end 

    private 

    def generate_authentication_token 
    loop do 
     token = Devise.friendly_token 
     break token unless self.class.unscoped.where(authentication_token: token).first 
    end 
    end 
end 

/app/models/user.rb

class User < ActiveRecord::Base 
    include TokenAuthenticatable 
end 

/應用/型號/員工。 RB

class Employee < ActiveRecord::Base 
    include TokenAuthenticatable 
end 

/spec/models/user_spec.rb

describe User do 
    it_behaves_like 'token_authenticatable' 
end 

/spec/models/employee_spec.rb

describe Employee do 
    it_behaves_like 'token_authenticatable' 
end 

規格/ shared_examples/token_authenticatable.rb

shared_examples 'token_authenticatable' do 
    describe '.find_by_authentication_token' do 
    context 'valid token' do 
     it 'finds correct user' do 
     class_symbol = described_class.name.underscore 
     item = create(class_symbol, :authentication_token) 
     create(class_symbol, :authentication_token) 

     item_found = described_class.find_by_authentication_token(
      item.authentication_token 
     ) 

     expect(item_found).to eq item 
     end 
    end 

    context 'nil token' do 
     it 'returns nil' do 
     class_symbol = described_class.name.underscore 
     create(class_symbol) 

     item_found = described_class.find_by_authentication_token(nil) 

     expect(item_found).to be_nil 
     end 
    end 
    end 

    describe '#ensure_authentication_token' do 
    it 'creates auth token' do 
     class_symbol = described_class.name.underscore 
     item = create(class_symbol, authentication_token: '') 

     item.ensure_authentication_token 

     expect(item.authentication_token).not_to be_blank 
    end 
    end 

    describe '#reset_authentication_token!' do 
    it 'resets auth token' do 
    end 
    end 
end 
+4

+1 - 不錯,但我認爲你應該在'generate_authentication_token'中用'self.class.unscoped'代替'User',這樣Concern就不會綁定到特定的'User'類。 –

+0

你能解釋一下爲什麼我們需要做'reset_authentication_token!'嗎? –

+0

即使在實現這個之後,你仍然需要通過你自己的 – Lucio

0

我以前回答了這個問題,並設置有例如代碼覆蓋how to do OAuth 2.0 API/Token authentication with Rails and Warden的替代方案。對於API來說,Devise幾乎是毫不相關的,我總是覺得不適合與Devise搏鬥,讓它按照我需要的方式工作,所以我放棄了它,但Devise所依賴的Warden中間件仍然支持多重身份驗證戰略,是我的例子使用。

0

這看起來像一個非常古老的問題,但我會把一個很棒的gem記錄在這裏。

您可以使用Doorkeeper Gem保護您的API,這是一個非常棒的Rails應用程序oauth提供程序。

相關問題