Class: Touchpass::KeyFileCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/touchpass/key_file_creator.rb

Overview

Used to help with creation of public / private key files for a device (by clients)

Constant Summary collapse

DEFAULT_KEYS_PATH =
File.join(ENV['HOME'], ".touchpass", "certs")
PRIVATE_KEY =
"private.pem"
PUBLIC_KEY =
"public.pem"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ KeyFileCreator

Returns a new instance of KeyFileCreator.



17
18
19
# File 'lib/touchpass/key_file_creator.rb', line 17

def initialize(id)
  @id = id.to_s
end

Class Attribute Details

.keys_pathObject

Returns the value of attribute keys_path.



12
13
14
# File 'lib/touchpass/key_file_creator.rb', line 12

def keys_path
  @keys_path
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/touchpass/key_file_creator.rb', line 15

def id
  @id
end

#private_keyObject

Returns the value of attribute private_key.



15
16
17
# File 'lib/touchpass/key_file_creator.rb', line 15

def private_key
  @private_key
end

#public_keyObject

Returns the value of attribute public_key.



15
16
17
# File 'lib/touchpass/key_file_creator.rb', line 15

def public_key
  @public_key
end

Instance Method Details

#generate_keysObject

Use openssl command to generate key pair. RSA public key PEM generated by Ruby’s OpenSSL::PKey::RSA are unreadable by OpenSSL. See: barelyenough.org/blog/2008/04/fun-with-public-keys/



24
25
26
27
28
29
# File 'lib/touchpass/key_file_creator.rb', line 24

def generate_keys
  directory = keys_path
  prepare_directories directory
  `openssl genrsa -out #{directory}/#{PRIVATE_KEY} 1024 > /dev/null 2>&1`
  `openssl rsa -in #{directory}/#{PRIVATE_KEY} -out #{directory}/#{PUBLIC_KEY} -outform PEM -pubout > /dev/null 2>&1`
end

#keys_pathObject



48
49
50
# File 'lib/touchpass/key_file_creator.rb', line 48

def keys_path
  File.join(self.class.keys_path, @id)
end

#private_key_pathObject



56
57
58
# File 'lib/touchpass/key_file_creator.rb', line 56

def private_key_path
  return File.join(keys_path, PRIVATE_KEY)
end

#public_key_pathObject



52
53
54
# File 'lib/touchpass/key_file_creator.rb', line 52

def public_key_path
  return File.join(keys_path, PUBLIC_KEY)
end

#public_key_textObject



40
41
42
# File 'lib/touchpass/key_file_creator.rb', line 40

def public_key_text
  return File.read(public_key_path)
end

#read_key(pem_file) ⇒ Object



44
45
46
# File 'lib/touchpass/key_file_creator.rb', line 44

def read_key(pem_file)
  return OpenSSL::PKey::RSA.new(File.read(pem_file))
end