Sha256: 92968c57771f2c5e0e2ce07f539b43c7be84128cc37f953edd1391c002420cbb

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# Mixin for Models that have an auth_token
module Cadenero::AuthToken

  # Generate authentication token unless already exists.
  def ensure_auth_token
    reset_auth_token if auth_token.blank?
  end

  # Generate authentication token unless already exists and save the record.
  def ensure_auth_token!
    reset_auth_token! if auth_token.blank?
  end

  # Generate new authentication token (a.k.a. "single access token").
  def reset_auth_token
    self.auth_token = generate_token(:auth_token)
  end

  # Generate new authentication token and save the record.
  def reset_auth_token!
    reset_auth_token
    save(:validate => false)
  end

  protected
  # Generate a token by looping and ensuring does not already exist.
  # @param [String] column is the name of the column that has the authentication token
  # @return {String]} a unique generated auth_token
  def generate_token(column)
    loop do
      token = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
      break token unless self.class.where({ column => token }).first
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cadenero-0.0.2.b10 app/models/cadenero/auth_token.rb
cadenero-0.0.2.b8 app/models/cadenero/auth_token.rb
cadenero-0.0.2.b7 app/models/cadenero/auth_token.rb