blib/lib/Mojolicious/Plugin/Authorization/AccessControl.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Mojolicious::Plugin::Authorization::AccessControl 0.02;
2 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Integrate Authorization::AccessControl into Mojolicious
6  
7 0 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use Authorization::AccessControl qw(acl);
10 use Readonly;
11 use Syntax::Keyword::Try;
12  
13 use experimental qw(signatures);
14  
15 Readonly::Scalar my $DEFAULT_PREFIX => 'authz';
16  
17 sub register($self, $app, $args) {
18 my $prefix = $args->{prefix} // $DEFAULT_PREFIX;
19 my $stash_ac = "_$prefix.request.accesscontrol";
20  
21 my $get_roles = sub($c) {$c->authn->current_user_roles};
22 $get_roles = $args->{get_roles} // sub($c) {[]}
23 if (exists($args->{get_roles}));
24 die("get_roles must be a CODEREF/anonymous subroutine") if (defined($get_roles) && ref($get_roles) ne 'CODE');
25  
26 my $log_f = sub($m) {$app->log->info($m)};
27 if (exists($args->{log})) {
28 if (defined($args->{log})) {
29 if (ref($args->{log}) && $args->{log}->isa('Mojo::Log')) {
30 $log_f = sub($m) {$args->{log}->info($m)};
31 }
32 } else {
33 $log_f = sub { }
34 }
35 }
36  
37 acl->hook(on_permit => sub ($ctx) {$log_f->("[Authorization::AccessControl] Granted: $ctx")});
38 acl->hook(on_deny => sub ($ctx) {$log_f->("[Authorization::AccessControl] Denied: $ctx")});
39  
40 my $get_ac = sub($c) {
41 my $ac = acl;
42 if ($c->tx->connection) {
43 $c->stash($stash_ac => $ac->clone) unless (defined($c->stash($stash_ac)));
44 $ac = $c->stash($stash_ac);
45 }
46 return $ac;
47 };
48  
49 $app->helper(
50 "$prefix.acl" => sub ($c) {
51 return $get_ac->($c);
52 }
53 );
54  
55 $app->helper(
56 "$prefix.role" => sub ($c, @params) {
57 return $get_ac->($c)->role(@params);
58 }
59 );
60  
61 my @get_attrs;
62 $app->helper(
63 "$prefix.dynamic_attrs" => sub ($c, @params) {
64 my $get_attrs = {handler => pop(@params)};
65 $get_attrs->{resource} = shift(@params) if (@params);
66 $get_attrs->{action} = shift(@params) if (@params);
67 push(@get_attrs, $get_attrs);
68 }
69 );
70  
71 my $get_get_attrs = sub ($resource, $action) {
72 my @c = @get_attrs;
73 @c = grep {!defined($_->{resource}) || $_->{resource} eq $resource} @c if (defined($resource));
74 @c = grep {!defined($_->{action}) || $_->{action} eq $action} @c if (defined($action));
75 @c = sort {defined($b->{resource}) + defined($b->{action}) - (defined($a->{resource}) + defined($a->{action}))} @c;
76 return ($c[0] // {})->{handler};
77 };
78  
79 $app->helper(
80 "$prefix.request" => sub ($c, $resource = undef, $action = undef) {
81 my $roles = [];
82 try {$roles = $get_roles->($c)} catch ($e) {
83 }
84  
85 my $req = $get_ac->($c)->request->with_roles($roles->@*);
86 $req = $req->with_action($action) if (defined($action));
87 $req = $req->with_resource($resource) if (defined($resource));
88 if (my $f = $get_get_attrs->($resource, $action)) {
89 $req = $req->with_get_attrs(sub($ctx) {$f->($c, $ctx)});
90 }
91  
92 return $req;
93 }
94 );
95 }
96  
97 =head1 NAME
98  
99 Mojolicious::Plugin::Authorization::AccessControl - Integrate Authorization::AccessControl into Mojolicious
100  
101 =head1 SYNOPSIS
102  
103 # in startup
104 $app->plugin('Authorization::AccessControl' => {
105 get_roles => sub($c) { [$c->authn->current_user->roles] }
106 });
107 # static grants
108 $app->authz->role('admin')
109 ->grant(User => 'list')
110 ->grant(User => 'create')
111 ->grant(User => 'delete');
112
113 $app->authz->dynamic_attrs(Book => list => undef);
114 $app->authz->dynamic_attrs(Book => sub($c, $ctx) {
115 return {
116 book_id => $ctx->id,
117 own => $ctx->owner_id == $c->authn->current_user->id,
118 deleted => defined($ctx->deleted_at)
119 }
120 });
121  
122 $app->hook(before_dispatch => sub($c) {
123 #dynamic grants specific to current request
124 $c->authz->grant(Book => 'delete', { book_id => $_->{book_id} })
125 foreach ($c->model("GrantDelete")->for_user($c->authn->current_user))
126 });
127  
128 # in controller
129 use Authorization::AccessControl qw(acl);
130  
131 # static grants
132 acl->role
133 ->grant(Book => 'list', { deleted => 0 })
134 ->grant(Book => "read")
135 ->grant(Book => "edit", { own => 1 })
136 ->role('admin')
137 ->grant(Book => "list")
138 ->grant(Book => "edit");
139  
140 sub list($self) {
141 my $deleted = !!$self->param('include_deleted');
142 $self->authz->request(Book => 'list')->with_attributes({ deleted => $deleted })->yield(sub() {
143 $deleted ? [$self->model("book")->all] : [$self->model("book")->all_except_deleted]
144 })
145 ->granted(sub ($books) {
146 $self->render(json => $books)
147 })
148 ->denied(sub() {
149 $self->render(status => 401, text => 'unauthorized')
150 })
151 ->null(sub() {
152 $self->render(status => 404, text => 'notfound')
153 })
154 }
155  
156 sub get($self) {
157 $self->authz->request(Book => 'read')->yield(sub() {
158 $self->model("book")->get($self->param('id'))
159 })
160 ->granted(sub ($book) {
161 $self->render(json => $book)
162 })
163 ->denied(sub () {
164 $self->render(status => 401, text => 'unauthorized')
165 })
166 ->null(sub () {
167 $self->render(status => 404, text => "book not found")
168 })
169  
170 sub edit($self) {
171 $self->authz->request(Book => 'edit')->yield(sub() {
172 $self->model("book")->get($self->param('id'))
173 })
174 ->granted(sub ($book) {
175 $book->update($self->req->body->json);
176 $self->render(json => $book)
177 })
178 ->denied(sub () {
179 $self->render(status => 401, text => 'unauthorized')
180 })
181 ->null(sub () {
182 $self->render(status => 404, text => "book not found")
183 })
184 }
185  
186 =head1 DESCRIPTION
187  
188 This plugin ties together the functionality of L<Authorization::AccessControl>
189 with the L<Mojolicious> framework. In essence, this means:
190  
191 =over
192  
193 =item * roles are computed and attached to requests automatically based on the L</get_roles> function
194  
195 =item * dynamic attributes callbacks can be registered and then they, too, will be used automatically at request time
196  
197 =item * privilege grants can be static/permanent, or dynamic (per-request, from database, etc). Determination is automatically made based on context.
198  
199 =item * all authorization checks are automatically logged to a Mojo::Log instance
200  
201 =back
202  
203 =head1 METHODS
204  
205 L<Mojolicious::Plugin::Authorization::AccessControl> inherits all methods from
206 L<Mojolicious::Plugin> and implements the following new ones
207  
208 =head2 register
209  
210 Register plugin in L<Mojolicious> application. Configuration via named arguments:
211  
212 =head4 prefix
213  
214 Configures the prefix used for the module's Mojolicious helper functions and
215 stash values. This documentation assumes that it is left unchanged
216  
217 Default: C<authz>
218  
219 =head4 get_roles
220  
221 Configures a callback for obtaining the roles relevent to the authorization
222 requests (i.e., the roles of the current user). The function receives one
223 argument, the Mojolicious controller, and returns an ArrayRef of roles.
224  
225 Default: C<sub($c) { $c-E<gt>current_user_roles }>
226  
227 =head4 log
228  
229 The L<Mojo::Log> instance that will be used to log Authorization activity in.
230 Set to undef to disable logging entirely.
231  
232 Default: C<app-E<gt>log>
233  
234 =head1 HELPERS
235  
236 =head2 authz.acl
237  
238 $app->authz->acl()
239 $c->authz->acl()
240  
241 Returns the L<Authorization::AccessControl::ACL> instance, depending on context
242 If called on a request controller, returns an ACL specific to that request;
243 otherwise, returns the global L<Authorization::AccessControl::ACL/acl> instance.
244  
245 The request-specific ACLs are constructed by cloning the global instance,
246 so you may populate the global instance with static grants, and then augment
247 them with dynamic request-specific grants.
248  
249 =head2 authz.role
250  
251 $app->authz->role( $role = undef )
252 $c->authz->role( $role = undef )
253  
254 A shortcut for calling L<Authorization::AccessControl::ACL/role> on
255 L</authz.acl>. Returns a dependent ACL instance contextualized on the given
256 C<$role> argument.
257  
258 =head2 authz.request
259  
260 $c->authz->request( $resource = undef, $action = undef )
261  
262 Creates and populates an L<Authorization::AccessControl::Request>. If
263 C<$resource> and/or C<$action> are given, they are set on the request, but they
264 are also used for determining the
265 L<Authorization::AccessControl::Request/with_get_attrs> function to assign.
266 The request's L<Authorization::AccessControl::Request/roles> are also configured,
267 using the L</get_roles> callback.
268  
269 =head2 authz.dynamic_attrs
270  
271 $c->authz->dynamic_attrs($coderef)
272 $c->authz->dynamic_attrs($resource, $coderef)
273 $c->authz->dynamic_attrs($resource, $action, $coderef)
274  
275 Registers a dynamic attributes callback function. C<$coderef> must be a CODEREF
276 or undefined. Dynamic attrs functions are searched from most to least specific,
277 so a general function may be overridden with a more-specific one, or blocked
278 entirely with an undefined entry, e.g.,
279  
280 $c->authz->dynamic_attrs(Book => sub($c, $ctx) { ... } );
281 $c->authz->dynamic_attrs(Book => list => undef);
282  
283 The first line declares a handler for attributes of Book resources. The second
284 clears the handler for the list action of Book resources, so the handler is used
285 for all Book resources, except for list actions.
286  
287 =head1 AUTHOR
288  
289 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
290  
291 =head1 LICENSE
292  
293 Copyright (c) 2024 Mark Tyrrell
294  
295 Permission is hereby granted, free of charge, to any person obtaining a copy
296 of this software and associated documentation files (the "Software"), to deal
297 in the Software without restriction, including without limitation the rights
298 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
299 copies of the Software, and to permit persons to whom the Software is
300 furnished to do so, subject to the following conditions:
301  
302 The above copyright notice and this permission notice shall be included in all
303 copies or substantial portions of the Software.
304  
305 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
306 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
307 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
308 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
309 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
310 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
311 SOFTWARE.
312  
313 =cut
314  
315 1;
316  
317 __END__
↑ Summary