Sha256: 2092b8ae2e62e0e5df35b135056c2ea86147e1aebc71f6c6499f70d2a5628e1b

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

require 'slosilo/key'

module Slosilo
  class Keystore
    def adapter 
      Slosilo::adapter or raise "No Slosilo adapter is configured or available"
    end
    
    def put id, key
      adapter.put_key id.to_s, key.to_der
    end
    
    def get id
      key = adapter.get_key(id.to_s)
      key && Key.new(key)
    end
    
    def each &_
      adapter.each { |k, v| yield k, Key.new(v) }
    end
    
    def any? &block
      each do |_, k|
        return true if yield k
      end
      return false
    end
  end
  
  class << self
    def []= id, value
      keystore.put id, value
    end
    
    def [] id
      keystore.get id
    end
    
    def each(&block)
      keystore.each(&block)
    end
    
    def sign object
      self[:own].sign object
    end
    
    def token_valid? token
      keystore.any? { |k| k.token_valid? token }
    end
    
    def token_signer token
      each do |id, key|
        return id if key.token_valid? token
      end
      return nil
    end
    
    attr_accessor :adapter
    
    private
    def keystore
      @keystore ||= Keystore.new
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slosilo-0.2.4 lib/slosilo/keystore.rb
slosilo-0.2.3 lib/slosilo/keystore.rb
slosilo-0.2.2 lib/slosilo/keystore.rb