Sha256: 4f22d95a1950c08233924c71ff0cedb4ecf0b15e740aa6c46bcbeacb635dd7ef

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

require 'ohm'
require 'ohm/contrib'
require 'bcrypt'
require 'securerandom'

module Cabal
  module API
    class User < Ohm::Model
      include Ohm::Callbacks

      # The secret key should not be saved plain, but we want to access it
      # immediately after creation
      attr_accessor :secret_key

      attribute :email
      attribute :access_key
      attribute :crypted_secret_key

      # Enable lookups via either the user's email or their access key.
      index :email
      index :access_key

      # Ensure that email and access keys are unique
      unique :email
      unique :access_key

      def before_create
        set_access_key
        set_secret_key
      end

      def authenticated_with?(secret_key)
        return false unless crypted_secret_key

        BCrypt::Password.new(crypted_secret_key) == secret_key
      end

      private
      def set_access_key
        self.access_key = SecureRandom.hex(16)
      end

      def set_secret_key
        self.secret_key = SecureRandom.hex(32)
        self.crypted_secret_key = BCrypt::Password.create(secret_key)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cabal-api-0.2.2 lib/cabal/api/user.rb
cabal-api-0.2.1 lib/cabal/api/user.rb
cabal-api-0.2.0 lib/cabal/api/user.rb
cabal-api-0.1.1 lib/cabal/api/user.rb
cabal-api-0.1.0 lib/cabal/api/user.rb
cabal-api-0.0.4 lib/cabal/api/user.rb
cabal-api-0.0.3 lib/cabal/api/user.rb