| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Realm::RealmBase; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Realm::RealmBase - internal realm base clas | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Convenience base implementation of B<Wombat::Realm>. Subclasses should | ||
| 16 | implement C<getName()>, C<getPassword()>, and C<getPrincipal()>. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | use base qw(Wombat::Realm); | ||
| 21 | use fields qw(algorithm container digest started); | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | |||
| 25 | use Wombat::Exception (); | ||
| 26 | |||
| 27 | =pod | ||
| 28 | |||
| 29 | =head1 CONSTRUCTOR | ||
| 30 | |||
| 31 | =over | ||
| 32 | |||
| 33 | =item new() | ||
| 34 | |||
| 35 | Construct and return a B<Wombat::Realm::RealmBase> instance, | ||
| 36 | initializing fields appropriately. If subclasses override the | ||
| 37 | constructor, they must be sure to call | ||
| 38 | |||
| 39 | $self->SUPER::new(); | ||
| 40 | |||
| 41 | =back | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | $self = fields::new($self) unless ref $self; | ||
| 49 | |||
| 50 | $self->{algorithm} = undef; | ||
| 51 | $self->{container} = undef; | ||
| 52 | $self->{digest} = undef; | ||
| 53 | $self->{started} = undef; | ||
| 54 | |||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | =pod | ||
| 59 | |||
| 60 | =head1 ACCESSOR METHODS | ||
| 61 | |||
| 62 | =over | ||
| 63 | |||
| 64 | =item getAlgorithm() | ||
| 65 | |||
| 66 | Return the digest algorithm used for authenticating credentials. If no | ||
| 67 | algorithm is specified, credentials will be used as submitted. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub getAlgorithm { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | return $self->{algorithm}; | ||
| 75 | } | ||
| 76 | |||
| 77 | =pod | ||
| 78 | |||
| 79 | =item setAlgorithm($algorithm) | ||
| 80 | |||
| 81 | Set the digest algorithm used for authenticating | ||
| 82 | credentials. Supported algorithms include: MD5, SHA1, HMAC, MD2, and | ||
| 83 | anything else provided by the L<Digest> library. | ||
| 84 | |||
| 85 | B<Parameters:> | ||
| 86 | |||
| 87 | =over | ||
| 88 | |||
| 89 | =item $algorithm | ||
| 90 | |||
| 91 | the digest algorithm | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub setAlgorithm { | ||
| 98 | my $self = shift; | ||
| 99 | my $algorithm = shift; | ||
| 100 | |||
| 101 | $self->{algorithm} = $algorithm; | ||
| 102 | |||
| 103 | return 1; | ||
| 104 | } | ||
| 105 | |||
| 106 | =pod | ||
| 107 | |||
| 108 | =item getContainer() | ||
| 109 | |||
| 110 | Return the Container associated with this Realm. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub getContainer { | ||
| 115 | my $self = shift; | ||
| 116 | |||
| 117 | return $self->{container}; | ||
| 118 | } | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | =item setContainer($container) | ||
| 123 | |||
| 124 | Set the Container associated with this Realm. | ||
| 125 | |||
| 126 | B<Parameters:> | ||
| 127 | |||
| 128 | =over | ||
| 129 | |||
| 130 | =item $container | ||
| 131 | |||
| 132 | the B<Wombat::Container> associated with this Realm | ||
| 133 | |||
| 134 | =back | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub setContainer { | ||
| 139 | my $self = shift; | ||
| 140 | my $container = shift; | ||
| 141 | |||
| 142 | $self->{container} = $container; | ||
| 143 | |||
| 144 | return 1; | ||
| 145 | } | ||
| 146 | |||
| 147 | =pod | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =head1 PUBLIC METHODS | ||
| 152 | |||
| 153 | =over | ||
| 154 | |||
| 155 | =item authenticate ($username, $credentials) | ||
| 156 | |||
| 157 | Return the Principal associated with the specified username and | ||
| 158 | credentials, if there is one, or C<undef> otherwise. | ||
| 159 | |||
| 160 | B<Parameters> | ||
| 161 | |||
| 162 | =over | ||
| 163 | |||
| 164 | =item $username | ||
| 165 | |||
| 166 | username of the principal to be looked up | ||
| 167 | |||
| 168 | =item $credentials | ||
| 169 | |||
| 170 | password or other credentials to use in authenticating this username | ||
| 171 | |||
| 172 | =back | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub authenticate { | ||
| 177 | my $self = shift; | ||
| 178 | my $username = shift; | ||
| 179 | my $credentials = shift; | ||
| 180 | |||
| 181 | my $password = $self->getPassword($username); | ||
| 182 | |||
| 183 | return undef unless defined $password && $password eq $credentials; | ||
| 184 | |||
| 185 | return $self->getPrincipal($username); | ||
| 186 | } | ||
| 187 | |||
| 188 | =pod | ||
| 189 | |||
| 190 | =item hasRole($principal, $role) | ||
| 191 | |||
| 192 | Return true if the specified Principal has the specified security | ||
| 193 | role within the context of this Realm, or false otherwise. | ||
| 194 | |||
| 195 | B<Parameters:> | ||
| 196 | |||
| 197 | =over | ||
| 198 | |||
| 199 | =item $principal | ||
| 200 | |||
| 201 | the B<Wombat::Realm::Genericrincipal> for whom the role is to be | ||
| 202 | checked | ||
| 203 | |||
| 204 | =item $role | ||
| 205 | |||
| 206 | the name of the security role to be checked | ||
| 207 | |||
| 208 | =back | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub hasRole { | ||
| 213 | my $self = shift; | ||
| 214 | my $principal = shift; | ||
| 215 | my $role = shift; | ||
| 216 | |||
| 217 | return undef unless defined $principal && defined $role && | ||
| 218 | $principal->isa('Wombat::Realm::GenericPrincipal'); | ||
| 219 | |||
| 220 | my $prealm = $principal->getRealm(); | ||
| 221 | return undef unless $prealm; | ||
| 222 | |||
| 223 | my $prealmname = $prealm->getName(); | ||
| 224 | return undef unless $prealmname eq $self->getName(); | ||
| 225 | |||
| 226 | return $principal->hasRole($role); | ||
| 227 | } | ||
| 228 | |||
| 229 | =pod | ||
| 230 | |||
| 231 | =back | ||
| 232 | |||
| 233 | =head1 PACKAGE METHODS | ||
| 234 | |||
| 235 | =over | ||
| 236 | |||
| 237 | =item digest() | ||
| 238 | |||
| 239 | Digest a submitted password using the configured algorithm and convert | ||
| 240 | the result to a corresponding hexadecimal string. If an exception is | ||
| 241 | thrown, the plain credentials string is returned. | ||
| 242 | |||
| 243 | B<Parameters:> | ||
| 244 | |||
| 245 | =over | ||
| 246 | |||
| 247 | =item $credentials | ||
| 248 | |||
| 249 | the password or other credentials to use in authentication | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | sub digest { | ||
| 256 | my $self = shift; | ||
| 257 | my $credentials = shift; | ||
| 258 | |||
| 259 | return $credentials unless $self->{digest}; | ||
| 260 | |||
| 261 | $self->{digest}->add($credentials); | ||
| 262 | return $self->{digest}->hexdigest(); | ||
| 263 | } | ||
| 264 | |||
| 265 | =pod | ||
| 266 | |||
| 267 | =item getName() | ||
| 268 | |||
| 269 | Return a short name for this Realm implementation. Must be overridden | ||
| 270 | by subclasses. | ||
| 271 | |||
| 272 | =cut | ||
| 273 | |||
| 274 | sub getName {} | ||
| 275 | |||
| 276 | =pod | ||
| 277 | |||
| 278 | =item getPassword($username) | ||
| 279 | |||
| 280 | Return the password associated with the given Principal's user | ||
| 281 | name. Should be overridden by subclasses. | ||
| 282 | |||
| 283 | B<Parameters:> | ||
| 284 | |||
| 285 | =over | ||
| 286 | |||
| 287 | =item $username | ||
| 288 | |||
| 289 | the username of the Principal | ||
| 290 | |||
| 291 | =back | ||
| 292 | |||
| 293 | =cut | ||
| 294 | |||
| 295 | sub getPassword {} | ||
| 296 | |||
| 297 | =pod | ||
| 298 | |||
| 299 | =item getPrincipal($username) | ||
| 300 | |||
| 301 | Return the Principal associated with the given user name. Should be | ||
| 302 | overridden by subclasses. | ||
| 303 | |||
| 304 | B<Parameters:> | ||
| 305 | |||
| 306 | =over | ||
| 307 | |||
| 308 | =item $username | ||
| 309 | |||
| 310 | the username of the Principal | ||
| 311 | |||
| 312 | =back | ||
| 313 | |||
| 314 | =cut | ||
| 315 | |||
| 316 | sub getPrincipal {} | ||
| 317 | |||
| 318 | =pod | ||
| 319 | |||
| 320 | =back | ||
| 321 | |||
| 322 | =head1 LIFECYCLE METHODS | ||
| 323 | |||
| 324 | =over | ||
| 325 | |||
| 326 | =item start() | ||
| 327 | |||
| 328 | Prepare for active use of this Realm. This method should be called | ||
| 329 | before any of the public methods of the Realm are utilized. | ||
| 330 | |||
| 331 | B<Throws:> | ||
| 332 | |||
| 333 | =over | ||
| 334 | |||
| 335 | =item B<Wombat::LifecycleException> | ||
| 336 | |||
| 337 | if the Realm has already been started | ||
| 338 | |||
| 339 | =back | ||
| 340 | |||
| 341 | =cut | ||
| 342 | |||
| 343 | sub start { | ||
| 344 | my $self = shift; | ||
| 345 | |||
| 346 | if ($self->{started}) { | ||
| 347 | my $msg = "start: realm already started"; | ||
| 348 | Wombat::LifecycleException->throw($msg); | ||
| 349 | } | ||
| 350 | |||
| 351 | if ($self->{algorithm}) { | ||
| 352 | my $class = join '::', 'Digest', $self->{algorithm}; | ||
| 353 | eval "require $class"; | ||
| 354 | if ($@) { | ||
| 355 | my $msg = | ||
| 356 | "start: unsupported digest algorithm [$self->{algorithm}]"; | ||
| 357 | Wombat::LifecycleException->throw($msg); | ||
| 358 | } | ||
| 359 | |||
| 360 | $self->{digest} = $class->new(); | ||
| 361 | } | ||
| 362 | |||
| 363 | $self->{started} = 1; | ||
| 364 | $self->log(sprintf("%s started", $self->getName()), undef, 'INFO'); | ||
| 365 | |||
| 366 | return 1; | ||
| 367 | } | ||
| 368 | |||
| 369 | =pod | ||
| 370 | |||
| 371 | =item stop() | ||
| 372 | |||
| 373 | Gracefully terminate active use of this Realm. Once this method | ||
| 374 | has been called, no public methods of the Realm should be | ||
| 375 | utilized. | ||
| 376 | |||
| 377 | B<Throws:> | ||
| 378 | |||
| 379 | =over | ||
| 380 | |||
| 381 | =item B<Wombat::LifecycleException> | ||
| 382 | |||
| 383 | if the Realm is not started | ||
| 384 | |||
| 385 | =back | ||
| 386 | |||
| 387 | =cut | ||
| 388 | |||
| 389 | sub stop { | ||
| 390 | my $self = shift; | ||
| 391 | |||
| 392 | unless ($self->{started}) { | ||
| 393 | my $msg = "stop: realm not started"; | ||
| 394 | Wombat::LifecycleException->throw($msg); | ||
| 395 | } | ||
| 396 | |||
| 397 | undef $self->{started}; | ||
| 398 | $self->log(sprintf("%s stopped", $self->getName()), undef, 'DEBUG'); | ||
| 399 | |||
| 400 | return 1; | ||
| 401 | } | ||
| 402 | |||
| 403 | =pod | ||
| 404 | |||
| 405 | =back | ||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | # private methods | ||
| 410 | |||
| 411 | sub log { | ||
| 412 | my $self = shift; | ||
| 413 | |||
| 414 | $self->{container}->log(@_) if $self->{container}; | ||
| 415 | |||
| 416 | return 1; | ||
| 417 | } | ||
| 418 | |||
| 419 | 1; | ||
| 420 | __END__ |