| 1 | package Authen::Class::HtAuth::Base; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 3 | ▶ | use base 'Class::Data::Inheritable'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | # Crikey! I don't really like this. | ||
| 9 | # Explanation: | ||
| 10 | # a) create these inheritable class data accessors | ||
| 11 | # b) in the "real" class, override them to turn them into translucent dealies | ||
| 12 | __PACKAGE__->mk_classdata('htusers'); | ||
| 13 | __PACKAGE__->mk_classdata('htgroups'); | ||
| 14 | __PACKAGE__->mk_classdata('_ApacheHtpasswd'); | ||
| 15 | __PACKAGE__->mk_classdata('_ApacheHtgroup'); | ||
| 16 | |||
| 17 | |||
| 18 | package Authen::Class::HtAuth; | ||
| 19 | use base 'Authen::Class::HtAuth::Base'; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use Carp; | ||
| 25 | use Apache::Htpasswd; | ||
| 26 | use Apache::Htgroup; | ||
| 27 | |||
| 28 | our $VERSION = 0.02; | ||
| 29 | |||
| 30 | sub _ApacheHtpasswd { | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | if (ref $self and defined $self->{_apachehtpasswd}) { | ||
| 34 | $self->{_apachehtpasswd} = shift if @_; | ||
| 35 | $self->{_apachehtpasswd}; | ||
| 36 | } | ||
| 37 | else { | ||
| 38 | $self->__ApacheHtpasswd_accessor(@_); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _ApacheHtgroup { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | if (ref $self and defined $self->{_apachehtgroup}) { | ||
| 46 | $self->{_apachehtgroup} = shift if @_; | ||
| 47 | $self->{_apachehtgroup}; | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | $self->__ApacheHtgroup_accessor(@_); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub htusers { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | if (ref $self) { | ||
| 58 | if (@_) { | ||
| 59 | $self->{_apachehtpasswd} = Apache::Htpasswd->new( | ||
| 60 | { passwdFile => $_[0], | ||
| 61 | ReadOnly => 1, | ||
| 62 | } | ||
| 63 | ); | ||
| 64 | $self->{htusers} = $_[0]; | ||
| 65 | } | ||
| 66 | |||
| 67 | return defined $self->{htusers} ? $self->{htusers} : $self->_htusers_accessor; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | if (@_) { | ||
| 71 | $self->_ApacheHtpasswd( | ||
| 72 | Apache::Htpasswd->new( | ||
| 73 | { passwdFile => $_[0], | ||
| 74 | ReadOnly => 1, | ||
| 75 | } | ||
| 76 | ) ); | ||
| 77 | } | ||
| 78 | |||
| 79 | return $self->_htusers_accessor(@_); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | sub htgroups { | ||
| 84 | my $self = shift; | ||
| 85 | |||
| 86 | if (ref $self) { | ||
| 87 | if (@_) { | ||
| 88 | $self->{_apachehtgroup} = Apache::Htgroup->new($_[0]); | ||
| 89 | $self->{htgroups} = $_[0]; | ||
| 90 | } | ||
| 91 | |||
| 92 | return defined $self->{htgroups} ? $self->{htgroups} : $self->_htgroups_accessor; | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | if (@_) { | ||
| 96 | $self->_ApacheHtgroup( Apache::Htgroup->new($_[0]) ); | ||
| 97 | } | ||
| 98 | |||
| 99 | return $self->_htgroups_accessor(@_); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _op_group_check { | ||
| 104 | my ($htgroup, $user, $groupdef) = @_; | ||
| 105 | my ($op, @groups) = @$groupdef; | ||
| 106 | |||
| 107 | if (lc $op eq "all") { | ||
| 108 | foreach (@groups) { | ||
| 109 | return 0 unless ref $_ eq "ARRAY" | ||
| 110 | ? _op_group_check($htgroup, $user, $_) | ||
| 111 | : $htgroup->ismember($user, $_); | ||
| 112 | } | ||
| 113 | return 1; | ||
| 114 | } | ||
| 115 | elsif (lc $op eq "one") { | ||
| 116 | foreach (@groups) { | ||
| 117 | return 1 if ref $_ eq "ARRAY" | ||
| 118 | ? _op_group_check($htgroup, $user, $_) | ||
| 119 | : $htgroup->ismember($user, $_); | ||
| 120 | } | ||
| 121 | return 0; | ||
| 122 | } | ||
| 123 | elsif (lc $op eq "not") { | ||
| 124 | foreach (@groups) { | ||
| 125 | return 0 if ref $_ eq "ARRAY" | ||
| 126 | ? _op_group_check($htgroup, $user, $_) | ||
| 127 | : $htgroup->ismember($user, $_); | ||
| 128 | } | ||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | croak "bad group definition, unknown logical operand $op"; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub check { | ||
| 137 | my ($self, $user, $pass, %named) = @_; | ||
| 138 | |||
| 139 | return 0 unless $self->_ApacheHtpasswd->htCheckPassword($user, $pass); | ||
| 140 | |||
| 141 | if (defined $named{groups}) { | ||
| 142 | return 0 unless $self->groupcheck($user, %named); | ||
| 143 | } | ||
| 144 | |||
| 145 | return 1; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub groupcheck { | ||
| 149 | my ($self, $user, %named) = @_; | ||
| 150 | my @groups; | ||
| 151 | |||
| 152 | defined $named{groups} or croak "->groupcheck called with no groups to check"; | ||
| 153 | |||
| 154 | @groups = @{$named{groups}}; | ||
| 155 | |||
| 156 | GROUP: foreach (@groups) { | ||
| 157 | if (ref $_ eq "ARRAY") { | ||
| 158 | return 0 unless _op_group_check($self->_ApacheHtgroup, $user, $_); | ||
| 159 | } | ||
| 160 | else { | ||
| 161 | return 0 unless $self->_ApacheHtgroup->ismember($user, $_); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | return 1; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub new { | ||
| 169 | my ($proto, %opts) = @_; | ||
| 170 | my $class = ref $proto || $proto; | ||
| 171 | |||
| 172 | my $self = bless {}, $class; | ||
| 173 | |||
| 174 | $self->htusers($opts{htusers}) if $opts{htusers}; | ||
| 175 | $self->htgroups($opts{htgroups}) if $opts{htgroups}; | ||
| 176 | |||
| 177 | return $self; | ||
| 178 | } | ||
| 179 | |||
| 180 | 1; | ||
| 181 | __END__ | ||