Class: Net::Tofu::Pub
- Inherits:
-
Object
- Object
- Net::Tofu::Pub
- Defined in:
- lib/net/tofu/pub.rb
Overview
Intermediate container for an X509 certificate public key. Manages Geminispace public certificates.
Constant Summary collapse
- DIR =
File.("~/.tofu")
- DB =
File.("~/.tofu/known_hosts")
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
The host associated with the certificate public key.
-
#key ⇒ String
readonly
The certificate public key associated with the host.
-
#x509 ⇒ OpenSSL::X509::Certificate
readonly
The X509 certificate.
Class Method Summary collapse
-
.db_exist? ⇒ Boolean
Check if known_hosts file exists, try to create it if it doesn’t.
-
.lookup(host) ⇒ Cert
Lookup a host in the known_hosts table.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare certificate public keys.
-
#initialize(host, data) ⇒ Pub
constructor
Constructor for the cert type.
- #line ⇒ Object
-
#pin ⇒ Object
Pin a certificate public key to the known_hosts file.
-
#to_s ⇒ Object
Hosts file format.
-
#to_x509 ⇒ Object
Conver the certificate public key to a properly formatted X509 string.
Constructor Details
#initialize(host, data) ⇒ Pub
Constructor for the cert type.
23 24 25 26 27 28 |
# File 'lib/net/tofu/pub.rb', line 23 def initialize(host, data) @host = host @key = data @key = format_hosts(data) if data.start_with?("-----BEGIN CERTIFICATE-----") @x509 = OpenSSL::X509::Certificate.new(to_x509) end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns The host associated with the certificate public key.
12 13 14 |
# File 'lib/net/tofu/pub.rb', line 12 def host @host end |
#key ⇒ String (readonly)
Returns The certificate public key associated with the host.
15 16 17 |
# File 'lib/net/tofu/pub.rb', line 15 def key @key end |
#x509 ⇒ OpenSSL::X509::Certificate (readonly)
Returns The X509 certificate.
18 19 20 |
# File 'lib/net/tofu/pub.rb', line 18 def x509 @x509 end |
Class Method Details
.db_exist? ⇒ Boolean
Check if known_hosts file exists, try to create it if it doesn’t.
81 82 83 84 85 86 87 |
# File 'lib/net/tofu/pub.rb', line 81 def self.db_exist? return true if File.exist?(DB) FileUtils.mkdir_p(DIR) unless File.directory?(DIR) FileUtils.touch(DB) true end |
.lookup(host) ⇒ Cert
Lookup a host in the known_hosts table.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/net/tofu/pub.rb', line 33 def self.lookup(host) db_exist? f = File.read(DB) includes = nil f.lines.each_with_index do |l, i| if l.split[0].include? host @line = i includes = l.split[1] break end end return nil if includes.nil? || includes.empty? new(host, includes) end |
Instance Method Details
#==(other) ⇒ Object
Compare certificate public keys
76 77 78 |
# File 'lib/net/tofu/pub.rb', line 76 def ==(other) @key == other.key end |
#line ⇒ Object
50 51 52 |
# File 'lib/net/tofu/pub.rb', line 50 def line @line ||= nil end |
#pin ⇒ Object
Pin a certificate public key to the known_hosts file
55 56 57 58 59 60 61 62 |
# File 'lib/net/tofu/pub.rb', line 55 def pin Net::Tofu::Pub.db_exist? f = File.open(DB, "a") f.puts self ensure f&.close end |
#to_s ⇒ Object
Hosts file format.
71 72 73 |
# File 'lib/net/tofu/pub.rb', line 71 def to_s "#{@host} #{@key}" end |
#to_x509 ⇒ Object
Conver the certificate public key to a properly formatted X509 string.
65 66 67 68 |
# File 'lib/net/tofu/pub.rb', line 65 def to_x509 lines = @key.chars.each_slice(64).map(&:join) "-----BEGIN CERTIFICATE-----\n#{lines.join("\n")}\n-----END CERTIFICATE-----" end |