Class: Pione::Relay::RelayAccountDB

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pione/relay/relay-account-db.rb

Defined Under Namespace

Classes: Account

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ RelayAccountDB

Returns a new instance of RelayAccountDB.

Raises:

  • (TypeError)


10
11
12
13
14
# File 'lib/pione/relay/relay-account-db.rb', line 10

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

Instance Method Details

#add(realm, name, password) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/pione/relay/relay-account-db.rb', line 16

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

#delete(realm) ⇒ Object



24
25
26
27
# File 'lib/pione/relay/relay-account-db.rb', line 24

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

#realmsObject



29
30
31
# File 'lib/pione/relay/relay-account-db.rb', line 29

def realms
  @table.keys
end

#saveObject



33
34
35
36
37
38
39
# File 'lib/pione/relay/relay-account-db.rb', line 33

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