| 1 | # Declare our package | ||
| 2 | package POE::Component::Lightspeed::Authentication; | ||
| 3 | |||
| 4 | # Standard stuff to catch errors | ||
| 5 | use strict qw(subs vars refs); # Make sure we can't mess up | ||
| 6 | use warnings FATAL => 'all'; # Enable warnings to catch errors | ||
| 7 | |||
| 8 | # Initialize our version | ||
| 9 | our $VERSION = '1.' . sprintf( "%04d", (qw($Revision: 1082 $))[1] ); | ||
| 10 | |||
| 11 | # Make sure the router instance is loaded no matter what | ||
| 12 | use POE::Component::Lightspeed::Router; | ||
| 13 | |||
| 14 | # Spawn the Router session to make sure :) | ||
| 15 | POE::Component::Lightspeed::Router->spawn(); | ||
| 16 | |||
| 17 | # We export some stuff | ||
| 18 | require Exporter; | ||
| 19 | our @ISA = qw( Exporter ); | ||
| 20 | our @EXPORT_OK = qw( auth_register auth_unregister ); | ||
| 21 | |||
| 22 | # The different types of hooks | ||
| 23 | our @HOOKS = qw( post call callreply introspection monitor_register monitor_unregister ); | ||
| 24 | |||
| 25 | # Registers an authentication hook | ||
| 26 | sub auth_register { | ||
| 27 | my( $hook, $sub, $arg ) = @_; | ||
| 28 | |||
| 29 | # Test for a valid hook | ||
| 30 | if ( ! defined $hook or ! Array_Search( \@HOOKS, $hook ) ) { | ||
| 31 | # Error... | ||
| 32 | return undef; | ||
| 33 | } | ||
| 34 | |||
| 35 | # Make sure we have a real sub | ||
| 36 | if ( ! defined $sub or ! ref( $sub ) or ref( $sub ) ne 'CODE' ) { | ||
| 37 | # Error... | ||
| 38 | return undef; | ||
| 39 | } | ||
| 40 | |||
| 41 | # Make sure we have the argument | ||
| 42 | if ( ! defined $arg ) { | ||
| 43 | $arg = '*'; | ||
| 44 | } | ||
| 45 | |||
| 46 | # Get the heap from the router session | ||
| 47 | my $heap = $POE::Kernel::poe_kernel->_resolve_session( $POE::Component::Lightspeed::Router::SES_ALIAS )->get_heap(); | ||
| 48 | |||
| 49 | # Insert this hook! | ||
| 50 | $heap->{'AUTH'}->{ $hook }->{ $arg } = $sub; | ||
| 51 | |||
| 52 | # All done! | ||
| 53 | return 1; | ||
| 54 | } | ||
| 55 | |||
| 56 | # Unregisters an authentication hook | ||
| 57 | sub auth_unregister { | ||
| 58 | my( $hook, $arg ) = @_; | ||
| 59 | |||
| 60 | # Test for a valid hook | ||
| 61 | if ( defined $hook and Array_Search( \@HOOKS, $hook ) ) { | ||
| 62 | # Make sure we have the argument | ||
| 63 | if ( ! defined $arg ) { | ||
| 64 | $arg = '*'; | ||
| 65 | } | ||
| 66 | |||
| 67 | # Get the heap from the router session | ||
| 68 | my $heap = $POE::Kernel::poe_kernel->_resolve_session( $POE::Component::Lightspeed::Router::SES_ALIAS )->get_heap(); | ||
| 69 | |||
| 70 | # Sanity check | ||
| 71 | if ( exists $heap->{'AUTH'}->{ $hook } ) { | ||
| 72 | if ( exists $heap->{'AUTH'}->{ $hook }->{ $arg } ) { | ||
| 73 | # Delete the hook | ||
| 74 | delete $heap->{'AUTH'}->{ $hook }->{ $arg }; | ||
| 75 | |||
| 76 | # Also, check the hook itself | ||
| 77 | if ( keys %{ $heap->{'AUTH'}->{ $hook } } == 0 ) { | ||
| 78 | delete $heap->{'AUTH'}->{ $hook }; | ||
| 79 | } | ||
| 80 | |||
| 81 | # All done! | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | # Error... | ||
| 88 | return undef; | ||
| 89 | } | ||
| 90 | |||
| 91 | # Searches for something in an array | ||
| 92 | sub Array_Search { | ||
| 93 | # Get the pointer to array | ||
| 94 | my $ary = shift; | ||
| 95 | |||
| 96 | # Get the name | ||
| 97 | my $name = shift; | ||
| 98 | |||
| 99 | # Iterate over the array | ||
| 100 | foreach my $tmp ( @{ $ary } ) { | ||
| 101 | if ( $tmp eq $name ) { | ||
| 102 | return 1; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | # Failed to find a match | ||
| 107 | return 0; | ||
| 108 | } | ||
| 109 | |||
| 110 | # End of module | ||
| 111 | 1; | ||
| 112 | __END__ |