Class: Rack::Blinkbox::Zuul::FileKeyFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/blinkbox/zuul/tokens/file_key_finder.rb

Overview

A simple key finder which uses the key identifier to locate keys on the file system.

Instance Method Summary collapse

Constructor Details

#initialize(key_dir = "./keys") ⇒ FileKeyFinder

Initialises a new file key finder.

Parameters:

  • key_dir (String) (defaults to: "./keys")

    The directory in which keys are located.



13
14
15
# File 'lib/rack/blinkbox/zuul/tokens/file_key_finder.rb', line 13

def initialize(key_dir = "./keys")
  @key_dir = key_dir
end

Instance Method Details

#key_with_id(key_id, type) ⇒ String

Loads a key with a specified identifier.

Parameters:

  • key_id (String)

    The key identifier.

  • type (Symbol)

    :public, :private or :symmetric, depending on the required key type.

Returns:

  • (String)

Raises:

  • (Sandal::InvalidTokenError)


22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/blinkbox/zuul/tokens/file_key_finder.rb', line 22

def key_with_id(key_id, type)
  raise Sandal::InvalidTokenError.new("Unspecified key.") if key_id.nil?
  key_dir = ::File.join(@key_dir, ::File.expand_path(key_id, "/")) # mitigate directory expansion attacks
  key_file = "#{key_dir}/#{type}.pem"
  begin
    ::File.read(key_file) # TODO: Binary read
  rescue
    raise Sandal::InvalidTokenError.new("Unknown key.")
  end
end