我以前使用过token_authenticatable
来保护我的API,但是我发现它已被弃用了。那么我应该使用什么替代方案?为什么会被弃用?
我以前使用过token_authenticatable
来保护我的API,但是我发现它已被弃用了。那么我应该使用什么替代方案?为什么会被弃用?
我希望保持向后兼容性,所以我将所有内容移动到关注点中以避免警告。这是我的代码和相关规范:
/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/models/employee.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
spec/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
我一直在使用devise_token_auth宝石,这是Devise令牌认证维基页面列出的替代品之一。
我不知道它现在是否成为Devise令牌认证的事实标准,但它绝对是我的首选。
我之前回答了这个问题,并提供了一个覆盖 如何使用Rails和Warden进行OAuth 2.0 API/Token身份验证的示例代码 的替代方案。
对于API而言,Devise几乎不相关,我总是感到不舒服尝试与Devise搏斗,使其按照我所需的方式工作,因此我放弃了它,但基于Warden中间件的Devise仍然有用于支持多种身份验证策略,这也是我的示例所使用的。
这似乎是一个非常古老的问题,但我会在这里记录一个很棒的gem
。
您可以使用Doorkeeper Gem来保护您的API,它是Rails应用程序的一个很棒的oauth提供者。
generate_authentication_token
方法中将User
替换为self.class.unscoped
,以便Concern不会绑定到特定的User
类。 - m_xreset_authentication_token!
吗? - Arup Rakshit