Class: Watobo::Gui::ClientCertDialog::PEMFrame

Inherits:
FXVerticalFrame
  • Object
show all
Defined in:
lib/watobo/gui/client_cert_dialog.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ PEMFrame

Returns a new instance of PEMFrame.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/watobo/gui/client_cert_dialog.rb', line 85

def initialize(owner)
  @client_cert_dt = FXDataTarget.new('')
  @client_key_dt = FXDataTarget.new('')
  @password_dt = FXDataTarget.new('')
  @retype_dt = FXDataTarget.new('')

  super owner, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_RAISED
  matrix = FXMatrix.new(self, 3, :opts => MATRIX_BY_COLUMNS|LAYOUT_FILL_X|LAYOUT_FILL_Y)

  FXLabel.new(matrix, "Certificate File:", nil, LAYOUT_TOP|JUSTIFY_RIGHT)
  @client_cert_txt = FXTextField.new(matrix, 25,
                                     :target => @client_cert_dt, :selector => FXDataTarget::ID_VALUE,
                                     :opts => TEXTFIELD_NORMAL|LAYOUT_SIDE_RIGHT)

  FXButton.new(matrix, "Select").connect(SEL_COMMAND) { select_cert_file }

  FXLabel.new(matrix, "Key File:", nil, LAYOUT_TOP|JUSTIFY_RIGHT)
  @client_key_txt = FXTextField.new(matrix, 25,
                                    :target => @client_key_dt, :selector => FXDataTarget::ID_VALUE,
                                    :opts => TEXTFIELD_NORMAL|LAYOUT_SIDE_RIGHT)
  FXButton.new(matrix, "Select").connect(SEL_COMMAND) { select_key_file }

  #  matrix = FXMatrix.new(main_frame, 2, :opts => MATRIX_BY_COLUMNS|LAYOUT_FILL_X|LAYOUT_FILL_Y)
  FXLabel.new(matrix, "Password:", nil, LAYOUT_TOP|JUSTIFY_RIGHT)
  @password_txt = FXTextField.new(matrix, 25,
                                  :target => @password_dt, :selector => FXDataTarget::ID_VALUE,
                                  :opts => TEXTFIELD_NORMAL|LAYOUT_SIDE_RIGHT|TEXTFIELD_PASSWD)

  FXButton.new(matrix, "", :opts => FRAME_NONE).disable

  FXLabel.new(matrix, "Retype:", nil, LAYOUT_TOP|JUSTIFY_RIGHT)
  @retype_txt = FXTextField.new(matrix, 25,
                                :target => @retype_dt, :selector => FXDataTarget::ID_VALUE,
                                :opts => TEXTFIELD_NORMAL|LAYOUT_SIDE_RIGHT|TEXTFIELD_PASSWD)

  FXButton.new(matrix, "", :opts => FRAME_NONE).disable

end

Instance Method Details

#certObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/watobo/gui/client_cert_dialog.rb', line 6

def cert
  client_cert = {}

  begin
    if File.exist?(@client_cert_dt.value)
      client_cert[:ssl_client_cert] = OpenSSL::X509::Certificate.new(File.read(@client_cert_dt.value))
    end

    if File.exist?(@client_key_dt.value)
      client_cert[:ssl_client_key] = OpenSSL::PKey::RSA.new(File.read(@client_key_dt.value), @password_dt.value)
    end

    client_cert[:password] = @password_dt.value

    client_cert[:certificate_file] = @client_cert_dt.value
    client_cert[:key_file] = @client_key_dt.value
    client_cert[:type] = :pem

    return client_cert
  rescue => bang
    puts bang
    puts bang.backtrace
  end
  return nil
end

#settingsObject



32
33
34
35
36
37
38
# File 'lib/watobo/gui/client_cert_dialog.rb', line 32

def settings
  s = {
      :certificate_file => @client_cert_dt.value,
      :password => @password_dt.value,
      :key_file => @client_key_dt.value
  }
end

#settings_valid?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/watobo/gui/client_cert_dialog.rb', line 55

def settings_valid?
  unless @password_dt.value.empty?
    puts "* password is set"
    if @password_dt.value != @retype_dt.value
      FXMessageBox.information(self, MBOX_OK, "Passwords", "Passwords don't match!")
      return false
    end
    password = @password_dt.value
  end

  unless File.exist?(@client_cert_dt.value)
    FXMessageBox.information(self, MBOX_OK, "File not found", "#{@client_cert_dt.value} does not exist!")
    return false
  end

  unless File.exist?(@client_key_dt.value)
    FXMessageBox.information(self, MBOX_OK, "File not found", "#{@client_key_dt.value} does not exist!")
    return false

  end
  # last but not least check if private key can be accessed
  begin
    key = OpenSSL::PKey::RSA.new(File.open(@client_key_dt.value), password)
  rescue => bang
    FXMessageBox.information(self, MBOX_OK, "Wrong Password", "Could not open private key file. Wrong password?")
    return false
  end
  true
end

#update_fields(c) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/watobo/gui/client_cert_dialog.rb', line 40

def update_fields(c)
  unless c.nil?
    @client_cert_dt.value = c[:certificate_file]
    @cert_path = File.dirname(c[:certificate_file]) + '/'
    @client_key_dt.value = c[:key_file]
    @password_dt.value = c[:password].nil? ? '' : c[:password]
    @retype_dt.value = c[:password].nil? ? '' : c[:password]
    @client_cert_txt.handle(self, FXSEL(SEL_UPDATE, 0), nil)
    @client_key_txt.handle(self, FXSEL(SEL_UPDATE, 0), nil)
    @password_txt.handle(self, FXSEL(SEL_UPDATE, 0), nil)
    @retype_txt.handle(self, FXSEL(SEL_UPDATE, 0), nil)
  end
end