Newer Older
231 lines | 6.956kb
add ssh keys page design
gitprep authored on 2014-05-17
1
<%
2
  # API
3
  my $api = gitprep_api;
4
  
5
  # Parameters
6
  my $op = param('op') || '';
7
  my $user = param('user') || '';
8
  
9
  # Authentication
10
  unless ($api->logined($user)) {
11
    $self->redirect_to('/');
12
    return;
13
  }
14
  
add ssh key add feature
gitprep authored on 2014-05-17
15
  # Process form
16
  my $errors;
17
  if (lc $self->req->method eq 'post') {
18
    # Add ssh key
19
    if ($op eq 'add') {
20
      
21
      # Paramters
22
      my $params = $api->params;
23
      
24
      # Rule
25
      my $rule = [
26
        title => [
27
          ['not_blank' => 'title is empty'],
28
          ['ascii' => 'title contains invalid character'],
29
        ],
30
        key => [
31
          ['not_blank' => 'key is empty'],
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
32
          # Check if key format is valid
add ssh key add feature
gitprep authored on 2014-05-17
33
          sub {
34
            my ($original_key, $args, $vc) = @_;
35
            
36
            my $type;
37
            my $original_key_edit;
38
            if ($original_key =~ /^(ssh-rsa|ssh-dss|ecdsa-sha2-nistp25|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521) +(\S+)/) {
39
              $type = $1;
40
              $original_key_edit = $2;
41
            }
42
            
43
            if ($type) {
44
              if ($vc->constraints->{ascii}->($original_key_edit)) {
45
                my $key = "$type $original_key_edit";
46
                
fix ssh key authentication b...
gitprep authored on 2014-05-20
47
                my $row = app->dbi->model('ssh_public_key')->select(id => $key)->one;
add ssh key add feature
gitprep authored on 2014-05-17
48
                
49
                if ($row) {
50
                  return {result => 0, message => 'Key already exists'};
51
                }
52
                else {
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
53
                  my $key_is_contained;
54
                  my $authorized_keys_file = app->manager->authorized_keys_file;
55
                  if (defined $authorized_keys_file) {
56
                    my $result
57
                      = app->manager->parse_authorized_keys_file($authorized_keys_file);
58

            
59
                    my $before_part = $result->{before_part};
60
                    my $after_part = $result->{after_part};
61
                    my $other_part = "$before_part\n$after_part";
62
                    if ($other_part =~ /\s\Q$original_key_edit\E(\s|$)/) {
63
                      $key_is_contained = 1;
64
                    }
65
                  }
66
                  
67
                  if ($key_is_contained) {
68
                    return {
69
                      result => 0,
70
                      message => "authorized_keys file already contain this key"
71
                    };
72
                  }
73
                  else {
74
                    return {result => 1, output => $key}
75
                  }
add ssh key add feature
gitprep authored on 2014-05-17
76
                }
77
              }
78
              else {
79
                return {
80
                  result => 0,
81
                  message => "Key contains invalid character."
82
                }
83
              }
84
            }
85
            else {
86
              return {
87
                result => 0,
88
                message => "Key is invalid. It must begin with 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256',"
89
                  . "'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. Check that you're copying the public half of the key"
90
              };
91
            }
92
          }
93
        ]
94
      ];
95
      
96
      # Validation
97
      my $vresult = app->vc->validate($params, $rule);
98
      
99
      # Register ssh key
100
      if ($vresult->is_ok) {
101
        my $safe_params = $vresult->data;
102
        my $title = $safe_params->{title};
103
        my $key = $safe_params->{key};
104
        
105
        my $p = {
106
          user_id => $user,
107
          title => $title,
108
          key => $key
109
        };
110
        eval {
add key delete feature and u...
gitprep authored on 2014-05-19
111
          app->dbi->connector->txn(sub {
112
            app->dbi->model('ssh_public_key')->insert($p);
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
113
            $self->app->manager->update_authorized_keys_file;
add key delete feature and u...
gitprep authored on 2014-05-19
114
          });
add ssh key add feature
gitprep authored on 2014-05-17
115
        };
add add ssh key form
gitprep authored on 2014-05-17
116
        
add ssh key add feature
gitprep authored on 2014-05-17
117
        if (my $e = $@) {
118
          app->log->error(url_for . ":$e");
119
          $errors = ['Internal error'];
add add ssh key form
gitprep authored on 2014-05-17
120
        }
121
        else {
add ssh key add feature
gitprep authored on 2014-05-17
122
          flash('message' => 'Success: ssh key is added');
123
          $self->redirect_to('current');
124
          return;
add add ssh key form
gitprep authored on 2014-05-17
125
        }
add ssh key add feature
gitprep authored on 2014-05-17
126
      }
127
      else {
128
        $errors = $vresult->messages;
129
      }
130
    }
add key delete feature and u...
gitprep authored on 2014-05-19
131
    # Delete ssh public key
132
    elsif ($op eq 'delete') {
133
      my $row_id = param('row-id');
134
      eval {
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
135
        app->dbi->connector->txn(sub {
136
          app->dbi->model('ssh_public_key')->delete(where => {row_id => $row_id});
137
          $self->app->manager->update_authorized_keys_file;
138
        });
add key delete feature and u...
gitprep authored on 2014-05-19
139
      };
140
      
141
      if (my $e = $@) {
142
        app->log->error(url_with . ": $e");
143
        $errors = ['Internal Error'];
144
      }
145
      else {
146
        flash(message => 'Success: a key is deleted');
147
        $self->redirect_to('current');
148
      }
149
    }
add ssh key add feature
gitprep authored on 2014-05-17
150
  }
add ssh keys page design
gitprep authored on 2014-05-17
151
  
add ssh key add feature
gitprep authored on 2014-05-17
152
  my $keys = app->dbi->model('ssh_public_key')->select(where => {user_id => $user})->all;
153
%>
154

            
155
% layout 'common', title => 'SSH keys';
156

            
add ssh keys page design
gitprep authored on 2014-05-17
157
  %= include '/include/header';
158
  
159
  <div class="container">
improve settings ssh design
Yuki Kimoto authored on 2016-01-20
160
    <div class="user-settings">
161
      <div class="left">
162
        <ul>
163
          <li>
164
            <a href="<%= url_for("/$user/_settings") %>">Profile</a>
165
          </li>
166
          <li class="active">
167
            <a href="<%= url_for("/$user/_settings/ssh") %>">SSH keys</a>
168
          </li>
169
        </ul>
170
      </div>
171
      <div class="right">
172
        <%= include '/include/errors', errors => $errors %>
173
        <%= include '/include/message', message => flash('message') %>
174
        
175
        <div class="user-settings-container ssh-keys">
176
          <div>
177
            <div>
178
              <span>SSH Keys</span> (<a href="<%= url_for("/$user.keys") %>">see</a>)
add ssh keys page design
gitprep authored on 2014-05-17
179
            </div>
180
          </div>
improve settings ssh design
Yuki Kimoto authored on 2016-01-20
181
          <div>
add add ssh key form
gitprep authored on 2014-05-17
182
            % if (@$keys > 0) {
improve settings ssh design
Yuki Kimoto authored on 2016-01-20
183
              <div>
add add ssh key form
gitprep authored on 2014-05-17
184
                This is a list of SSH keys associated with your account. Remove any keys that you do not recognize.
185
              </div>
186
              % for my $key (@$keys) {
improve settings ssh design
Yuki Kimoto authored on 2016-01-20
187
                <form action="<%= url_for->query(op => 'delete') %>" method="post">
188
                  <div>
189
                    <b><%= $key->{title} %></b>
190
                    <a class="btn btn-small btn-danger" href="javascript:void(0)" onclick="$(this).closest('form').submit()">Delete</a>
191
                    <%= hidden_field 'row-id' => $key->{row_id} %>
add ssh keys page design
gitprep authored on 2014-05-17
192
                  </div>
add key delete feature and u...
gitprep authored on 2014-05-19
193
                </form>
add add ssh key form
gitprep authored on 2014-05-17
194
              % }
195
            % } else {
improve settings ssh design
Yuki Kimoto authored on 2016-01-20
196
              <div>
add add ssh key form
gitprep authored on 2014-05-17
197
                SSH key don't exists.
add ssh keys page design
gitprep authored on 2014-05-17
198
              </div>
199
            % }
add add ssh key form
gitprep authored on 2014-05-17
200
          </div>
improve settings ssh design
Yuki Kimoto authored on 2016-01-20
201
        </div>
202
        
203
        <div class="user-settings-container ssh-key-add">
add ssh key add feature
gitprep authored on 2014-05-17
204
          <div>
improve settings ssh design
Yuki Kimoto authored on 2016-01-20
205
            <div>
206
              Add an SSH Key
add ssh keys page design
gitprep authored on 2014-05-17
207
            </div>
add add ssh key form
gitprep authored on 2014-05-17
208
          </div>
improve settings ssh design
Yuki Kimoto authored on 2016-01-20
209
          <div>
210
            <form action="<%= url_for->query(op => 'add') %>" method="post" %>
211
              <div>
212
                Title
213
              </div>
214
              <div>
215
                <%= text_field 'title' %>
216
              </div>
217
              <div>
218
                Key
219
              </div>
220
              <div>
221
                <%= text_area 'key' %>
222
              </div>
223
              <input type="submit" class="btn btn-success" value="Add key">
224
            </form>
225
          </div>
add ssh keys page design
gitprep authored on 2014-05-17
226
        </div>
227
      </div>
228
    </div>
229
  </div>
230
  
231
  %= include '/include/footer';