Class: Localhost::Authority
- Inherits:
-
Object
- Object
- Localhost::Authority
- Defined in:
- lib/localhost/authority.rb
Class Method Summary collapse
Instance Method Summary collapse
- #certificate ⇒ Object
-
#initialize(hostname = "localhost") ⇒ Authority
constructor
A new instance of Authority.
- #key ⇒ Object
- #load(path) ⇒ Object
- #name ⇒ Object
- #save(path) ⇒ Object
-
#store ⇒ Object
The certificate store which is used for validating the server certificate:.
Constructor Details
#initialize(hostname = "localhost") ⇒ Authority
Returns a new instance of Authority.
43 44 45 46 47 48 49 |
# File 'lib/localhost/authority.rb', line 43 def initialize(hostname = "localhost") @hostname = hostname @key = nil @name = nil @certificate = nil end |
Class Method Details
.fetch(*args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/localhost/authority.rb', line 30 def self.fetch(*args) = self.new(*args) path = self.path unless .load(path) Dir.mkdir(path, 0700) unless File.directory?(path) .save(path) end return end |
.path ⇒ Object
26 27 28 |
# File 'lib/localhost/authority.rb', line 26 def self.path File.("~/.localhost") end |
Instance Method Details
#certificate ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/localhost/authority.rb', line 59 def certificate @certificate ||= OpenSSL::X509::Certificate.new.tap do |certificate| certificate.subject = self.name # We use the same issuer as the subject, which makes this certificate self-signed: certificate.issuer = self.name certificate.public_key = self.key.public_key certificate.serial = 1 certificate.not_before = Time.now certificate.not_after = Time.now + (3600 * 24 * 365 * 10) extension_factory = OpenSSL::X509::ExtensionFactory.new extension_factory.subject_certificate = certificate extension_factory.issuer_certificate = certificate certificate.sign self.key, OpenSSL::Digest::SHA256.new end end |
#key ⇒ Object
51 52 53 |
# File 'lib/localhost/authority.rb', line 51 def key @key ||= OpenSSL::PKey::RSA.new(1024) end |
#load(path) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/localhost/authority.rb', line 87 def load(path) if File.directory? path key_path = File.join(path, "#{@hostname}.key") return false unless File.exist?(key_path) @key = OpenSSL::PKey::RSA.new(File.read(key_path)) certificate_path = File.join(path, "#{@hostname}.crt") @certificate = OpenSSL::X509::Certificate.new(File.read(certificate_path)) return true end end |
#name ⇒ Object
55 56 57 |
# File 'lib/localhost/authority.rb', line 55 def name @name ||= OpenSSL::X509::Name.parse("O=Development/CN=#{@hostname}") end |
#save(path) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/localhost/authority.rb', line 100 def save(path) File.write( File.join(path, "#{@hostname}.crt"), self.certificate.to_pem ) File.write( File.join(path, "#{@hostname}.key"), self.key.to_pem ) end |
#store ⇒ Object
The certificate store which is used for validating the server certificate:
81 82 83 84 85 |
# File 'lib/localhost/authority.rb', line 81 def store @store ||= OpenSSL::X509::Store.new.tap do |store| store.add_cert(self.certificate) end end |