<% # API my $api = gitprep_api; # Parameters my $op = param('op') || ''; my $user = param('user') || ''; # Authentication unless ($api->logined($user)) { $self->redirect_to('/'); return; } # Process form my $errors; if (lc $self->req->method eq 'post') { # Add ssh key if ($op eq 'add') { # Paramters my $params = $api->params; # Rule my $rule = [ title => [ ['not_blank' => 'title is empty'], ['ascii' => 'title contains invalid character'], ], key => [ ['not_blank' => 'key is empty'], sub { my ($original_key, $args, $vc) = @_; my $type; my $original_key_edit; if ($original_key =~ /^(ssh-rsa|ssh-dss|ecdsa-sha2-nistp25|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521) +(\S+)/) { $type = $1; $original_key_edit = $2; } if ($type) { if ($vc->constraints->{ascii}->($original_key_edit)) { my $key = "$type $original_key_edit"; my $row = app->dbi->model('ssh_public_key')->select(id => [$user, $key])->one; if ($row) { return {result => 0, message => 'Key already exists'}; } else { return {result => 1, output => $key} } } else { return { result => 0, message => "Key contains invalid character." } } } else { return { result => 0, message => "Key is invalid. It must begin with 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256'," . "'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. Check that you're copying the public half of the key" }; } } ] ]; # Validation my $vresult = app->vc->validate($params, $rule); # Register ssh key if ($vresult->is_ok) { my $safe_params = $vresult->data; my $title = $safe_params->{title}; my $key = $safe_params->{key}; my $p = { user_id => $user, title => $title, key => $key }; eval { app->dbi->connector->txn(sub { app->dbi->model('ssh_public_key')->insert($p); # $self->manager->update_authorized_keys_file; }); }; if (my $e = $@) { app->log->error(url_for . ":$e"); $errors = ['Internal error']; } else { flash('message' => 'Success: ssh key is added'); $self->redirect_to('current'); return; } } else { $errors = $vresult->messages; } } # Delete ssh public key elsif ($op eq 'delete') { my $row_id = param('row-id'); eval { app->dbi->model('ssh_public_key')->delete(where => {row_id => $row_id}); }; if (my $e = $@) { app->log->error(url_with . ": $e"); $errors = ['Internal Error']; } else { flash(message => 'Success: a key is deleted'); $self->redirect_to('current'); } } } my $keys = app->dbi->model('ssh_public_key')->select(where => {user_id => $user})->all; %> % layout 'common', title => 'SSH keys'; %= include '/include/header';
<%= include '/include/errors', errors => $errors %> <%= include '/include/message', message => flash('message') %>
SSH Keys (">see)
% if (@$keys > 0) {
This is a list of SSH keys associated with your account. Remove any keys that you do not recognize.
% for my $key (@$keys) {
<%= $key->{title} %>
Delete <%= hidden_field 'row-id' => $key->{row_id} %>
% } % } else {
SSH key don't exists.
% }
Add an SSH Key
Title
<%= text_field 'title', style => "width:400px" %>
Key
<%= text_area 'key', style => "width:730px;height:200px" %>
%= include '/include/footer';