]>
Commit | Line | Data |
---|---|---|
1 | package PVE::API2::User; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Exception qw(raise raise_perm_exc raise_param_exc); | |
7 | use PVE::Cluster qw (cfs_read_file cfs_write_file); | |
8 | use PVE::Tools qw(split_list extract_param); | |
9 | use PVE::JSONSchema qw(get_standard_option register_standard_option); | |
10 | use PVE::SafeSyslog; | |
11 | ||
12 | use PVE::AccessControl; | |
13 | use PVE::Auth::Plugin; | |
14 | use PVE::TokenConfig; | |
15 | ||
16 | use PVE::RESTHandler; | |
17 | ||
18 | use base qw(PVE::RESTHandler); | |
19 | ||
20 | register_standard_option('user-enable', { | |
21 | description => "Enable the account (default). You can set this to '0' to disable the account", | |
22 | type => 'boolean', | |
23 | optional => 1, | |
24 | default => 1, | |
25 | }); | |
26 | register_standard_option('user-expire', { | |
27 | description => "Account expiration date (seconds since epoch). '0' means no expiration date.", | |
28 | type => 'integer', | |
29 | minimum => 0, | |
30 | optional => 1, | |
31 | }); | |
32 | register_standard_option('user-firstname', { type => 'string', optional => 1 }); | |
33 | register_standard_option('user-lastname', { type => 'string', optional => 1 }); | |
34 | register_standard_option('user-email', { type => 'string', optional => 1, format => 'email-opt' }); | |
35 | register_standard_option('user-comment', { type => 'string', optional => 1 }); | |
36 | register_standard_option('user-keys', { | |
37 | description => "Keys for two factor auth (yubico).", | |
38 | type => 'string', | |
39 | optional => 1, | |
40 | }); | |
41 | register_standard_option('group-list', { | |
42 | type => 'string', format => 'pve-groupid-list', | |
43 | optional => 1, | |
44 | completion => \&PVE::AccessControl::complete_group, | |
45 | }); | |
46 | register_standard_option('token-subid', { | |
47 | type => 'string', | |
48 | pattern => $PVE::AccessControl::token_subid_regex, | |
49 | description => 'User-specific token identifier.', | |
50 | }); | |
51 | register_standard_option('token-expire', { | |
52 | description => "API token expiration date (seconds since epoch). '0' means no expiration date.", | |
53 | type => 'integer', | |
54 | minimum => 0, | |
55 | optional => 1, | |
56 | default => 'same as user', | |
57 | }); | |
58 | register_standard_option('token-privsep', { | |
59 | description => "Restrict API token privileges with separate ACLs (default), or give full privileges of corresponding user.", | |
60 | type => 'boolean', | |
61 | optional => 1, | |
62 | default => 1, | |
63 | }); | |
64 | register_standard_option('token-comment', { type => 'string', optional => 1 }); | |
65 | register_standard_option('token-info', { | |
66 | type => 'object', | |
67 | properties => { | |
68 | expire => get_standard_option('token-expire'), | |
69 | privsep => get_standard_option('token-privsep'), | |
70 | comment => get_standard_option('token-comment'), | |
71 | } | |
72 | }); | |
73 | ||
74 | my $token_info_extend = sub { | |
75 | my ($props) = @_; | |
76 | ||
77 | my $obj = get_standard_option('token-info'); | |
78 | my $base_props = $obj->{properties}; | |
79 | $obj->{properties} = {}; | |
80 | ||
81 | foreach my $prop (keys %$base_props) { | |
82 | $obj->{properties}->{$prop} = $base_props->{$prop}; | |
83 | } | |
84 | ||
85 | foreach my $add_prop (keys %$props) { | |
86 | $obj->{properties}->{$add_prop} = $props->{$add_prop}; | |
87 | } | |
88 | ||
89 | return $obj; | |
90 | }; | |
91 | ||
92 | my $extract_user_data = sub { | |
93 | my ($data, $full) = @_; | |
94 | ||
95 | my $res = {}; | |
96 | ||
97 | foreach my $prop (qw(enable expire firstname lastname email comment keys)) { | |
98 | $res->{$prop} = $data->{$prop} if defined($data->{$prop}); | |
99 | } | |
100 | ||
101 | return $res if !$full; | |
102 | ||
103 | $res->{groups} = $data->{groups} ? [ keys %{$data->{groups}} ] : []; | |
104 | $res->{tokens} = $data->{tokens}; | |
105 | ||
106 | return $res; | |
107 | }; | |
108 | ||
109 | __PACKAGE__->register_method ({ | |
110 | name => 'index', | |
111 | path => '', | |
112 | method => 'GET', | |
113 | description => "User index.", | |
114 | permissions => { | |
115 | description => "The returned list is restricted to users where you have 'User.Modify' or 'Sys.Audit' permissions on '/access/groups' or on a group the user belongs too. But it always includes the current (authenticated) user.", | |
116 | user => 'all', | |
117 | }, | |
118 | parameters => { | |
119 | additionalProperties => 0, | |
120 | properties => { | |
121 | enabled => { | |
122 | type => 'boolean', | |
123 | description => "Optional filter for enable property.", | |
124 | optional => 1, | |
125 | }, | |
126 | full => { | |
127 | type => 'boolean', | |
128 | description => "Include group and token information.", | |
129 | optional => 1, | |
130 | default => 0, | |
131 | } | |
132 | }, | |
133 | }, | |
134 | returns => { | |
135 | type => 'array', | |
136 | items => { | |
137 | type => "object", | |
138 | properties => { | |
139 | userid => get_standard_option('userid-completed'), | |
140 | enable => get_standard_option('user-enable'), | |
141 | expire => get_standard_option('user-expire'), | |
142 | firstname => get_standard_option('user-firstname'), | |
143 | lastname => get_standard_option('user-lastname'), | |
144 | email => get_standard_option('user-email'), | |
145 | comment => get_standard_option('user-comment'), | |
146 | keys => get_standard_option('user-keys'), | |
147 | groups => get_standard_option('group-list'), | |
148 | tokens => { | |
149 | type => 'array', | |
150 | optional => 1, | |
151 | items => $token_info_extend->({ | |
152 | tokenid => get_standard_option('token-subid'), | |
153 | }), | |
154 | }, | |
155 | 'realm-type' => { | |
156 | type => 'string', format => 'pve-realm', | |
157 | description => 'The type of the users realm', | |
158 | optional => 1, # it should always be there, but we use conditional code below, so.. | |
159 | }, | |
160 | }, | |
161 | }, | |
162 | links => [ { rel => 'child', href => "{userid}" } ], | |
163 | }, | |
164 | code => sub { | |
165 | my ($param) = @_; | |
166 | ||
167 | my $rpcenv = PVE::RPCEnvironment::get(); | |
168 | my $usercfg = $rpcenv->{user_cfg}; | |
169 | my $authuser = $rpcenv->get_user(); | |
170 | ||
171 | my $domainscfg = cfs_read_file('domains.cfg'); | |
172 | my $domainids = $domainscfg->{ids}; | |
173 | ||
174 | my $res = []; | |
175 | ||
176 | my $privs = [ 'User.Modify', 'Sys.Audit' ]; | |
177 | my $canUserMod = $rpcenv->check_any($authuser, "/access/groups", $privs, 1); | |
178 | my $groups = $rpcenv->filter_groups($authuser, $privs, 1); | |
179 | my $allowed_users = $rpcenv->group_member_join([keys %$groups]); | |
180 | ||
181 | foreach my $user (sort keys %{$usercfg->{users}}) { | |
182 | if (!($canUserMod || $user eq $authuser)) { | |
183 | next if !$allowed_users->{$user}; | |
184 | } | |
185 | ||
186 | my $entry = $extract_user_data->($usercfg->{users}->{$user}, $param->{full}); | |
187 | ||
188 | if (defined($param->{enabled})) { | |
189 | next if $entry->{enable} && !$param->{enabled}; | |
190 | next if !$entry->{enable} && $param->{enabled}; | |
191 | } | |
192 | ||
193 | $entry->{groups} = join(',', @{$entry->{groups}}) if $entry->{groups}; | |
194 | ||
195 | if (defined(my $tokens = $entry->{tokens})) { | |
196 | $entry->{tokens} = [ | |
197 | map { { tokenid => $_, %{$tokens->{$_}} } } sort keys %$tokens | |
198 | ]; | |
199 | } | |
200 | ||
201 | if ($user =~ /($PVE::Auth::Plugin::realm_regex)$/) { | |
202 | my $realm = $1; | |
203 | $entry->{'realm-type'} = $domainids->{$realm}->{type} if exists $domainids->{$realm}; | |
204 | } | |
205 | ||
206 | $entry->{userid} = $user; | |
207 | ||
208 | push @$res, $entry; | |
209 | } | |
210 | ||
211 | return $res; | |
212 | }}); | |
213 | ||
214 | __PACKAGE__->register_method ({ | |
215 | name => 'create_user', | |
216 | protected => 1, | |
217 | path => '', | |
218 | method => 'POST', | |
219 | permissions => { | |
220 | description => "You need 'Realm.AllocateUser' on '/access/realm/<realm>' on the realm of user <userid>, and 'User.Modify' permissions to '/access/groups/<group>' for any group specified (or 'User.Modify' on '/access/groups' if you pass no groups.", | |
221 | check => [ | |
222 | 'and', | |
223 | [ 'userid-param', 'Realm.AllocateUser'], | |
224 | [ 'userid-group', ['User.Modify'], groups_param => 1], | |
225 | ], | |
226 | }, | |
227 | description => "Create new user.", | |
228 | parameters => { | |
229 | additionalProperties => 0, | |
230 | properties => { | |
231 | userid => get_standard_option('userid-completed'), | |
232 | enable => get_standard_option('user-enable'), | |
233 | expire => get_standard_option('user-expire'), | |
234 | firstname => get_standard_option('user-firstname'), | |
235 | lastname => get_standard_option('user-lastname'), | |
236 | email => get_standard_option('user-email'), | |
237 | comment => get_standard_option('user-comment'), | |
238 | keys => get_standard_option('user-keys'), | |
239 | password => { | |
240 | description => "Initial password.", | |
241 | type => 'string', | |
242 | optional => 1, | |
243 | minLength => 5, | |
244 | maxLength => 64 | |
245 | }, | |
246 | groups => get_standard_option('group-list'), | |
247 | }, | |
248 | }, | |
249 | returns => { type => 'null' }, | |
250 | code => sub { | |
251 | my ($param) = @_; | |
252 | ||
253 | PVE::AccessControl::lock_user_config(sub { | |
254 | my ($username, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
255 | ||
256 | my $usercfg = cfs_read_file("user.cfg"); | |
257 | ||
258 | # ensure "user exists" check works for case insensitive realms | |
259 | $username = PVE::AccessControl::lookup_username($username, 1); | |
260 | die "user '$username' already exists\n" if $usercfg->{users}->{$username}; | |
261 | ||
262 | PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password}) | |
263 | if defined($param->{password}); | |
264 | ||
265 | my $enable = defined($param->{enable}) ? $param->{enable} : 1; | |
266 | $usercfg->{users}->{$username} = { enable => $enable }; | |
267 | $usercfg->{users}->{$username}->{expire} = $param->{expire} if $param->{expire}; | |
268 | ||
269 | if ($param->{groups}) { | |
270 | foreach my $group (split_list($param->{groups})) { | |
271 | if ($usercfg->{groups}->{$group}) { | |
272 | PVE::AccessControl::add_user_group($username, $usercfg, $group); | |
273 | } else { | |
274 | die "no such group '$group'\n"; | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
279 | $usercfg->{users}->{$username}->{firstname} = $param->{firstname} if $param->{firstname}; | |
280 | $usercfg->{users}->{$username}->{lastname} = $param->{lastname} if $param->{lastname}; | |
281 | $usercfg->{users}->{$username}->{email} = $param->{email} if $param->{email}; | |
282 | $usercfg->{users}->{$username}->{comment} = $param->{comment} if $param->{comment}; | |
283 | $usercfg->{users}->{$username}->{keys} = $param->{keys} if $param->{keys}; | |
284 | ||
285 | cfs_write_file("user.cfg", $usercfg); | |
286 | }, "create user failed"); | |
287 | ||
288 | return undef; | |
289 | }}); | |
290 | ||
291 | __PACKAGE__->register_method ({ | |
292 | name => 'read_user', | |
293 | path => '{userid}', | |
294 | method => 'GET', | |
295 | description => "Get user configuration.", | |
296 | permissions => { | |
297 | check => ['userid-group', ['User.Modify', 'Sys.Audit']], | |
298 | }, | |
299 | parameters => { | |
300 | additionalProperties => 0, | |
301 | properties => { | |
302 | userid => get_standard_option('userid-completed'), | |
303 | }, | |
304 | }, | |
305 | returns => { | |
306 | additionalProperties => 0, | |
307 | properties => { | |
308 | enable => get_standard_option('user-enable'), | |
309 | expire => get_standard_option('user-expire'), | |
310 | firstname => get_standard_option('user-firstname'), | |
311 | lastname => get_standard_option('user-lastname'), | |
312 | email => get_standard_option('user-email'), | |
313 | comment => get_standard_option('user-comment'), | |
314 | keys => get_standard_option('user-keys'), | |
315 | groups => { | |
316 | type => 'array', | |
317 | optional => 1, | |
318 | items => { | |
319 | type => 'string', | |
320 | format => 'pve-groupid', | |
321 | }, | |
322 | }, | |
323 | tokens => { | |
324 | optional => 1, | |
325 | type => 'object', | |
326 | }, | |
327 | }, | |
328 | type => "object" | |
329 | }, | |
330 | code => sub { | |
331 | my ($param) = @_; | |
332 | ||
333 | my ($username, undef, $domain) = PVE::AccessControl::verify_username($param->{userid}); | |
334 | ||
335 | my $usercfg = cfs_read_file("user.cfg"); | |
336 | ||
337 | my $data = PVE::AccessControl::check_user_exist($usercfg, $username); | |
338 | ||
339 | return &$extract_user_data($data, 1); | |
340 | }}); | |
341 | ||
342 | __PACKAGE__->register_method ({ | |
343 | name => 'update_user', | |
344 | protected => 1, | |
345 | path => '{userid}', | |
346 | method => 'PUT', | |
347 | permissions => { | |
348 | check => ['userid-group', ['User.Modify'], groups_param => 1 ], | |
349 | }, | |
350 | description => "Update user configuration.", | |
351 | parameters => { | |
352 | additionalProperties => 0, | |
353 | properties => { | |
354 | userid => get_standard_option('userid-completed'), | |
355 | enable => get_standard_option('user-enable'), | |
356 | expire => get_standard_option('user-expire'), | |
357 | firstname => get_standard_option('user-firstname'), | |
358 | lastname => get_standard_option('user-lastname'), | |
359 | email => get_standard_option('user-email'), | |
360 | comment => get_standard_option('user-comment'), | |
361 | keys => get_standard_option('user-keys'), | |
362 | groups => get_standard_option('group-list'), | |
363 | append => { | |
364 | type => 'boolean', | |
365 | optional => 1, | |
366 | requires => 'groups', | |
367 | }, | |
368 | }, | |
369 | }, | |
370 | returns => { type => 'null' }, | |
371 | code => sub { | |
372 | my ($param) = @_; | |
373 | ||
374 | my ($username, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
375 | ||
376 | PVE::AccessControl::lock_user_config(sub { | |
377 | my $usercfg = cfs_read_file("user.cfg"); | |
378 | ||
379 | PVE::AccessControl::check_user_exist($usercfg, $username); | |
380 | ||
381 | $usercfg->{users}->{$username}->{enable} = $param->{enable} if defined($param->{enable}); | |
382 | $usercfg->{users}->{$username}->{expire} = $param->{expire} if defined($param->{expire}); | |
383 | ||
384 | PVE::AccessControl::delete_user_group($username, $usercfg) | |
385 | if (!$param->{append} && defined($param->{groups})); | |
386 | ||
387 | if ($param->{groups}) { | |
388 | foreach my $group (split_list($param->{groups})) { | |
389 | if ($usercfg->{groups}->{$group}) { | |
390 | PVE::AccessControl::add_user_group($username, $usercfg, $group); | |
391 | } else { | |
392 | die "no such group '$group'\n"; | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | $usercfg->{users}->{$username}->{firstname} = $param->{firstname} if defined($param->{firstname}); | |
398 | $usercfg->{users}->{$username}->{lastname} = $param->{lastname} if defined($param->{lastname}); | |
399 | $usercfg->{users}->{$username}->{email} = $param->{email} if defined($param->{email}); | |
400 | $usercfg->{users}->{$username}->{comment} = $param->{comment} if defined($param->{comment}); | |
401 | $usercfg->{users}->{$username}->{keys} = $param->{keys} if defined($param->{keys}); | |
402 | ||
403 | cfs_write_file("user.cfg", $usercfg); | |
404 | }, "update user failed"); | |
405 | ||
406 | return undef; | |
407 | }}); | |
408 | ||
409 | __PACKAGE__->register_method ({ | |
410 | name => 'delete_user', | |
411 | protected => 1, | |
412 | path => '{userid}', | |
413 | method => 'DELETE', | |
414 | description => "Delete user.", | |
415 | permissions => { | |
416 | check => [ 'and', | |
417 | [ 'userid-param', 'Realm.AllocateUser'], | |
418 | [ 'userid-group', ['User.Modify']], | |
419 | ], | |
420 | }, | |
421 | parameters => { | |
422 | additionalProperties => 0, | |
423 | properties => { | |
424 | userid => get_standard_option('userid-completed'), | |
425 | } | |
426 | }, | |
427 | returns => { type => 'null' }, | |
428 | code => sub { | |
429 | my ($param) = @_; | |
430 | ||
431 | my $rpcenv = PVE::RPCEnvironment::get(); | |
432 | my $authuser = $rpcenv->get_user(); | |
433 | ||
434 | my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
435 | ||
436 | PVE::AccessControl::lock_user_config(sub { | |
437 | my $usercfg = cfs_read_file("user.cfg"); | |
438 | ||
439 | my $domain_cfg = cfs_read_file('domains.cfg'); | |
440 | if (my $cfg = $domain_cfg->{ids}->{$realm}) { | |
441 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
442 | $plugin->delete_user($cfg, $realm, $ruid); | |
443 | } | |
444 | ||
445 | # Remove TFA data before removing the user entry as the user entry tells us whether | |
446 | # we need ot update priv/tfa.cfg. | |
447 | PVE::AccessControl::user_set_tfa($userid, $realm, undef, undef, $usercfg, $domain_cfg); | |
448 | ||
449 | delete $usercfg->{users}->{$userid}; | |
450 | ||
451 | PVE::AccessControl::delete_user_group($userid, $usercfg); | |
452 | PVE::AccessControl::delete_user_acl($userid, $usercfg); | |
453 | cfs_write_file("user.cfg", $usercfg); | |
454 | }, "delete user failed"); | |
455 | ||
456 | return undef; | |
457 | }}); | |
458 | ||
459 | __PACKAGE__->register_method ({ | |
460 | name => 'read_user_tfa_type', | |
461 | path => '{userid}/tfa', | |
462 | method => 'GET', | |
463 | protected => 1, | |
464 | description => "Get user TFA types (Personal and Realm).", | |
465 | permissions => { | |
466 | check => [ 'or', | |
467 | ['userid-param', 'self'], | |
468 | ['userid-group', ['User.Modify', 'Sys.Audit']], | |
469 | ], | |
470 | }, | |
471 | parameters => { | |
472 | additionalProperties => 0, | |
473 | properties => { | |
474 | userid => get_standard_option('userid-completed'), | |
475 | }, | |
476 | }, | |
477 | returns => { | |
478 | additionalProperties => 0, | |
479 | properties => { | |
480 | realm => { | |
481 | type => 'string', | |
482 | enum => [qw(oath yubico)], | |
483 | description => "The type of TFA the users realm has set, if any.", | |
484 | optional => 1, | |
485 | }, | |
486 | user => { | |
487 | type => 'string', | |
488 | enum => [qw(oath u2f)], | |
489 | description => "The type of TFA the user has set, if any.", | |
490 | optional => 1, | |
491 | }, | |
492 | }, | |
493 | type => "object" | |
494 | }, | |
495 | code => sub { | |
496 | my ($param) = @_; | |
497 | ||
498 | my ($username, undef, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
499 | ||
500 | my $domain_cfg = cfs_read_file('domains.cfg'); | |
501 | my $realm_cfg = $domain_cfg->{ids}->{$realm}; | |
502 | die "auth domain '$realm' does not exist\n" if !$realm_cfg; | |
503 | ||
504 | my $realm_tfa = {}; | |
505 | $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_cfg->{tfa}) if $realm_cfg->{tfa}; | |
506 | ||
507 | my $tfa_cfg = cfs_read_file('priv/tfa.cfg'); | |
508 | my $tfa = $tfa_cfg->{users}->{$username}; | |
509 | ||
510 | my $res = {}; | |
511 | $res->{realm} = $realm_tfa->{type} if $realm_tfa->{type}; | |
512 | $res->{user} = $tfa->{type} if $tfa->{type}; | |
513 | return $res; | |
514 | }}); | |
515 | ||
516 | __PACKAGE__->register_method ({ | |
517 | name => 'token_index', | |
518 | path => '{userid}/token', | |
519 | method => 'GET', | |
520 | description => "Get user API tokens.", | |
521 | permissions => { | |
522 | check => [ | |
523 | 'or', | |
524 | ['userid-param', 'self'], | |
525 | ['perm', '/access/users/{userid}', ['User.Modify']], | |
526 | ], | |
527 | }, | |
528 | parameters => { | |
529 | additionalProperties => 0, | |
530 | properties => { | |
531 | userid => get_standard_option('userid-completed'), | |
532 | }, | |
533 | }, | |
534 | returns => { | |
535 | type => "array", | |
536 | items => $token_info_extend->({ | |
537 | tokenid => get_standard_option('token-subid'), | |
538 | }), | |
539 | links => [ { rel => 'child', href => "{tokenid}" } ], | |
540 | }, | |
541 | code => sub { | |
542 | my ($param) = @_; | |
543 | ||
544 | my $userid = PVE::AccessControl::verify_username($param->{userid}); | |
545 | my $usercfg = cfs_read_file("user.cfg"); | |
546 | ||
547 | my $user = PVE::AccessControl::check_user_exist($usercfg, $userid); | |
548 | ||
549 | my $tokens = $user->{tokens} // {}; | |
550 | return [ map { $tokens->{$_}->{tokenid} = $_; $tokens->{$_} } keys %$tokens]; | |
551 | }}); | |
552 | ||
553 | __PACKAGE__->register_method ({ | |
554 | name => 'read_token', | |
555 | path => '{userid}/token/{tokenid}', | |
556 | method => 'GET', | |
557 | description => "Get specific API token information.", | |
558 | permissions => { | |
559 | check => [ | |
560 | 'or', | |
561 | ['userid-param', 'self'], | |
562 | ['perm', '/access/users/{userid}', ['User.Modify']], | |
563 | ], | |
564 | }, | |
565 | parameters => { | |
566 | additionalProperties => 0, | |
567 | properties => { | |
568 | userid => get_standard_option('userid-completed'), | |
569 | tokenid => get_standard_option('token-subid'), | |
570 | }, | |
571 | }, | |
572 | returns => get_standard_option('token-info'), | |
573 | code => sub { | |
574 | my ($param) = @_; | |
575 | ||
576 | my $userid = PVE::AccessControl::verify_username($param->{userid}); | |
577 | my $tokenid = $param->{tokenid}; | |
578 | ||
579 | my $usercfg = cfs_read_file("user.cfg"); | |
580 | ||
581 | return PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid); | |
582 | }}); | |
583 | ||
584 | __PACKAGE__->register_method ({ | |
585 | name => 'generate_token', | |
586 | path => '{userid}/token/{tokenid}', | |
587 | method => 'POST', | |
588 | description => "Generate a new API token for a specific user. NOTE: returns API token value, which needs to be stored as it cannot be retrieved afterwards!", | |
589 | protected => 1, | |
590 | permissions => { | |
591 | check => [ | |
592 | 'or', | |
593 | ['userid-param', 'self'], | |
594 | ['perm', '/access/users/{userid}', ['User.Modify']], | |
595 | ], | |
596 | }, | |
597 | parameters => { | |
598 | additionalProperties => 0, | |
599 | properties => { | |
600 | userid => get_standard_option('userid-completed'), | |
601 | tokenid => get_standard_option('token-subid'), | |
602 | expire => get_standard_option('token-expire'), | |
603 | privsep => get_standard_option('token-privsep'), | |
604 | comment => get_standard_option('token-comment'), | |
605 | }, | |
606 | }, | |
607 | returns => { | |
608 | additionalProperties => 0, | |
609 | type => "object", | |
610 | properties => { | |
611 | info => get_standard_option('token-info'), | |
612 | value => { | |
613 | type => 'string', | |
614 | description => 'API token value used for authentication.', | |
615 | }, | |
616 | 'full-tokenid' => { | |
617 | type => 'string', | |
618 | format_description => '<userid>!<tokenid>', | |
619 | description => 'The full token id.', | |
620 | }, | |
621 | }, | |
622 | }, | |
623 | code => sub { | |
624 | my ($param) = @_; | |
625 | ||
626 | my $userid = PVE::AccessControl::verify_username(extract_param($param, 'userid')); | |
627 | my $tokenid = extract_param($param, 'tokenid'); | |
628 | ||
629 | my $usercfg = cfs_read_file("user.cfg"); | |
630 | ||
631 | my $token = PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid, 1); | |
632 | my ($full_tokenid, $value); | |
633 | ||
634 | PVE::AccessControl::check_user_exist($usercfg, $userid); | |
635 | raise_param_exc({ 'tokenid' => 'Token already exists.' }) if defined($token); | |
636 | ||
637 | my $generate_and_add_token = sub { | |
638 | $usercfg = cfs_read_file("user.cfg"); | |
639 | PVE::AccessControl::check_user_exist($usercfg, $userid); | |
640 | die "Token already exists.\n" if defined(PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid, 1)); | |
641 | ||
642 | $full_tokenid = PVE::AccessControl::join_tokenid($userid, $tokenid); | |
643 | $value = PVE::TokenConfig::generate_token($full_tokenid); | |
644 | ||
645 | $token = {}; | |
646 | $token->{privsep} = defined($param->{privsep}) ? $param->{privsep} : 1; | |
647 | $token->{expire} = $param->{expire} if defined($param->{expire}); | |
648 | $token->{comment} = $param->{comment} if $param->{comment}; | |
649 | ||
650 | $usercfg->{users}->{$userid}->{tokens}->{$tokenid} = $token; | |
651 | cfs_write_file("user.cfg", $usercfg); | |
652 | }; | |
653 | ||
654 | PVE::AccessControl::lock_user_config($generate_and_add_token, 'generating token failed'); | |
655 | ||
656 | return { | |
657 | info => $token, | |
658 | value => $value, | |
659 | 'full-tokenid' => $full_tokenid, | |
660 | }; | |
661 | }}); | |
662 | ||
663 | ||
664 | __PACKAGE__->register_method ({ | |
665 | name => 'update_token_info', | |
666 | path => '{userid}/token/{tokenid}', | |
667 | method => 'PUT', | |
668 | description => "Update API token for a specific user.", | |
669 | protected => 1, | |
670 | permissions => { | |
671 | check => [ | |
672 | 'or', | |
673 | ['userid-param', 'self'], | |
674 | ['perm', '/access/users/{userid}', ['User.Modify']], | |
675 | ], | |
676 | }, | |
677 | parameters => { | |
678 | additionalProperties => 0, | |
679 | properties => { | |
680 | userid => get_standard_option('userid-completed'), | |
681 | tokenid => get_standard_option('token-subid'), | |
682 | expire => get_standard_option('token-expire'), | |
683 | privsep => get_standard_option('token-privsep'), | |
684 | comment => get_standard_option('token-comment'), | |
685 | }, | |
686 | }, | |
687 | returns => get_standard_option('token-info', { description => "Updated token information." }), | |
688 | code => sub { | |
689 | my ($param) = @_; | |
690 | ||
691 | my $userid = PVE::AccessControl::verify_username(extract_param($param, 'userid')); | |
692 | my $tokenid = extract_param($param, 'tokenid'); | |
693 | ||
694 | my $usercfg = cfs_read_file("user.cfg"); | |
695 | my $token = PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid); | |
696 | ||
697 | PVE::AccessControl::lock_user_config(sub { | |
698 | $usercfg = cfs_read_file("user.cfg"); | |
699 | $token = PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid); | |
700 | ||
701 | my $full_tokenid = PVE::AccessControl::join_tokenid($userid, $tokenid); | |
702 | ||
703 | $token->{privsep} = $param->{privsep} if defined($param->{privsep}); | |
704 | $token->{expire} = $param->{expire} if defined($param->{expire}); | |
705 | $token->{comment} = $param->{comment} if $param->{comment}; | |
706 | ||
707 | $usercfg->{users}->{$userid}->{tokens}->{$tokenid} = $token; | |
708 | cfs_write_file("user.cfg", $usercfg); | |
709 | }, 'updating token info failed'); | |
710 | ||
711 | return $token; | |
712 | }}); | |
713 | ||
714 | ||
715 | __PACKAGE__->register_method ({ | |
716 | name => 'remove_token', | |
717 | path => '{userid}/token/{tokenid}', | |
718 | method => 'DELETE', | |
719 | description => "Remove API token for a specific user.", | |
720 | protected => 1, | |
721 | permissions => { | |
722 | check => [ | |
723 | 'or', | |
724 | ['userid-param', 'self'], | |
725 | ['perm', '/access/users/{userid}', ['User.Modify']], | |
726 | ], | |
727 | }, | |
728 | parameters => { | |
729 | additionalProperties => 0, | |
730 | properties => { | |
731 | userid => get_standard_option('userid-completed'), | |
732 | tokenid => get_standard_option('token-subid'), | |
733 | }, | |
734 | }, | |
735 | returns => { type => 'null' }, | |
736 | code => sub { | |
737 | my ($param) = @_; | |
738 | ||
739 | my $userid = PVE::AccessControl::verify_username(extract_param($param, 'userid')); | |
740 | my $tokenid = extract_param($param, 'tokenid'); | |
741 | ||
742 | my $usercfg = cfs_read_file("user.cfg"); | |
743 | my $token = PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid); | |
744 | ||
745 | PVE::AccessControl::lock_user_config(sub { | |
746 | $usercfg = cfs_read_file("user.cfg"); | |
747 | ||
748 | PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid); | |
749 | ||
750 | my $full_tokenid = PVE::AccessControl::join_tokenid($userid, $tokenid); | |
751 | PVE::TokenConfig::delete_token($full_tokenid); | |
752 | delete $usercfg->{users}->{$userid}->{tokens}->{$tokenid}; | |
753 | ||
754 | cfs_write_file("user.cfg", $usercfg); | |
755 | }, 'deleting token failed'); | |
756 | ||
757 | return; | |
758 | }}); | |
759 | 1; |