Class: Lyrebird::Certificate

Inherits:
Object
  • Object
show all
Defined in:
lib/lyrebird/certificate.rb

Constant Summary collapse

LOCK =
Mutex.new
EXTENSIONS =
{
  "basicConstraints" => "CA:FALSE",
  "keyUsage" => "digitalSignature,keyEncipherment"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bits: 2048, cn: "lyrebird", o: nil, valid_for: 365, valid_until: nil, key: nil, x509: nil) ⇒ Certificate

Returns a new instance of Certificate.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lyrebird/certificate.rb', line 30

def initialize(
  bits: 2048,
  cn: "lyrebird",
  o: nil,
  valid_for: 365,
  valid_until: nil,
  key: nil,
  x509: nil
)
  @common_name = cn
  @organization = o
  @valid_for = valid_for
  @valid_until = valid_until
  @key = key || OpenSSL::PKey::RSA.new(bits)
  @x509 = x509 || build_x509
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



12
13
14
# File 'lib/lyrebird/certificate.rb', line 12

def key
  @key
end

#x509Object (readonly)

Returns the value of attribute x509.



12
13
14
# File 'lib/lyrebird/certificate.rb', line 12

def x509
  @x509
end

Class Method Details

.build(**kwargs) {|config| ... } ⇒ Object

Yields:

  • (config)


18
19
20
21
22
# File 'lib/lyrebird/certificate.rb', line 18

def self.build(**kwargs)
  config = OpenStruct.new(kwargs)
  yield config if block_given?
  new(**config.to_h)
end

.defaultObject



14
15
16
# File 'lib/lyrebird/certificate.rb', line 14

def self.default
  LOCK.synchronize { @default ||= build }
end

.load(key_pem:, x509_pem:) ⇒ Object



24
25
26
27
28
# File 'lib/lyrebird/certificate.rb', line 24

def self.load(key_pem:, x509_pem:)
  key = OpenSSL::PKey::RSA.new(key_pem)
  x509 = OpenSSL::X509::Certificate.new(x509_pem)
  new(key: key, x509: x509)
end

Instance Method Details

#base64Object



60
61
62
# File 'lib/lyrebird/certificate.rb', line 60

def base64
  Base64.strict_encode64(@x509.to_der)
end

#fingerprint(algorithm = :sha256) ⇒ Object



55
56
57
58
# File 'lib/lyrebird/certificate.rb', line 55

def fingerprint(algorithm = :sha256)
  digest = OpenSSL::Digest.new(algorithm.to_s)
  digest.hexdigest(@x509.to_der).upcase.scan(/../).join(":")
end

#key_pemObject



47
48
49
# File 'lib/lyrebird/certificate.rb', line 47

def key_pem
  @key.to_pem
end

#x509_pemObject



51
52
53
# File 'lib/lyrebird/certificate.rb', line 51

def x509_pem
  @x509.to_pem
end