Ho utilizzato token_authenticatable
prima di proteggere la mia API, tuttavia, ho scoperto che era deprecato? Cosa dovrei usare invece e perché lo hanno deprecato?Devise token_authenticatable deprecato, qual è l'alternativa?
risposta
Dal loro blog:
"non possiamo digerire il token di autenticazione fornito da TokenAuthenticatable, in quanto sono spesso parte di API in cui il simbolo viene utilizzato più volte Poiché l'uso del token autenticabile può variare considerevolmente. tra le applicazioni, ognuna delle quali richiede garanzie di sicurezza diverse, abbiamo deciso di rimuovere TokenAuthenticatable da Devise, consentendo agli utenti di scegliere l'opzione migliore. "
Spetta ora agli sviluppatori scegliere la soluzione migliore in base al loro utilizzo del token di autenticazione.
Acquista questo gist.
Volevo mantenere la compatibilità all'indietro, quindi ho spostato tutto in un problema per evitare l'avviso. Ecco il mio codice e le specifiche associate:
/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/modelli/dipendente. 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
+1 - bello, ma penso che dovresti sostituire 'Utente' di' self.class.unscoped' in 'generate_authentication_token' in modo che Concern non sia legato a una specifica classe' User'. –
Puoi spiegare perché dobbiamo fare 'reset_authentication_token!'? –
Anche dopo averlo implementato, dovrai ancora gestire il token (archivio su DB, generare con una richiesta, ecc.) Da solo – Lucio
Ho risposto a questa domanda precedentemente e fornito un'alternativa con rivestimento esempio di codice how to do OAuth 2.0 API/Token authentication with Rails and Warden.
Devise è praticamente irrilevante per le API e mi sono sempre sentito a disagio nel cercare di lottare con Devise per farlo funzionare nel modo in cui mi serviva, quindi il middleware Warden su cui è basato Devise è ancora utile per supportare l'autenticazione multipla strategie ed è ciò che il mio esempio utilizza.
Sto usando la gemma devise_token_auth che è una delle alternative elencate nello Devise wiki page for token authentication.
Non so se ora è lo standard de-facto per l'autenticazione token Devise o no, ma è sicuramente il mio go-to.
Questa sembra una domanda molto vecchia, tuttavia metterò un impressionante gem
per il record qui.
Puoi proteggere la tua API con lo Doorkeeper Gem, un fantastico fornitore oauth per le app Rails.
Se desideri un consiglio per una libreria specifica, sono stato contento di [devise_token_auth] (https://github.com/lynndylanhurley/devise_token_auth). –