Class: VPN::Config::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/vpn/config/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_name: nil, auth_pass: nil, identifier: nil, certificate_path: nil, certificate_pass: nil, provider: nil, endpoints: nil, data_file: nil) ⇒ Generator

Returns a new instance of Generator.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vpn/config/generator.rb', line 11

def initialize(auth_name: nil, auth_pass: nil, identifier: nil,
    certificate_path: nil, certificate_pass: nil, provider: nil,
    endpoints: nil, data_file: nil)
  @auth_name = auth_name
  @auth_pass = auth_pass
  @identifier = identifier || "com.example.vpn"
  @certificate_path = certificate_path
  @certificate_pass = certificate_pass
  @provider = provider || "Private Internet Access"
  @endpoints = endpoints
  @data_file = data_file || VPN::Config::PROVIDERS_PATH
end

Instance Attribute Details

#auth_nameObject

Returns the value of attribute auth_name.



8
9
10
# File 'lib/vpn/config/generator.rb', line 8

def auth_name
  @auth_name
end

#auth_passObject

Returns the value of attribute auth_pass.



8
9
10
# File 'lib/vpn/config/generator.rb', line 8

def auth_pass
  @auth_pass
end

#certificate_passObject

Returns the value of attribute certificate_pass.



8
9
10
# File 'lib/vpn/config/generator.rb', line 8

def certificate_pass
  @certificate_pass
end

#certificate_pathObject

Returns the value of attribute certificate_path.



8
9
10
# File 'lib/vpn/config/generator.rb', line 8

def certificate_path
  @certificate_path
end

#data_fileObject

Returns the value of attribute data_file.



8
9
10
# File 'lib/vpn/config/generator.rb', line 8

def data_file
  @data_file
end

#endpointsObject

Returns the value of attribute endpoints.



8
9
10
# File 'lib/vpn/config/generator.rb', line 8

def endpoints
  @endpoints
end

#identifierObject

Returns the value of attribute identifier.



8
9
10
# File 'lib/vpn/config/generator.rb', line 8

def identifier
  @identifier
end

#providerObject

Returns the value of attribute provider.



8
9
10
# File 'lib/vpn/config/generator.rb', line 8

def provider
  @provider
end

Instance Method Details

#enabled_vpnsObject



43
44
45
46
47
48
49
# File 'lib/vpn/config/generator.rb', line 43

def enabled_vpns
  if endpoints && endpoints.any?
    vpns.select {|e| endpoints.include? e["name"] }
  else
    vpns
  end
end

#generate_plistObject



51
52
53
# File 'lib/vpn/config/generator.rb', line 51

def generate_plist
  config.to_plist
end

#generate_signed_plistObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vpn/config/generator.rb', line 55

def generate_signed_plist
  private_key = p12.key
  signing_cert = p12.certificate

  private_key = p12.key
  intermediate_certs = p12.ca_certs
  signing_cert = p12.certificate

  # Read configuration profile
  configuration_profile_data = generate_plist

  # Sign the configuration profile
  signing_flags = OpenSSL::PKCS7::BINARY
  signature = OpenSSL::PKCS7.sign(signing_cert, private_key,
                                  configuration_profile_data, intermediate_certs,
                                  signing_flags)

  signature.to_der
end

#providersObject



24
25
26
# File 'lib/vpn/config/generator.rb', line 24

def providers
  @providers ||= YAML.load_file(data_file)["providers"]
end

#selected_providerObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/vpn/config/generator.rb', line 28

def selected_provider
  @selected_provider ||= begin
    prov = providers.find {|pr| pr["name"] == provider }
    if prov
      prov
    else
      raise ArgumentError, "Provider '#{provider}' not found"
    end
  end
end

#vpnsObject



39
40
41
# File 'lib/vpn/config/generator.rb', line 39

def vpns
  @vpns ||= selected_provider["endpoints"]
end