2013-09-21 71 views

Trả lời

31

Từ họ blog:

"chúng ta không thể tiêu hóa token xác thực được cung cấp bởi TokenAuthenticatable, vì họ thường là một phần của API nơi token được sử dụng nhiều lần Kể từ khi sử dụng các thẻ authenticatable có thể thay đổi đáng kể. giữa các ứng dụng, mỗi ứng dụng yêu cầu đảm bảo an toàn khác nhau, chúng tôi đã quyết định xóa TokenAuthenticatable khỏi Devise, cho phép người dùng chọn tùy chọn tốt nhất. "

Hiện tại, các nhà phát triển chọn phù hợp nhất tùy thuộc vào việc sử dụng mã thông báo xác thực của họ.

Thanh toán số này gist.

+0

Nếu bạn muốn có đề xuất cho một thư viện cụ thể, tôi đã hài lòng với [devise_token_auth] (https://github.com/lynndylanhurley/devise_token_auth). –

41

Tôi muốn giữ tính tương thích ngược nên tôi chỉ chuyển mọi thứ vào một mối quan tâm để tránh cảnh báo. Dưới đây là mã của tôi và liên quan đến thông số kỹ thuật:

/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 

/app/mô hình/nhân viên. 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 

đặc tả/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 - đẹp, nhưng tôi nghĩ bạn nên thay thế 'User' bằng' self.class.unscoped' trong 'generate_authentication_token' để Concern không gắn với một lớp' User' cụ thể. –

+0

Bạn có thể giải thích tại sao chúng ta cần phải thực hiện 'reset_authentication_token!'? –

+0

Ngay cả sau khi triển khai điều này, bạn vẫn sẽ cần xử lý mã thông báo (lưu trữ trên DB, tạo yêu cầu, v.v.) của riêng bạn – Lucio

0

Tôi đã trả lời câu hỏi này trước đây và cung cấp một sự thay thế với mã ví dụ bao gồm how to do OAuth 2.0 API/Token authentication with Rails and Warden.

Devise không liên quan gì đến API và tôi luôn cảm thấy khó chịu khi cố gắng đấu vật với Devise để làm cho nó hoạt động theo cách tôi cần, vì vậy phần mềm trung gian Warden mà Devise dựa trên vẫn hữu ích để hỗ trợ xác thực nhiều chiến lược và là những gì ví dụ của tôi sử dụng.

0

Tôi đã sử dụng đá quý devise_token_auth là một trong những lựa chọn thay thế được liệt kê trong Devise wiki page for token authentication.

Tôi không biết liệu đó có phải là tiêu chuẩn thực tế cho thẻ xác thực Devise hay không nhưng đó chắc chắn là sự truy cập của tôi.

0

Điều này trông giống như một câu hỏi rất cũ, tuy nhiên tôi sẽ đặt một tuyệt vời gem cho bản ghi tại đây.

Bạn có thể bảo mật API của mình với Doorkeeper Gem, nhà cung cấp Oauth tuyệt vời cho các ứng dụng Rails.