localhost 1.1.8 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a8bb85b8e82b1ceaec73363ba246313ee46da6d47c948ed2b57bfa1f931b51a
4
- data.tar.gz: 8953521fbcd8eb215ddfaaa59a65b96715c8e69dfb925b0794d1e5e41c204613
3
+ metadata.gz: 5c6fcac1295924f18159bdfbf56da57dc568c01dfe16fe030ca4efa03a51f77d
4
+ data.tar.gz: e781042e92c7456d0e99ca8373c22433e193e4ac2dbea36829484a0fa121f77a
5
5
  SHA512:
6
- metadata.gz: 16e6781fb5b9caf6180ae68579066d2ab0006fbbb5107d81a6ed475dabeb37a0362ecb67a4e19a7f4d527794e56848c9c367286a1a6424494e0f3bc81cfc9704
7
- data.tar.gz: 93a1b52e82b6abf55413b9af17f3769bc659cfcc857da9e6a0c9860120137247490abd11d9261d17539f1ef9ef234db29827c3ce07745ac89a0dd1ebad28b2fb
6
+ metadata.gz: 03f3a2b111cfdc84199224ac5ab66c842d03f79d7514354ba06086d057769d3b31fc7606333696f342d2e9d0d438c89a19a470f2e38f0f64a77c200d9897a724
7
+ data.tar.gz: b696353ce1f45d8dbd863cf33b677a5d25ff83b2703585eb1a9b42faf7efe83a0b6bc5105f7c558e36c8542c01da8649eb21804dba415c52a7772e9db292ff85
checksums.yaml.gz.sig ADDED
Binary file
@@ -22,25 +22,44 @@ require 'yaml'
22
22
  require 'openssl'
23
23
 
24
24
  module Localhost
25
+ # Represents a single public/private key pair for a given hostname.
25
26
  class Authority
26
27
  def self.path
27
28
  File.expand_path("~/.localhost")
28
29
  end
29
30
 
30
- def self.fetch(*args)
31
- authority = self.new(*args)
32
- path = self.path
31
+ # List all certificate authorities in the given directory:
32
+ def self.list(root = self.path)
33
+ return to_enum(:list) unless block_given?
33
34
 
34
- unless authority.load(path)
35
- Dir.mkdir(path, 0700) unless File.directory?(path)
35
+ Dir.glob("*.crt", base: root) do |path|
36
+ name = File.basename(path, ".crt")
36
37
 
37
- authority.save(path)
38
+ authority = self.new(name, root: root)
39
+
40
+ if authority.load
41
+ yield authority
42
+ end
43
+ end
44
+ end
45
+
46
+ # Fetch (load or create) a certificate with the given hostname.
47
+ # See {#initialize} for the format of the arguments.
48
+ def self.fetch(*arguments, **options)
49
+ authority = self.new(*arguments, **options)
50
+
51
+ unless authority.load
52
+ authority.save
38
53
  end
39
54
 
40
55
  return authority
41
56
  end
42
57
 
43
- def initialize(hostname = "localhost")
58
+ # Create an authority forn the given hostname.
59
+ # @parameter hostname [String] The common name to use for the certificate.
60
+ # @parameter root [String] The root path for loading and saving the certificate.
61
+ def initialize(hostname = "localhost", root: self.class.path)
62
+ @root = root
44
63
  @hostname = hostname
45
64
 
46
65
  @key = nil
@@ -49,6 +68,9 @@ module Localhost
49
68
  @store = nil
50
69
  end
51
70
 
71
+ # The hostname of the certificate authority.
72
+ attr :hostname
73
+
52
74
  BITS = 1024*2
53
75
 
54
76
  def ecdh_key
@@ -59,6 +81,17 @@ module Localhost
59
81
  @dh_key ||= OpenSSL::PKey::DH.new(BITS)
60
82
  end
61
83
 
84
+ # The private key path.
85
+ def key_path
86
+ File.join(@root, "#{@hostname}.key")
87
+ end
88
+
89
+ # The public certificate path.
90
+ def certificate_path
91
+ File.join(@root, "#{@hostname}.crt")
92
+ end
93
+
94
+ # The private key.
62
95
  def key
63
96
  @key ||= OpenSSL::PKey::RSA.new(BITS)
64
97
  end
@@ -67,6 +100,7 @@ module Localhost
67
100
  @key = key
68
101
  end
69
102
 
103
+ # The certificate name.
70
104
  def name
71
105
  @name ||= OpenSSL::X509::Name.parse("/O=Development/CN=#{@hostname}")
72
106
  end
@@ -75,6 +109,8 @@ module Localhost
75
109
  @name = name
76
110
  end
77
111
 
112
+ # The public certificate.
113
+ # @returns [OpenSSL::X509::Certificate] A self-signed certificate.
78
114
  def certificate
79
115
  @certificate ||= OpenSSL::X509::Certificate.new.tap do |certificate|
80
116
  certificate.subject = self.name
@@ -105,7 +141,7 @@ module Localhost
105
141
  end
106
142
  end
107
143
 
108
- # The certificate store which is used for validating the server certificate:
144
+ # The certificate store which is used for validating the server certificate.
109
145
  def store
110
146
  @store ||= OpenSSL::X509::Store.new.tap do |store|
111
147
  store.add_cert(self.certificate)
@@ -114,8 +150,9 @@ module Localhost
114
150
 
115
151
  SERVER_CIPHERS = "EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5".freeze
116
152
 
117
- def server_context(*args)
118
- OpenSSL::SSL::SSLContext.new(*args).tap do |context|
153
+ # @returns [OpenSSL::SSL::SSLContext] An context suitable for implementing a secure server.
154
+ def server_context(*arguments)
155
+ OpenSSL::SSL::SSLContext.new(*arguments).tap do |context|
119
156
  context.key = self.key
120
157
  context.cert = self.certificate
121
158
 
@@ -138,6 +175,7 @@ module Localhost
138
175
  end
139
176
  end
140
177
 
178
+ # @returns [OpenSSL::SSL::SSLContext] An context suitable for connecting to a secure server using this authority.
141
179
  def client_context(*args)
142
180
  OpenSSL::SSL::SSLContext.new(*args).tap do |context|
143
181
  context.cert_store = self.store
@@ -148,8 +186,8 @@ module Localhost
148
186
  end
149
187
  end
150
188
 
151
- def load(path)
152
- if File.directory? path
189
+ def load(path = @root)
190
+ if File.directory?(path)
153
191
  certificate_path = File.join(path, "#{@hostname}.crt")
154
192
  key_path = File.join(path, "#{@hostname}.key")
155
193
 
@@ -168,7 +206,9 @@ module Localhost
168
206
  end
169
207
  end
170
208
 
171
- def save(path)
209
+ def save(path = @root)
210
+ Dir.mkdir(path, 0700) unless File.directory?(path)
211
+
172
212
  lockfile_path = File.join(path, "#{@hostname}.lock")
173
213
 
174
214
  File.open(lockfile_path, File::RDWR|File::CREAT, 0644) do |lockfile|
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Localhost
22
- VERSION = "1.1.8"
22
+ VERSION = "1.1.9"
23
23
  end
data/lib/localhost.rb CHANGED
@@ -18,4 +18,5 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  # THE SOFTWARE.
20
20
 
21
- require "localhost/version"
21
+ require_relative 'localhost/version'
22
+ require_relative 'localhost/authority'
data.tar.gz.sig ADDED
Binary file
metadata CHANGED
@@ -1,14 +1,42 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: localhost
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.8
4
+ version: 1.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
- cert_chain: []
11
- date: 2021-04-24 00:00:00.000000000 Z
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEhDCCAuygAwIBAgIBATANBgkqhkiG9w0BAQsFADA3MTUwMwYDVQQDDCxzYW11
14
+ ZWwud2lsbGlhbXMvREM9b3Jpb250cmFuc2Zlci9EQz1jby9EQz1uejAeFw0yMTA4
15
+ MTYwNjMzNDRaFw0yMjA4MTYwNjMzNDRaMDcxNTAzBgNVBAMMLHNhbXVlbC53aWxs
16
+ aWFtcy9EQz1vcmlvbnRyYW5zZmVyL0RDPWNvL0RDPW56MIIBojANBgkqhkiG9w0B
17
+ AQEFAAOCAY8AMIIBigKCAYEAyXLSS/cw+fXJ5e7hi+U/TeChPWeYdwJojDsFY1xr
18
+ xvtqbTTL8gbLHz5LW3QD2nfwCv3qTlw0qI3Ie7a9VMJMbSvgVEGEfQirqIgJXWMj
19
+ eNMDgKsMJtC7u/43abRKx7TCURW3iWyR19NRngsJJmaR51yGGGm2Kfsr+JtKKLtL
20
+ L188Wm3f13KAx7QJU8qyuBnj1/gWem076hzdA7xi1DbrZrch9GCRz62xymJlrJHn
21
+ 9iZEZ7AxrS7vokhMlzSr/XMUihx/8aFKtk+tMLClqxZSmBWIErWdicCGTULXCBNb
22
+ E/mljo4zEVKhlTWpJklMIhr55Z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z
12
40
  dependencies:
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
@@ -38,20 +66,6 @@ dependencies:
38
66
  - - ">="
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '10.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '10.0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -94,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
108
  - !ruby/object:Gem::Version
95
109
  version: '0'
96
110
  requirements: []
97
- rubygems_version: 3.2.3
111
+ rubygems_version: 3.1.6
98
112
  signing_key:
99
113
  specification_version: 4
100
114
  summary: Manage a local certificate authority for self-signed localhost development
metadata.gz.sig ADDED
Binary file