Sha256: 2e29cfa69d97fb69485ddc04541c460f356096fba68f777d97c99ab4b79fd805

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 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
      id = id.to_s
      fail ArgumentError, "id can't be empty" if id.empty?
      adapter.put_key id, 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

4 entries across 4 versions & 1 rubygems

Version Path
slosilo-2.0.1 lib/slosilo/keystore.rb
slosilo-2.0.0 lib/slosilo/keystore.rb
slosilo-1.1.0 lib/slosilo/keystore.rb
slosilo-1.0.0 lib/slosilo/keystore.rb