Sha256: 38475d82d04a1ad391a64e305a5f18f8a7bd41687302f2afac21ea8b6045fc9e

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 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
    end
    
    def get opts
      id, fingerprint = opts.is_a?(Hash) ? [nil, opts[:fingerprint]] : [opts, nil]
      if id
        key = adapter.get_key(id.to_s)
      elsif fingerprint
        key, _ = get_by_fingerprint(fingerprint)
      end
      key
    end

    def get_by_fingerprint fingerprint
      adapter.get_by_fingerprint fingerprint
    end
    
    def each &_
      adapter.each { |k, v| yield k, 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
      key, id = keystore.get_by_fingerprint token['key']
      if key && key.token_valid?(token)
        return id
      else
        return nil
      end
    end
    
    attr_accessor :adapter
    
    private
    def keystore
      @keystore ||= Keystore.new
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slosilo-0.4.1 lib/slosilo/keystore.rb
slosilo-0.4.0 lib/slosilo/keystore.rb