Sha256: 3b8e970768ddfe245ac5df19fdc16f2a9dc20c8315db5fd4af1f86caa0d1a8b5

Contents?: true

Size: 1.21 KB

Versions: 17

Compression:

Stored size: 1.21 KB

Contents

module Pione
  module Relay
    class RelayAccountDB
      extend Forwardable

      Account = Struct.new(:name, :digest)

      def_delegator :@table, "[]"

      def initialize(path)
        raise TypeError.new(path) unless path.kind_of?(Pathname)
        @path = path
        @table = read_database
      end

      def add(realm, name, password)
        # stretching x 1000
        digest = (1..1000).inject("") {|hash, _|
          Digest::SHA512.hexdigest("%s:%s:%s" % [hash, name, password])
        }
        @table[realm] = Account.new(name, digest)
      end

      def delete(realm)
        @table.delete(realm)
        save
      end

      def realms
        @table.keys
      end

      def save
        @path.open("w+", 0600) do |f|
          @table.each do |realm, account|
            f.puts "%s:%s:%s" % [realm, account.name, account.digest] if realm and account
          end
        end
      end

      private

      def read_database
        if @path.exist?
          @path.readlines.inject({}) do |tbl, line|
            realm, name, digest = line.chomp.split(":")
            tbl.tap{ tbl.store(realm, Account.new(name, digest)) }
          end
        else
          {}
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pione-0.5.0 lib/pione/relay/relay-account-db.rb
pione-0.5.0.alpha.2 lib/pione/relay/relay-account-db.rb
pione-0.5.0.alpha.1 lib/pione/relay/relay-account-db.rb
pione-0.4.2 lib/pione/relay/relay-account-db.rb
pione-0.4.1 lib/pione/relay/relay-account-db.rb
pione-0.4.0 lib/pione/relay/relay-account-db.rb
pione-0.3.2 lib/pione/relay/relay-account-db.rb
pione-0.3.1 lib/pione/relay/relay-account-db.rb
pione-0.3.0 lib/pione/relay/relay-account-db.rb
pione-0.2.2 lib/pione/relay/relay-account-db.rb
pione-0.2.1 lib/pione/relay/relay-account-db.rb
pione-0.2.0 lib/pione/relay/relay-account-db.rb
pione-0.1.4 lib/pione/relay/relay-account-db.rb
pione-0.1.3 lib/pione/relay/relay-account-db.rb
pione-0.1.2 lib/pione/relay/relay-account-db.rb
pione-0.1.1 lib/pione/relay/relay-account-db.rb
pione-0.1.0 lib/pione/relay/relay-account-db.rb