PlugAuth/Routes.pm

filter: Statementstmt 24.7% 49 / 198 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 23.3% 7 / 30 Podpod n/a totaltotal 20.5% 56 / 272 CC 24 SCAR 21.0
CC
24
Cov
20%
__ANON__
30.0
__ANON__
12.0
__ANON__
12.0
CRAP 8.1
« Clustericious/Plugin/SelfPlugAuth.pm ↑ Summary PlugAuth/Client.pm »
1 package PlugAuth::Routes;
2  
3 # ABSTRACT: routes for plugauth
4 41 our $VERSION = '0.39'; # VERSION
5  
6  
7 # There may be external authentication for these routes, i.e. using
8 # this CI to determine who can check/update other's access.
9  
10 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 41 ▶use Log::Log4perl qw/:easy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 41 ▶use Mojo::ByteStream qw/b/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 41 ▶use Clustericious::RouteBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 41 ▶use Clustericious::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 41 ▶use List::Util qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 0 ▶get '/' => sub { shift->welcome } => 'index';
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 0 ▶get '/index' => sub { shift->welcome };
Subroutine __ANON__: not called CC=1 SCAR=1.2
21  
22  
23 # Check authentication for a user (http basic auth protocol).
24 0 ▶get '/auth' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
25 0 my $self = shift;
26 0 ▶ my $auth = $self->req->headers->authorization or do {
Branch: 1 branch
BranchTrueFalseTotal
my $auth = $self->req->headers->authorization or do { $self->res->headers->www_authenticate("Basic \"ACPS\""); $self->render_message("please authenticate", 401); return } 0 0 0
27 0 $self->res->headers->www_authenticate('Basic "ACPS"');
28 0 $self->render_message('please authenticate', 401);
29 0 return;
30 };
31 0 my ($method,$str) = split / /,$auth;
32 0 my ($user,$pw) = split /:/, b($str)->b64_decode;
33  
34 0 ▶ if($self->auth->check_credentials($user,$pw))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auth->check_credentials($user, $pw)) { } 0 0 0
35 {
36 0 $self->render_message('ok');
37 0 INFO "Authentication succeeded for user $user";
38 }
39 else
40 {
41 0 $self->render_message('not ok', 403);
42 0 INFO "Authentication failed for user $user";
43 }
44 41 };
45  
46  
47 # Check authorization for a user to perform $action on $resource.
48 0 ▶get '/authz/user/#user/#action/(*resource)' => { resource => '/' } => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 my $c = shift;
50 # Ok iff the user is in a group for which $action on $resource is allowed.
51 0 my ($user,$resource,$action) = map $c->stash($_), qw/user resource action/;
52 0 $resource =~ s{^/?}{/};
53 0 TRACE "Checking authorization for $user to perform $action on $resource...";
54 0 my $found = $c->authz->can_user_action_resource($user,$action,$resource);
55 0 ▶ if ($found)
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 0 0 0
56 {
57 0 TRACE "Authorization succeeded ($found)";
58 0 return $c->render_message('ok');
59 }
60 0 TRACE "Authorization failed";
61 0 $c->render_message("unauthorized : $user cannot $action $resource", 403);
62 41 };
63  
64  
65 # Given a user, an action and a regex, return a list of resources
66 # on which $user can do $action, where each resource matches that regex.
67 0 ▶get '/authz/resources/#user/#action/(*resourceregex)' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
68 0 my $c = shift;
69 0 my ($user,$action,$resourceregex) = map $c->stash($_), qw/user action resourceregex/;
70 0 TRACE "Checking $user, $action, $resourceregex";
71 0 $resourceregex = qr[$resourceregex];
72 0 my @resources;
73 0 for my $resource ($c->authz->match_resources($resourceregex))
74 {
75 0 TRACE "Checking resource $resource";
76 0 ▶ push @resources, $resource if $c->authz->can_user_action_resource($user,$action,$resource);
Branch: 1 branch
BranchTrueFalseTotal
if $c->authz->can_user_action_resource($user, $action, $resource) 0 0 0
77 }
78 0 $c->stash->{autodata} = [sort @resources];
79 41 };
80  
81  
82 # Return a list of all defined actions
83 0 ▶get '/actions' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 my($self) = @_;
85 0 $self->stash->{autodata} = [ $self->authz->actions ];
86 41 };
87  
88  
89 # All the groups for a user :
90 0 ▶get '/groups/#user' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
91 0 my $c = shift;
92 0 my $groups = $c->authz->groups_for_user($c->stash('user'));
93 0 ▶ $c->render_message('not ok', 404) unless defined $groups;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $groups 0 0 0
94 0 $c->stash->{autodata} = $groups;
95 41 };
96  
97  
98 # Given a host and a tag (e.g. "trusted") return true if that host has
99 # that tag.
100 0 ▶get '/host/#host/:tag' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
101 0 my $c = shift;
102 0 my ($host,$tag) = map $c->stash($_), qw/host tag/;
103 0 ▶ if ($c->authz->host_has_tag($host,$tag))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->authz->host_has_tag($host, $tag)) 0 0 0
104 {
105 0 TRACE "Host $host has tag $tag";
106 0 return $c->render_message('ok', 200);
107 }
108 0 TRACE "Host $host does not have tag $tag";
109 0 return $c->render_message('not ok', 403);
110 41 };
111  
112  
113 0 ▶get '/user' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 my $c = shift;
115 0 $c->stash->{autodata} = [ uniq sort $c->auth->all_users ];
116 41 };
117  
118  
119 0 ▶get '/group' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 my $c = shift;
121 0 $c->stash->{autodata} = [ $c->authz->all_groups ];
122 41 };
123  
124  
125 0 ▶get '/users/:group' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
126 0 my $c = shift;
127 0 my $users = $c->authz->users_in_group($c->stash('group'));
128 0 ▶ $c->render_message('not ok', 404) unless defined $users;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $users 0 0 0
129 0 $c->stash->{autodata} = $users;
130 41 };
131  
132 41 authenticate;
133 41 authorize 'accounts';
134  
135  
136 0 ▶post '/user' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
137 0 my $c = shift;
138 0 $c->parse_autodata;
139 0 my $user = $c->stash->{autodata}->{user};
140 0 ▶ my $password = $c->stash->{autodata}->{password} || '';
Condition0%0/2
$c->stash->{'autodata'}{'password'} || ''
l!l
00
MC/DC0%0/1
$c->stash->{'autodata'}{'password'} || ''
$c->stash->{'autodata'}{'password'}
Truth table0%0/2
A
A= $c->stash->{'autodata'}{'password'}
Aresult
11
00
141 0 my $groups = $c->stash->{autodata}->{groups};
142 0 delete $c->stash->{autodata};
143
144 0 my $method = 'create_user';
145 0 my $cb;
146
147 0 my $auth_plugin = $c->auth;
148
149 0 ▶ if(defined $groups)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $groups) 0 0 0
150 {
151 0 $method = 'create_user_cb';
152 0 $auth_plugin = $c->auth->_find_create_user_cb;
153 0 ▶ return $c->render_message('not ok', 501)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $auth_plugin 0 0 0
154 unless defined $auth_plugin;
155 0 ▶ $cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
156 0 foreach my $group (split /\s*,\s*/, $groups)
157 {
158 0 my $users = $c->app->authz->add_user_to_group($group, $user);
159 0 $c->app->emit(create_group => {
160 admin => $c->stash('user'),
161 group => $group,
162 users => $users,
163 });
164 }
165 0 };
166 }
167
168 0 ▶ if($auth_plugin->$method($user, $password, $cb))
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_plugin->$method($user, $password, $cb)) { } 0 0 0
169 {
170 0 $c->render_message('ok', 200);
171 0 $c->app->emit('user_list_changed'); # deprecated, but documented in a previous version
172 0 $c->app->emit(create_user => {
173 admin => $c->stash('user'),
174 user => $user,
175 });
176 }
177 else
178 {
179 0 $c->render_message('not ok', 403);
180 }
181 41 };
182  
183  
184 0 ▶del '/user/#username' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
185 0 my $c = shift;
186 0 my $user = $c->param('username');
187 0 ▶ if($c->auth->delete_user($user))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->auth->delete_user($user)) { } 0 0 0
188 {
189 0 $c->render_message('ok', 200);
190 0 $c->app->emit('user_list_changed'); # deprecated, but documented in a previous version
191 0 $c->app->emit(delete_user => {
192 admin => $c->stash('user'),
193 user => $user,
194 });
195 }
196 else
197 {
198 0 $c->render_message('not ok', 404);
199 }
200 41 };
201  
202  
203 0 ▶post '/group' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
204 0 my $c = shift;
205 0 $c->parse_autodata;
206 0 my $group = $c->stash->{autodata}->{group};
207 0 my $users = $c->stash->{autodata}->{users};
208 0 delete $c->stash->{autodata};
209 0 ▶ if($c->authz->create_group($group, $users))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->authz->create_group($group, $users)) { } 0 0 0
210 {
211 0 $c->render_message('ok', 200);
212 0 $c->app->emit(create_group => {
213 admin => $c->stash('user'),
214 group => $group,
215 users => $users,
216 });
217 }
218 else
219 {
220 0 $c->render_message('not ok', 403);
221 }
222 41 };
223  
224  
225 0 ▶del '/group/:group' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
226 0 my $c = shift;
227 0 my $group = $c->param('group');
228 0 ▶ if($c->authz->delete_group($group))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->authz->delete_group($group)) { } 0 0 0
229 {
230 0 $c->render_message('ok', 200);
231 0 $c->app->emit(delete_group => {
232 admin => $c->stash('user'),
233 group => $group,
234 });
235 }
236 else
237 {
238 0 $c->render_message('not ok', 404);
239 }
240 41 };
241  
242  
243 0 ▶post '/group/:group' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
244 0 my $c = shift;
245 0 $c->parse_autodata;
246 0 my $users = $c->stash->{autodata}->{users};
247 0 my $group = $c->param('group');
248 0 delete $c->stash->{autodata};
249 0 ▶ if($c->authz->update_group($group, $users))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->authz->update_group($group, $users)) { } 0 0 0
250 {
251 0 $c->render_message('ok', 200);
252 0 $c->app->emit(update_group => {
253 admin => $c->stash('user'),
254 group => $group,
255 users => $users,
256 });
257 }
258 else
259 {
260 0 $c->render_message('not ok', 404);
261 }
262 41 };
263  
264  
265 0 ▶post '/group/:group/#username' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
266 0 my($c) = @_;
267 0 my $group = $c->stash('group');
268 0 my $user = $c->stash('username');
269 0 ▶ if(my $users = $c->authz->add_user_to_group($group, $user))
Branch: 1 branch
BranchTrueFalseTotal
if (my $users = $c->authz->add_user_to_group($group, $user)) { } 0 0 0
270 {
271 0 $c->render_message('ok', 200);
272 0 $c->app->emit(update_group => {
273 admin => $c->stash('user'),
274 group => $group,
275 users => $users,
276 });
277 }
278 else
279 {
280 0 $c->render_message('not ok', 404);
281 }
282 41 };
283  
284  
285 0 ▶del '/group/:group/#username' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
286 0 my($c) = @_;
287 0 my $group = $c->stash('group');
288 0 my $user = $c->stash('username');
289 0 ▶ if(my $users = $c->authz->remove_user_from_group($group, $user))
Branch: 1 branch
BranchTrueFalseTotal
if (my $users = $c->authz->remove_user_from_group($group, $user)) { } 0 0 0
290 {
291 0 $c->render_message('ok', 200);
292 0 $c->app->emit(update_group => {
293 admin => $c->stash('user'),
294 group => $group,
295 users => $users,
296 });
297 }
298 else
299 {
300 0 $c->render_message('not ok', 404);
301 }
302 41 };
303  
304  
305 0 ▶post '/grant/#group/:action1/(*resource)' => { resource => '/' } => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
306 0 my $c = shift;
307 0 my($group, $action, $resource) = map { $c->stash($_) } qw( group action1 resource );
308 0 $resource =~ s/\.(json|yml)$//;
309 0 ▶ if($c->authz->grant($group, $action, $resource))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->authz->grant($group, $action, $resource)) { } 0 0 0
310 {
311 0 $c->render_message('ok', 200);
312 0 $c->app->emit(grant => {
313 admin => $c->stash('user'),
314 group => $group,
315 action => $action,
316 resource => $resource,
317 });
318 }
319 else
320 {
321 0 $c->render_message('not ok', 404);
322 }
323 41 };
324  
325  
326 0 ▶del '/grant/#group/:action1/(*resource)' => { resource => '/' } => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
327 0 my($c) = @_;
328 0 my($group, $action, $resource) = map { $c->stash($_) } qw( group action1 resource );
329 0 $resource =~ s/\.(json|yml)$//;
330 0 ▶ if($c->authz->revoke($group, $action, $resource))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->authz->revoke($group, $action, $resource)) { } 0 0 0
331 {
332 0 $c->render_message('ok', 200);
333 0 $c->app->emit(revoke => {
334 admin => $c->stash('user'),
335 group => $group,
336 action => $action,
337 resource => $resource,
338 });
339 }
340 else
341 {
342 0 $c->render_message('not ok', 404);
343 }
344 41 };
345  
346  
347 0 ▶get '/grant' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
348 0 my($c) = @_;
349 0 $c->stash->{autodata} = $c->authz->granted;
350 41 };
351  
352  
353 41 authenticate;
354 41 authorize 'change_password';
355  
356 0 ▶post '/user/#username' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
357 0 my($c) = @_;
358 0 $c->parse_autodata;
359 0 my $user = $c->param('username');
360 0 ▶ my $password = eval { $c->stash->{autodata}->{password} } || '';
Condition0%0/2
eval { do { $c->stash->{'autodata'}{'password'} } } || ''
l!l
00
MC/DC0%0/1
eval { do { $c->stash->{'autodata'}{'password'} } } || ''
eval { do { $c->stash->{'autodata'}{'password'} } }
Truth table0%0/2
A
A= eval { do { $c->stash->{'autodata'}{'password'} } }
Aresult
11
00
361 0 delete $c->stash->{autodata};
362 0 ▶ if($c->auth->change_password($user, $password))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->auth->change_password($user, $password)) { } 0 0 0
363 {
364 0 $c->render_message('ok', 200);
365 0 $c->app->emit(change_password => { admin => $c->stash('user'), user => $user });
366 }
367 else
368 {
369 0 $c->render_message('not ok', 403);
370 }
371 41 };
372  
373 41 1;
374  
375 __END__
« Clustericious/Plugin/SelfPlugAuth.pm ↑ Summary PlugAuth/Client.pm »