blib/lib/App/Netdisco/Web/API/User.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 104 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Web::API::User;
2  
3 use Dancer ':syntax';
4 use Dancer::Plugin::DBIC;
5 use Dancer::Plugin::Swagger;
6 use Dancer::Plugin::Auth::Extensible;
7  
8 use App::Netdisco::Util::Token 'random_token';
9 use Try::Tiny;
10  
11 swagger_path {
12 tags => ['User'],
13 path => (setting('api_base') || '').'/users',
14 description => 'List all users with their roles and token status',
15 responses => { default => {} },
16 }, get '/api/v1/users' => require_role api_admin => sub {
17 header('Content-Type' => 'application/json');
18  
19 my @users = schema(vars->{'tenant'})->resultset('User')->search(undef, {
20 '+columns' => { token_hint => \"right(token, 8)" },
21 order_by => 'username',
22 })->hri->all;
23  
24 my @result = map {{
25 username => $_->{username},
26 fullname => $_->{fullname},
27 note => $_->{note},
28 admin => $_->{admin} ? \1 : \0,
29 port_control => $_->{port_control} ? \1 : \0,
30 portctl_roles => $_->{portctl_roles} || [],
31 ldap => $_->{ldap} ? \1 : \0,
32 radius => $_->{radius} ? \1 : \0,
33 tacacs => $_->{tacacs} ? \1 : \0,
34 token_auth_only => $_->{token_auth_only} ? \1 : \0,
35 token_hint => $_->{token_hint},
36 token_permanent => $_->{token_no_expire} ? \1 : \0,
37 token_acl => $_->{token_acl} || '',
38 last_on => $_->{last_on},
39 }} @users;
40  
41 return to_json \@result;
42 };
43  
44 swagger_path {
45 tags => ['User'],
46 path => (setting('api_base') || '').'/user',
47 description => 'Provision a service account and issue an API token. Creates the user if not present (null password, token-auth only). Idempotent.',
48 parameters => [
49 body => {
50 in => 'body',
51 schema => {
52 type => 'object',
53 required => ['username'],
54 properties => {
55 username => {
56 type => 'string',
57 description => 'Username of the service account',
58 },
59 permanent => {
60 type => 'boolean',
61 default => 0,
62 description => 'Issue a non-expiring token (requires allow_permanent_tokens in config)',
63 },
64 token_acl => {
65 type => 'string',
66 description => 'Name of the Host Group ACL controlling the sources allowed to use this token (omit for no restriction)',
67 },
68 revoke => {
69 type => 'boolean',
70 default => 0,
71 description => 'Revoke the token instead of issuing one',
72 },
73 },
74 },
75 },
76 ],
77 responses => { default => { examples => {
78 'application/json' => { username => 'grafana-svc', api_key => 'cc9d5c02d8898e5728b7d7a0339c0785', permanent => 1 },
79 } } },
80 }, post '/api/v1/user' => require_role api_admin => sub {
81 header('Content-Type' => 'application/json');
82  
83 my $body = try { from_json(request->body) } catch { {} };
84 my $username = $body->{username}
85 or return send_error(to_json({ error => 'Missing username' }), 400);
86  
87 my $user = schema('netdisco')->resultset('User')
88 ->find_or_create({ username => $username });
89  
90 if ($body->{revoke}) {
91 $user->update({ token => undef, token_from => undef, token_no_expire => \"false",
92 token_acl => undef });
93 return to_json { username => $username, revoked => \1 };
94 }
95  
96 my $provider = Dancer::Plugin::Auth::Extensible::auth_provider('users');
97  
98 my $want_permanent = $body->{permanent} && setting('allow_permanent_tokens');
99 my $token_acl = $body->{token_acl};
100  
101 $user->update({
102 token_from => time,
103 token_no_expire => ($want_permanent ? \"true" : \"false"),
104 (defined $token_acl ? (token_acl => $token_acl) : ()),
105 ($provider->validate_api_token($user->token)
106 ? () : (token => random_token())),
107 })->discard_changes();
108  
109 return to_json {
110 username => $username,
111 api_key => $user->token,
112 permanent => ($user->token_no_expire ? \1 : \0),
113 ($user->token_acl
114 ? (token_acl => $user->token_acl) : ()),
115 };
116 };
117  
118 true;