| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Deploy::SecurityConstraint; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Deploy::SecurityConstraint - security-constraint deployment | ||
| 10 | descriptor element class | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Representation of a security constraint object for a web application, | ||
| 17 | as specified in a I<security-constraint> element in the deployment | ||
| 18 | descriptor. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | use fields qw(allRoles authConstraint authRoles collections displayName); | ||
| 23 | use fields qw(userConstraint); | ||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | |||
| 27 | =pod | ||
| 28 | |||
| 29 | =head1 CONSTRUCTOR | ||
| 30 | |||
| 31 | =over | ||
| 32 | |||
| 33 | =item new() | ||
| 34 | |||
| 35 | Construct and return a B<Wombat::Deploy::SecurityConstraint> instance, | ||
| 36 | initializing fields appropriately. | ||
| 37 | |||
| 38 | =back | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | $self = fields::new($self) unless ref $self; | ||
| 46 | |||
| 47 | $self->{allRoles} = undef; | ||
| 48 | $self->{authConstraint} = undef; | ||
| 49 | $self->{authRoles} = {}; | ||
| 50 | $self->{collections} = {}; | ||
| 51 | $self->{displayName} = undef; | ||
| 52 | $self->{userConstraint} = 'NONE'; | ||
| 53 | |||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | =pod | ||
| 58 | |||
| 59 | =head1 ACCESSOR METHODS | ||
| 60 | |||
| 61 | =over | ||
| 62 | |||
| 63 | =item getAllRoles() | ||
| 64 | |||
| 65 | Return a flag indicating if the "all roles" wildcard is included in | ||
| 66 | this SecurityConstraint. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub getAllRoles { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | return $self->{allRoles}; | ||
| 74 | } | ||
| 75 | |||
| 76 | =pod | ||
| 77 | |||
| 78 | =item getAuthConstraint() | ||
| 79 | |||
| 80 | Return a flag indication if an authorization constraint is included in | ||
| 81 | this SecurityConstraint. This is necessary to distinguish between an | ||
| 82 | auth-constraint with no roles (signifying no direct access at all) and | ||
| 83 | the lack of auth-constraint (which implies no access control checking). | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub getAuthConstraint { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | return $self->{authConstraint}; | ||
| 91 | } | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | =item setAuthConstraint($flag) | ||
| 96 | |||
| 97 | Set a flag indicating if an authorization constraint is included in | ||
| 98 | this SecurityConstraint. | ||
| 99 | |||
| 100 | B<Parameters:> | ||
| 101 | |||
| 102 | =over | ||
| 103 | |||
| 104 | =item $flag | ||
| 105 | |||
| 106 | a boolean value | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub setAuthConstraint { | ||
| 113 | my $self = shift; | ||
| 114 | my $flag = shift; | ||
| 115 | |||
| 116 | $self->{authConstraint} = $flag; | ||
| 117 | |||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =item getDisplayName() | ||
| 124 | |||
| 125 | Return the display name of this SecurityConstraint. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub getDisplayName { | ||
| 130 | my $self = shift; | ||
| 131 | |||
| 132 | return $self->{getDisplayName}; | ||
| 133 | } | ||
| 134 | |||
| 135 | =pod | ||
| 136 | |||
| 137 | =item setDisplayName($displayName) | ||
| 138 | |||
| 139 | Set the display name of this SecurityCosntraint. | ||
| 140 | |||
| 141 | B<Parameters:> | ||
| 142 | |||
| 143 | =over | ||
| 144 | |||
| 145 | =item $displayName | ||
| 146 | |||
| 147 | the display name | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub setDisplayName { | ||
| 154 | my $self = shift; | ||
| 155 | my $displayName = shift; | ||
| 156 | |||
| 157 | $self->{displayName} = $displayName; | ||
| 158 | |||
| 159 | return 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | =pod | ||
| 163 | |||
| 164 | =item getUserConstraint() | ||
| 165 | |||
| 166 | Return the user data constraint for this SecurityConstraint. | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub getUserConstraint { | ||
| 171 | my $self = shift; | ||
| 172 | |||
| 173 | return $self->{userConstraint}; | ||
| 174 | } | ||
| 175 | |||
| 176 | =pod | ||
| 177 | |||
| 178 | =item setUserConstraint($userConstraint) | ||
| 179 | |||
| 180 | Set the user data constraint for this SecurityConstraint. Must be one | ||
| 181 | of I<NONE>, I<INTEGRAL> or I<CONFIDENTIAL>. | ||
| 182 | |||
| 183 | B<Parameters:> | ||
| 184 | |||
| 185 | =over | ||
| 186 | |||
| 187 | =item $userConstraint | ||
| 188 | |||
| 189 | the user data constraint | ||
| 190 | |||
| 191 | =back | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub setUserConstraint { | ||
| 196 | my $self = shift; | ||
| 197 | my $userConstraint = shift; | ||
| 198 | |||
| 199 | $self->{userConstraint} = $userConstraint; | ||
| 200 | |||
| 201 | return 1; | ||
| 202 | } | ||
| 203 | |||
| 204 | =pod | ||
| 205 | |||
| 206 | =back | ||
| 207 | |||
| 208 | =head1 PUBLIC METHODS | ||
| 209 | |||
| 210 | =over | ||
| 211 | |||
| 212 | =item addAuthRole($authRole) | ||
| 213 | |||
| 214 | Add an authorization role, which is a role name that will be permitted | ||
| 215 | access to the resources protected by this SecurityConstraint. | ||
| 216 | |||
| 217 | B<Parameters:> | ||
| 218 | |||
| 219 | =over | ||
| 220 | |||
| 221 | =item $authRole | ||
| 222 | |||
| 223 | the role name to be added | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub addAuthRole { | ||
| 230 | my $self = shift; | ||
| 231 | my $role = shift; | ||
| 232 | |||
| 233 | return 1 unless $role; | ||
| 234 | |||
| 235 | if ($role eq '*') { | ||
| 236 | $self->{allRoles} = 1; | ||
| 237 | } else { | ||
| 238 | $self->{authRoles}->{$role} = 1; | ||
| 239 | } | ||
| 240 | |||
| 241 | $self->{authConstraint} = 1; | ||
| 242 | |||
| 243 | return 1; | ||
| 244 | } | ||
| 245 | |||
| 246 | =pod | ||
| 247 | |||
| 248 | =item hasAuthRole($role) | ||
| 249 | |||
| 250 | Return true if the specified role is permitted access to the resources | ||
| 251 | protected by this SecurityConstraint. | ||
| 252 | |||
| 253 | B<Parameters:> | ||
| 254 | |||
| 255 | =over | ||
| 256 | |||
| 257 | =item $role | ||
| 258 | |||
| 259 | the role name to be checked | ||
| 260 | |||
| 261 | =back | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub hasAuthRole { | ||
| 266 | my $self = shift; | ||
| 267 | my $role = shift; | ||
| 268 | |||
| 269 | return $self->{authRoles}->{$role}; | ||
| 270 | } | ||
| 271 | |||
| 272 | =pod | ||
| 273 | |||
| 274 | =item getAuthRoles() | ||
| 275 | |||
| 276 | Return an array containing the names of the roles that are permitted | ||
| 277 | access to the resources protected by this SecurityConstraint. | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | sub getAuthRoles { | ||
| 282 | my $self = shift; | ||
| 283 | |||
| 284 | my @roles = keys %{ $self->{authRoles} }; | ||
| 285 | |||
| 286 | return wantarray ? @roles : \@roles; | ||
| 287 | } | ||
| 288 | |||
| 289 | =pod | ||
| 290 | |||
| 291 | =item removeAuthRole($role) | ||
| 292 | |||
| 293 | Remove the specified role from the set of roles permitted to access | ||
| 294 | the resources protected by this SecurityConstraint. | ||
| 295 | |||
| 296 | B<Parameters:> | ||
| 297 | |||
| 298 | =over | ||
| 299 | |||
| 300 | =item $role | ||
| 301 | |||
| 302 | the name of the role to be removed | ||
| 303 | |||
| 304 | =back | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | sub removeAuthRole { | ||
| 309 | my $self = shift; | ||
| 310 | my $role = shift; | ||
| 311 | |||
| 312 | delete $self->{authRoles}->{$role}; | ||
| 313 | |||
| 314 | return 1; | ||
| 315 | } | ||
| 316 | |||
| 317 | =pod | ||
| 318 | |||
| 319 | =item addCollection($collection) | ||
| 320 | |||
| 321 | Add a new web resource collection to those protected by this | ||
| 322 | SecurityConstraint. | ||
| 323 | |||
| 324 | B<Parameters:> | ||
| 325 | |||
| 326 | =over | ||
| 327 | |||
| 328 | =item $collection | ||
| 329 | |||
| 330 | the B<Wombat::Deploy::SecurityConstraint> to be added | ||
| 331 | |||
| 332 | =back | ||
| 333 | |||
| 334 | =cut | ||
| 335 | |||
| 336 | sub addCollection { | ||
| 337 | my $self = shift; | ||
| 338 | my $collection = shift; | ||
| 339 | |||
| 340 | return 1 unless $collection; | ||
| 341 | |||
| 342 | my $key = $collection->getName() || ref $collection; | ||
| 343 | $self->{collections}->{$key} = $collection; | ||
| 344 | |||
| 345 | return 1; | ||
| 346 | } | ||
| 347 | |||
| 348 | =pod | ||
| 349 | |||
| 350 | =item getCollection($name) | ||
| 351 | |||
| 352 | Return the named web resource collection or C<undef>. | ||
| 353 | |||
| 354 | B<Parameters:> | ||
| 355 | |||
| 356 | =over | ||
| 357 | |||
| 358 | =item $name | ||
| 359 | |||
| 360 | the name of the web resource collection | ||
| 361 | |||
| 362 | =back | ||
| 363 | |||
| 364 | =cut | ||
| 365 | |||
| 366 | sub getCollection { | ||
| 367 | my $self = shift; | ||
| 368 | my $name = shift; | ||
| 369 | |||
| 370 | return $self->{collections}->{$name}; | ||
| 371 | } | ||
| 372 | |||
| 373 | =pod | ||
| 374 | |||
| 375 | =item getCollections() | ||
| 376 | |||
| 377 | Return an array containing the web resource collections protected by | ||
| 378 | this SecurityConstraint. | ||
| 379 | |||
| 380 | =cut | ||
| 381 | |||
| 382 | sub getCollections { | ||
| 383 | my $self = shift; | ||
| 384 | |||
| 385 | my @collections = values %{ $self->{collections} }; | ||
| 386 | |||
| 387 | return wantarray ? @collections : \@collections; | ||
| 388 | } | ||
| 389 | |||
| 390 | =pod | ||
| 391 | |||
| 392 | =item removeCollection($collection) | ||
| 393 | |||
| 394 | Remove the specified web resource collection from those protected by | ||
| 395 | this SecurityConstraint. | ||
| 396 | |||
| 397 | B<Parameters:> | ||
| 398 | |||
| 399 | =over | ||
| 400 | |||
| 401 | =item $collection | ||
| 402 | |||
| 403 | the B<Wombat::Deploy::SecurityCollection> to be removed | ||
| 404 | |||
| 405 | =back | ||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | sub removeCollection { | ||
| 410 | my $self = shift; | ||
| 411 | my $collection = shift; | ||
| 412 | |||
| 413 | return 1 unless $collection; | ||
| 414 | |||
| 415 | my $key = $collection->getName() || ref $collection; | ||
| 416 | delete $self->{collections}->{$key}; | ||
| 417 | |||
| 418 | return 1; | ||
| 419 | } | ||
| 420 | |||
| 421 | =pod | ||
| 422 | |||
| 423 | =item included($uri, $method) | ||
| 424 | |||
| 425 | Return true if the specified context-relative URI and associated HTTP | ||
| 426 | method are protected by this SecurityConstraint. | ||
| 427 | |||
| 428 | B<Parameters:> | ||
| 429 | |||
| 430 | =over | ||
| 431 | |||
| 432 | =item $uri | ||
| 433 | |||
| 434 | the uri to check | ||
| 435 | |||
| 436 | =item $method | ||
| 437 | |||
| 438 | the method being used | ||
| 439 | |||
| 440 | =back | ||
| 441 | |||
| 442 | =cut | ||
| 443 | |||
| 444 | sub included { | ||
| 445 | my $self = shift; | ||
| 446 | my $uri = shift; | ||
| 447 | my $method = shift; | ||
| 448 | |||
| 449 | return undef unless $method; | ||
| 450 | |||
| 451 | for my $collection ($self->getCollections()) { | ||
| 452 | next unless $collection->hasMethod($method); | ||
| 453 | for my $pattern ($collection->getPatterns()) { | ||
| 454 | return 1 if $self->matchPattern($uri, $pattern); | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | return undef; | ||
| 459 | } | ||
| 460 | |||
| 461 | # private methods | ||
| 462 | |||
| 463 | sub matchPattern { | ||
| 464 | my $self = shift; | ||
| 465 | my $path = shift; | ||
| 466 | my $pattern = shift; | ||
| 467 | |||
| 468 | # normalize the argument | ||
| 469 | $path ||= '/'; | ||
| 470 | $pattern ||= '/'; | ||
| 471 | |||
| 472 | # check for exact match | ||
| 473 | return 1 if $path eq $pattern; | ||
| 474 | |||
| 475 | # check for path prefix matching | ||
| 476 | if ($pattern =~ m|^/| && $pattern =~ s|/\*$||) { | ||
| 477 | return 1 unless $pattern; # "/*" is the same as "/" | ||
| 478 | return $path =~ m|^$pattern|; | ||
| 479 | } | ||
| 480 | |||
| 481 | # check for suffix matching | ||
| 482 | if ($pattern =~ m|^\*\.|) { | ||
| 483 | return $path =~ m|$pattern$|; | ||
| 484 | } | ||
| 485 | |||
| 486 | # check for universal mapping | ||
| 487 | return $pattern eq '/'; | ||
| 488 | } | ||
| 489 | |||
| 490 | 1; | ||
| 491 | __END__ |