Newer Older
239 lines | 7.081kb
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') || '';
fix bug that ssh key is not ...
Yuki Kimoto authored on 2016-05-04
7
  my $user_id = param('user') || '';
add ssh keys page design
gitprep authored on 2014-05-17
8
  
9
  # Authentication
fix bug that ssh key is not ...
Yuki Kimoto authored on 2016-05-04
10
  unless ($api->logined($user_id)) {
add ssh keys page design
gitprep authored on 2014-05-17
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') {
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
20
      # Paramerters
21
      my $title = param('title');
22
      my $original_key = param('key');
add ssh key add feature
gitprep authored on 2014-05-17
23
      
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
24
      # Validator
25
      my $vc = app->vc;
add ssh key add feature
gitprep authored on 2014-05-17
26
      
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
27
      # Validation result
28
      my $validation = $vc->validation;
29
      
30
      # "title"
31
      if (!(defined $title && length $title)) {
32
        $validation->add_failed(title => 'title is empty');
33
      }
34
      elsif (!$vc->check($title, 'ascii_graphic')) {
35
        $validation->add_failed(title => 'title contains invalid character');
36
      }
37
      else {
38
        my $ssh_public_key = app->dbi->model('ssh_public_key')->select(
39
          where => {title => $title}
40
        )->one;
41
        
42
        if ($ssh_public_key) {
43
          $validation->add_failed(title => 'title already exists');
44
        }
45
      }
46
      
47
      # "key"
48
      my $key;
49
      if (!(defined $original_key && length $original_key)) {
50
        $validation->add_failed(key => 'key is empty');
51
      }
fix bug that ssh key is not ...
Yuki Kimoto authored on 2016-05-04
52
      elsif (length $original_key > 2000) {
53
        $validation->add_failed(key => 'key is too long');
54
      }
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
55
      else {
56
        my $type;
57
        my $original_key_edit;
58
        if ($original_key =~ /^(ssh-rsa|ssh-dss|ecdsa-sha2-nistp25|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521) +(\S+)/) {
59
          $type = $1;
60
          $original_key_edit = $2;
61
        }
62
        
63
        if (!$type) {
64
          my $message = "Key is invalid. It must begin with 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256',"
65
            . "'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. Check that you're copying the public half of the key";
66
          $validation->add_failed(key => $message);
67
        }
68
        elsif (!$vc->check($original_key_edit, 'ascii_graphic')) {
69
          $validation->add_failed(key => 'Key contains invalid character.');
70
        }
71
        else {
72
          $key = "$type $original_key_edit";
73
          
74
          my $row = app->dbi->model('ssh_public_key')->select(
75
            where => {key => $key}
76
          )->one;
77
          
78
          if ($row) {
79
            $validation->add_failed(key => 'Key already exists');
80
          }
81
          else {
82
            my $key_is_contained;
83
            my $authorized_keys_file = app->manager->authorized_keys_file;
84
            if (defined $authorized_keys_file) {
85
              my $result
86
                = app->manager->parse_authorized_keys_file($authorized_keys_file);
forbidden public key which i...
Yuki Kimoto authored on 2014-12-15
87

            
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
88
              my $before_part = $result->{before_part};
89
              my $after_part = $result->{after_part};
90
              my $other_part = "$before_part\n$after_part";
91
              if ($other_part =~ /\s\Q$original_key_edit\E(\s|$)/) {
92
                $key_is_contained = 1;
add ssh key add feature
gitprep authored on 2014-05-17
93
              }
94
            }
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
95
            
96
            if ($key_is_contained) {
97
              $validation->add_failed(key => "authorized_keys file already contain this key");
add ssh key add feature
gitprep authored on 2014-05-17
98
            }
99
          }
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
100
        }
101
      }
add ssh key add feature
gitprep authored on 2014-05-17
102
      
103
      # Register ssh key
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
104
      if ($validation->is_valid) {
fix bug that ssh key is not ...
Yuki Kimoto authored on 2016-05-04
105
        my $session_user_row_id = $api->session_user_row_id;
add ssh key add feature
gitprep authored on 2014-05-17
106
        my $p = {
fix bug that ssh key is not ...
Yuki Kimoto authored on 2016-05-04
107
          user => $session_user_row_id,
add ssh key add feature
gitprep authored on 2014-05-17
108
          title => $title,
109
          key => $key
110
        };
111
        eval {
add key delete feature and u...
gitprep authored on 2014-05-19
112
          app->dbi->connector->txn(sub {
113
            app->dbi->model('ssh_public_key')->insert($p);
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
114
            $self->app->manager->update_authorized_keys_file;
add key delete feature and u...
gitprep authored on 2014-05-19
115
          });
add ssh key add feature
gitprep authored on 2014-05-17
116
        };
add add ssh key form
gitprep authored on 2014-05-17
117
        
add ssh key add feature
gitprep authored on 2014-05-17
118
        if (my $e = $@) {
119
          app->log->error(url_for . ":$e");
120
          $errors = ['Internal error'];
add add ssh key form
gitprep authored on 2014-05-17
121
        }
122
        else {
add ssh key add feature
gitprep authored on 2014-05-17
123
          flash('message' => 'Success: ssh key is added');
124
          $self->redirect_to('current');
125
          return;
add add ssh key form
gitprep authored on 2014-05-17
126
        }
add ssh key add feature
gitprep authored on 2014-05-17
127
      }
128
      else {
cleanup ssh page validation
Yuki Kimoto authored on 2016-02-11
129
        $errors = $validation->messages;
add ssh key add feature
gitprep authored on 2014-05-17
130
      }
131
    }
add key delete feature and u...
gitprep authored on 2014-05-19
132
    # Delete ssh public key
133
    elsif ($op eq 'delete') {
134
      my $row_id = param('row-id');
fix bug that ssh key is not ...
Yuki Kimoto authored on 2016-05-04
135
      
add key delete feature and u...
gitprep authored on 2014-05-19
136
      eval {
fix update_authorized_keys_f...
gitprep authored on 2014-05-19
137
        app->dbi->connector->txn(sub {
138
          app->dbi->model('ssh_public_key')->delete(where => {row_id => $row_id});
139
          $self->app->manager->update_authorized_keys_file;
140
        });
add key delete feature and u...
gitprep authored on 2014-05-19
141
      };
142
      
143
      if (my $e = $@) {
144
        app->log->error(url_with . ": $e");
145
        $errors = ['Internal Error'];
146
      }
147
      else {
148
        flash(message => 'Success: a key is deleted');
149
        $self->redirect_to('current');
150
      }
151
    }
add ssh key add feature
gitprep authored on 2014-05-17
152
  }
add ssh keys page design
gitprep authored on 2014-05-17
153
  
fix bug that ssh key is not ...
Yuki Kimoto authored on 2016-05-04
154
  my $keys = app->dbi->model('ssh_public_key')->select(
155
    {
156
      __MY__ => '*'
157
    },
remove table __ prefix
Yuki Kimoto authored on 2016-06-11
158
    where => {'user.id' => $user_id},
fix bug that ssh key is not ...
Yuki Kimoto authored on 2016-05-04
159
    append => 'order by title'
160
  )->all;
add ssh key add feature
gitprep authored on 2014-05-17
161
%>
162

            
163
% layout 'common', title => 'SSH keys';
164

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