Skip to content

Commit c426848

Browse files
committed
Add ability for SKA administrators to add pubkeys on behalf of other users
1 parent 9044f6e commit c426848

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

model/user.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ public function add_public_key(PublicKey $key) {
200200
$email->add_reply_to($config['email']['admin_address'], $config['email']['admin_name']);
201201
$email->add_recipient($this->email, $this->name);
202202
$email->add_cc($config['email']['report_address'], $config['email']['report_name']);
203-
$email->subject = "A new SSH public key has been added to your account ({$this->uid})";
204-
$email->body = "A new SSH public key has been added to your account on SSH Key Authority.\n\nIf you added this key then all is well. If you do not recall adding this key, please contact {$config['email']['admin_address']} immediately.\n\n".$key->summarize_key_information();
203+
if($active_user && $active_user->entity_id != $this->entity_id) {
204+
$email->subject = "A new SSH public key has been added to your account ({$this->uid}) by {$active_user->uid}";
205+
$email->body = "{$active_user->name} ({$active_user->uid}) has added a new SSH public key to your account on SSH Key Authority.\n\nIf you did not request this change, please contact {$config['email']['admin_address']} immediately.\n\n".$key->summarize_key_information();
206+
} else {
207+
$email->subject = "A new SSH public key has been added to your account ({$this->uid})";
208+
$email->body = "A new SSH public key has been added to your account on SSH Key Authority.\n\nIf you added this key then all is well. If you do not recall adding this key, please contact {$config['email']['admin_address']} immediately.\n\n".$key->summarize_key_information();
209+
}
205210
$email->send();
206211
$this->log(array('action' => 'Pubkey add', 'value' => $key->fingerprint_md5), LOG_WARNING);
207212
}

templates/user_pubkeys.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@
2525
<span class="glyphicon glyphicon-console"></span> JSON
2626
</a>
2727
</p>
28+
<?php if($this->get('allow_admin_add')) { ?>
29+
<div class="panel panel-default">
30+
<div class="panel-heading">
31+
<h2 class="panel-title">Add public key for <?php out($this->get('user')->name)?></h2>
32+
</div>
33+
<div class="panel-body">
34+
<form method="post" action="<?php outurl($this->data->relative_request_url) ?>">
35+
<?php out($this->get('active_user')->get_csrf_field(), ESC_NONE) ?>
36+
<div class="form-group">
37+
<label for="add_public_key">Public key</label>
38+
<textarea class="form-control" rows="4" id="add_public_key" name="add_public_key" required></textarea>
39+
</div>
40+
<p class="help-block">The key will be added to <?php out($this->get('user')->uid)?>.</p>
41+
<button type="submit" class="btn btn-primary">Add public key</button>
42+
</form>
43+
</div>
44+
</div>
45+
<?php } ?>
2846
<?php foreach($this->get('pubkeys') as $pubkey) { ?>
2947
<div class="panel panel-default">
3048
<dl class="panel-body">

views/user_pubkeys.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@
2121
require('views/error404.php');
2222
die;
2323
}
24+
25+
$is_target_active_user = $active_user && $active_user->entity_id == $user->entity_id;
26+
$can_admin_add_for_user = $active_user && $active_user->admin && !$is_target_active_user;
27+
$can_submit_key = $is_target_active_user || $can_admin_add_for_user;
28+
29+
if(isset($_POST['add_public_key'])) {
30+
if(!$can_submit_key) {
31+
require('views/error403.php');
32+
die;
33+
}
34+
try {
35+
$public_key = new PublicKey;
36+
$public_key->import($_POST['add_public_key'], $user->uid);
37+
$user->add_public_key($public_key);
38+
redirect();
39+
} catch(InvalidArgumentException $e) {
40+
global $config;
41+
$content = new PageSection('key_upload_fail');
42+
$error_message = $e->getMessage();
43+
if(preg_match('/^Insufficient bits in public key: (\d+) < (\d+)$/', $error_message, $matches)) {
44+
$actual_bits = $matches[1];
45+
$required_bits = $matches[2];
46+
$content->set('message', "The public key you submitted is of insufficient strength; it has {$actual_bits} bits but must be at least {$required_bits} bits.");
47+
} else {
48+
$content->set('message', "The public key you submitted doesn't look valid.");
49+
}
50+
}
51+
}
52+
2453
$pubkeys = $user->list_public_keys();
2554
if(isset($router->vars['format']) && $router->vars['format'] == 'txt') {
2655
$page = new PageSection('entity_pubkeys_txt');
@@ -33,16 +62,19 @@
3362
header('Content-type: application/json; charset=utf-8');
3463
echo $page->generate();
3564
} else {
36-
$content = new PageSection('user_pubkeys');
37-
$content->set('user', $user);
38-
$content->set('pubkeys', $pubkeys);
39-
$content->set('admin', $active_user->admin);
65+
$head = '<link rel="alternate" type="application/json" href="pubkeys.json" title="JSON for this page">' . "\n";
66+
$head .= '<link rel="alternate" type="text/plain" href="pubkeys.txt" title="TXT format for this page">' . "\n";
4067

41-
$head = '<link rel="alternate" type="application/json" href="pubkeys.json" title="JSON for this page">'."\n";
42-
$head .= '<link rel="alternate" type="text/plain" href="pubkeys.txt" title="TXT format for this page">'."\n";
68+
if(!isset($content)) {
69+
$content = new PageSection('user_pubkeys');
70+
$content->set('user', $user);
71+
$content->set('pubkeys', $pubkeys);
72+
$content->set('admin', $active_user ? $active_user->admin : false);
73+
$content->set('allow_admin_add', $can_admin_add_for_user);
74+
}
4375

4476
$page = new PageSection('base');
45-
$page->set('title', 'Public keys for '.$user->name);
77+
$page->set('title', 'Public keys for ' . $user->name);
4678
$page->set('head', $head);
4779
$page->set('content', $content);
4880
$page->set('alerts', $active_user->pop_alerts());

0 commit comments

Comments
 (0)