2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 // Defined here as workaround for emacs vala-mode indentation failure.
39 static const string CANCEL = Stock.CANCEL;
41 static const string CANCEL = STOCK_CANCEL;
45 // For use when exporting certificates.
46 static string export_directory = null;
48 class IdentityDialog : Dialog
50 private static Gdk.Color white = make_color(65535, 65535, 65535);
51 private static Gdk.Color selected_color = make_color(0xd9 << 8, 0xf7 << 8, 65535);
53 private static MoonshotLogger logger = get_logger("IdentityDialog");
55 static const string displayname_labeltext = _("Display Name");
56 static const string realm_labeltext = _("Realm");
57 static const string username_labeltext = _("Username");
58 static const string password_labeltext = _("Password");
60 private Entry displayname_entry;
61 private Label displayname_label;
62 private Entry realm_entry;
63 private Label realm_label;
64 private Entry username_entry;
65 private Label username_label;
66 private Entry password_entry;
67 private Label password_label;
68 private CheckButton remember_checkbutton;
69 private Label message_label;
73 private Label selected_item = null;
75 // Whether to clear the card's TrustAnchor after the user selects OK
76 internal bool clear_trust_anchor = false;
78 public string display_name {
79 get { return displayname_entry.get_text(); }
82 public string issuer {
83 get { return realm_entry.get_text(); }
86 public string username {
87 get { return username_entry.get_text(); }
90 public string password {
91 get { return password_entry.get_text(); }
94 public bool store_password {
95 get { return remember_checkbutton.active; }
98 internal ArrayList<string> get_services()
100 return card.services;
103 public IdentityDialog(IdentityManagerView parent)
105 this.with_idcard(null, _("Add ID Card"), parent);
108 public IdentityDialog.with_idcard(IdCard? a_card, string title, IdentityManagerView parent)
110 bool is_new_card = false;
116 card = a_card ?? new IdCard();
117 this.set_title(title);
118 this.set_modal(true);
119 this.set_transient_for(parent);
121 this.add_buttons(_("OK"), ResponseType.OK, CANCEL, ResponseType.CANCEL);
122 Box content_area = (Box) this.get_content_area();
124 displayname_label = new Label(@"$displayname_labeltext:");
125 displayname_label.set_alignment(0, (float) 0.5);
126 displayname_entry = new Entry();
127 displayname_entry.set_text(card.display_name);
128 displayname_entry.set_width_chars(40);
130 realm_label = new Label(@"$realm_labeltext:");
131 realm_label.set_alignment(0, (float) 0.5);
132 realm_entry = new Entry();
133 realm_entry.set_text(card.issuer);
134 realm_entry.set_width_chars(60);
136 username_label = new Label(@"$username_labeltext:");
137 username_label.set_alignment(0, (float) 0.5);
138 username_entry = new Entry();
139 username_entry.set_text(card.username);
140 username_entry.set_width_chars(40);
142 password_label = new Label(@"$password_labeltext:");
143 password_label.set_alignment(0, (float) 0.5);
145 remember_checkbutton = new CheckButton.with_label(_("Remember password"));
146 remember_checkbutton.active = card.store_password;
148 password_entry = new Entry();
149 password_entry.set_invisible_char('*');
150 password_entry.set_visibility(false);
151 password_entry.set_width_chars(40);
152 password_entry.set_text(card.password);
154 message_label = new Label("");
155 message_label.set_visible(false);
157 set_atk_relation(displayname_label, displayname_entry, Atk.RelationType.LABEL_FOR);
158 set_atk_relation(realm_label, realm_entry, Atk.RelationType.LABEL_FOR);
159 set_atk_relation(username_label, username_entry, Atk.RelationType.LABEL_FOR);
160 set_atk_relation(password_label, password_entry, Atk.RelationType.LABEL_FOR);
162 content_area.pack_start(message_label, false, false, 6);
163 add_as_vbox(content_area, displayname_label, displayname_entry);
164 add_as_vbox(content_area, username_label, username_entry);
165 add_as_vbox(content_area, realm_label, realm_entry);
166 add_as_vbox(content_area, password_label, password_entry);
168 // var entries = new VBox(false, 6);
169 // add_as_vbox(entries, displayname_label, displayname_entry);
170 // add_as_vbox(entries, realm_label, realm_entry);
171 // add_as_vbox(entries, username_label, username_entry);
172 // add_as_vbox(entries, password_label, password_entry);
173 // content_area.pack_start(entries, false, false, 0);
175 var remember_hbox = new HBox(false, 40);
176 remember_hbox.pack_start(new HBox(false, 0), false, false, 0);
177 remember_hbox.pack_start(remember_checkbutton, false, false, 0);
178 content_area.pack_start(remember_hbox, false, false, 2);
179 // content_area.pack_start(remember_checkbutton, false, false, 2);
181 this.response.connect(on_response);
182 content_area.set_border_width(6);
186 Widget trust_anchor_box = make_trust_anchor_box(card);
187 content_area.pack_start(trust_anchor_box, false, false, 15);
189 var services_vbox = make_services_vbox();
190 content_area.pack_start(services_vbox);
193 if (card.is_no_identity())
195 displayname_entry.set_sensitive(false);
196 realm_entry.set_sensitive(false);
197 username_entry.set_sensitive(false);
198 password_entry.set_sensitive(false);
199 remember_checkbutton.set_sensitive(false);
202 this.set_border_width(6);
203 this.set_resizable(false);
204 this.modify_bg(StateType.NORMAL, white);
208 private Widget make_trust_anchor_box(IdCard id)
211 Label ta_label = new Label(_("Trust anchor: ")
212 + (id.trust_anchor.is_empty() ? _("None") : _("Enterprise provisioned")));
213 ta_label.set_alignment(0, 0.5f);
215 if (id.trust_anchor.is_empty()) {
220 AttachOptions opts = AttachOptions.EXPAND | AttachOptions.FILL;
221 AttachOptions fill = AttachOptions.FILL;
223 Table ta_table = new Table(6, 2, false);
226 var ta_clear_button = new Button.with_label(_("Clear Trust Anchor"));
227 ta_clear_button.clicked.connect((w) => {
228 clear_trust_anchor = true;
229 ta_table.set_sensitive(false);
233 ta_table.attach(ta_label, 0, 1, row, row + 1, opts, opts, 0, 0);
234 ta_table.attach(ta_clear_button, 1, 2, row, row + 1, fill, fill, 0, 0);
237 Label added_label = new Label(_("Added : " + id.trust_anchor.datetime_added));
238 added_label.set_alignment(0, 0.5f);
239 ta_table.attach(added_label, 0, 1, row, row + 1, opts, opts, 20, 5);
242 if (id.trust_anchor.get_anchor_type() == TrustAnchor.TrustAnchorType.SERVER_CERT) {
243 Widget fingerprint = make_ta_fingerprint_widget(id.trust_anchor);
244 ta_table.attach(fingerprint, 0, 2, row, row + 2, opts, opts, 20, 5);
247 Label ca_cert_label = new Label(_("CA Certificate:"));
248 ca_cert_label.set_alignment(0, 0.5f);
249 var export_button = new Button.with_label(_("Export Certificate"));
251 export_button.clicked.connect((w) => {export_certificate(id);});
253 ta_table.attach(ca_cert_label, 0, 1, row, row + 1, opts, opts, 20, 0);
254 ta_table.attach(export_button, 1, 2, row, row + 1, fill, fill, 0, 0);
257 //!!TODO: When to show Subject, and when (if ever) show Subject-Altname here?
258 Label subject_label = new Label(_("Subject: ") + id.trust_anchor.subject);
259 subject_label.set_alignment(0, 0.5f);
260 ta_table.attach(subject_label, 0, 1, row, row + 1, opts, opts, 40, 5);
263 Label expiration_label = new Label(_("Expiration date: ") + id.trust_anchor.get_expiration_date());
264 expiration_label.set_alignment(0, 0.5f);
265 ta_table.attach(expiration_label, 0, 1, row, row + 1, opts, opts, 40, 5);
268 //!!TODO: What *is* this?
269 Label constraint_label = new Label(_("Constraint: "));
270 constraint_label.set_alignment(0, 0.5f);
271 ta_table.attach(constraint_label, 0, 1, row, row + 1, opts, opts, 20, 0);
279 private static void add_as_vbox(Box content_area, Label label, Entry entry)
281 VBox vbox = new VBox(false, 2);
283 vbox.pack_start(label, false, false, 0);
284 vbox.pack_start(entry, false, false, 0);
286 // Hack to prevent the text entries from stretching horizontally
287 HBox hbox = new HBox(false, 0);
288 hbox.pack_start(vbox, false, false, 0);
289 content_area.pack_start(hbox, false, false, 6);
292 private static string update_preamble(string preamble)
295 return _("Missing required field: ");
296 return _("Missing required fields: ");
299 private static string update_message(string old_message, string new_item)
302 if (old_message == "")
305 message = old_message + ", " + new_item;
309 private static void check_field(string field, Label label, string fieldname, ref string preamble, ref string message)
312 label.set_markup(@"$fieldname:");
315 label.set_markup(@"<span foreground=\"red\">$fieldname:</span>");
316 preamble = update_preamble(preamble);
317 message = update_message(message, fieldname);
320 private bool check_fields()
322 string preamble = "";
324 string password_test = store_password ? password : "not required";
325 if (!card.is_no_identity())
327 check_field(display_name, displayname_label, displayname_labeltext, ref preamble, ref message);
328 check_field(username, username_label, username_labeltext, ref preamble, ref message);
329 check_field(issuer, realm_label, realm_labeltext, ref preamble, ref message);
330 check_field(password_test, password_label, password_labeltext, ref preamble, ref message);
333 message_label.set_visible(true);
334 message_label.set_markup(@"<span foreground=\"red\">$preamble$message</span>");
340 private void on_response(Dialog source, int response_id)
342 switch (response_id) {
343 case ResponseType.OK:
344 complete = check_fields();
346 case ResponseType.CANCEL:
352 private static void label_make_bold(Label label)
354 var font_desc = new Pango.FontDescription();
356 font_desc.set_weight(Pango.Weight.BOLD);
358 /* This will only affect the weight of the font. The rest is
359 * from the current state of the widget, which comes from the
360 * theme or user prefs, since the font desc only has the
361 * weight flag turned on.
363 label.modify_font(font_desc);
366 private VBox make_services_vbox()
368 logger.trace("make_services_vbox");
370 var services_vbox_alignment = new Alignment(0, 0, 1, 0);
371 var services_vscroll = new ScrolledWindow(null, null);
372 services_vscroll.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
373 services_vscroll.set_shadow_type(ShadowType.IN);
374 services_vscroll.set_size_request(0, 60);
375 services_vscroll.add_with_viewport(services_vbox_alignment);
378 var remove_button = new Button.from_stock(Stock.REMOVE);
380 var remove_button = new Button.from_stock(STOCK_REMOVE);
382 remove_button.set_sensitive(false);
385 var services_table = new Table(card.services.size, 1, false);
386 services_table.set_row_spacings(1);
387 services_table.set_col_spacings(0);
388 services_table.modify_bg(StateType.NORMAL, white);
390 var table_button_hbox = new HBox(false, 6);
391 table_button_hbox.pack_start(services_vscroll, true, true, 6);
393 // Hack to prevent the button from growing vertically
394 VBox fixed_height = new VBox(false, 0);
395 fixed_height.pack_start(remove_button, false, false, 0);
396 table_button_hbox.pack_start(fixed_height, false, false, 0);
398 // A table doesn't have a background color, so put it in an EventBox, and
399 // set the EventBox's background color instead.
400 EventBox table_bg = new EventBox();
401 table_bg.modify_bg(StateType.NORMAL, white);
402 table_bg.add(services_table);
403 services_vbox_alignment.add(table_bg);
405 var services_vbox_title = new Label(_("Services:"));
406 label_make_bold(services_vbox_title);
407 services_vbox_title.set_alignment(0, 0.5f);
409 var services_vbox = new VBox(false, 6);
410 services_vbox.pack_start(services_vbox_title, false, false, 0);
411 services_vbox.pack_start(table_button_hbox, true, true, 0);
414 foreach (string service in card.services)
416 var label = new Label(service);
417 label.set_alignment((float) 0, (float) 0);
420 EventBox event_box = new EventBox();
421 event_box.modify_bg(StateType.NORMAL, white);
422 event_box.add(label);
423 event_box.button_press_event.connect(() =>
425 var state = label.get_state();
426 logger.trace("button_press_callback: Label state=" + state.to_string() + " setting bg to " + white.to_string());
428 if (selected_item == label)
431 selected_item.parent.modify_bg(state, white);
432 selected_item = null;
433 remove_button.set_sensitive(false);
437 if (selected_item != null)
440 selected_item.parent.modify_bg(state, white);
441 selected_item = null;
445 selected_item = label;
446 selected_item.parent.modify_bg(state, selected_color);
447 remove_button.set_sensitive(true);
452 services_table.attach_defaults(event_box, 0, 1, i, i+1);
456 remove_button.clicked.connect((remove_button) =>
458 var result = WarningDialog.confirm(this,
459 Markup.printf_escaped(
460 "<span font-weight='heavy'>You are about to remove the service '%s'.</span>",
462 + "\n\nAre you sure you want to do this?",
468 card.services.remove(selected_item.label);
469 services_table.remove(selected_item.parent);
470 selected_item = null;
471 remove_button.set_sensitive(false);
477 return services_vbox;
480 private void export_certificate(IdCard id)
482 var dialog = new FileChooserDialog("Save File",
484 FileChooserAction.SAVE,
485 _("Cancel"),ResponseType.CANCEL,
486 _("Save"), ResponseType.ACCEPT,
488 dialog.set_do_overwrite_confirmation(true);
489 if (export_directory != null) {
490 dialog.set_current_folder(export_directory);
492 // Remove slashes from the default filename.
493 string default_filename =
494 (id.display_name + ".pem").replace(Path.DIR_SEPARATOR_S, "_");
495 dialog.set_current_name(default_filename);
496 if (dialog.run() == ResponseType.ACCEPT)
498 // Export the certificate in PEM format.
500 const string CERT_HEADER = "-----BEGIN CERTIFICATE-----\n";
501 const string CERT_FOOTER = "\n-----END CERTIFICATE-----\n";
503 // Strip any embedded newlines in the certificate...
504 string cert = id.trust_anchor.ca_cert.replace("\n", "");
506 // Re-embed newlines every 64 chars.
507 string newcert = CERT_HEADER;
508 while (cert.length > 63) {
509 newcert += cert[0:64];
511 cert = cert[64:cert.length];
513 if (cert.length > 0) {
516 newcert += CERT_FOOTER;
518 string filename = dialog.get_filename();
519 var file = File.new_for_path(filename);
520 var stream = file.replace(null, false, FileCreateFlags.PRIVATE);
521 stream.write(newcert.data);
523 // Save the parent directory to use as default for next save
524 export_directory = file.get_parent().get_path();