I'm searching for a开发者_运维技巧 Gem that will create a private and public key from a given string, that can then be stored in a database. Can anyone recommend such a Gem?
Devise allows this for a token via token_authenticable, I have not found a gem that does this however, so my models usually looks like:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :token_authenticatable, :confirmable
before_save :ensure_authentication_token
before_save :create_secret_token
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
private
def create_secret_token
self.secret_token = ActiveSupport::SecureRandom.base64(20).tr('+/=', '-_ ').strip.delete("\n") unless self.secret_token
end
end
The secret_token generator line ensures that the +/= characters are replaced and then whitespace is removed. "+/=" characters can be difficult to deal with when using RESTful APIs:
ActiveSupport::SecureRandom.base64(20).tr('+/=', '-_ ').strip.delete("\n")
精彩评论