ActiveDirectory.pm

filter: Statementstmt 75.2% 64 / 85 Branchbran 61.7% 21 / 34 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 85.7% 18 / 21 Podpod 100% 11 / 11 totaltotal 71.5% 118 / 165 CC 23 SCAR 13.1
CC
23
Cov
70%
authenticate
7.2
_create_connection
6.6
_v_is_error
6.1
CRAP 3.7
↑ Summary ActiveDirectory/User.pm »
1 package Auth::ActiveDirectory;
2  
3 =head1 NAME
4  
5 Auth::ActiveDirectory - Authentication module for MS ActiveDirectory
6  
7 =head1 VERSION
8  
9 Version 0.02
10  
11 =cut
12  
13 3 our $VERSION = '0.02';
14  
15 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Net::LDAP qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Net::LDAP::Constant qw[LDAP_INVALID_CREDENTIALS];
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 my $ErrorCodes = {
20 '525' => { error => 'user not found' },
21 '52e' => { error => 'invalid credentials' },
22 '530' => { error => 'not permitted to logon at this time' },
23 '531' => { error => 'not permitted to logon at this workstation' },
24 '532' => { error => 'password expired' },
25 '533' => { error => 'data 533' },
26 '701' => { error => 'account expired' },
27 '773' => { error => 'user must reset password' },
28 '775' => { error => 'user account locked' },
29 '534' => {
30 error => 'account disabled',
31 description => 'The user has not been granted the requested logon type at this machine'
32 },
33 };
34  
35 =head1 SUBROUTINES/METHODS
36  
37 =cut
38  
39 {
40  
41 =head2 _ad2unixtimestamp
42  
43 This value represents the number of 100-nanosecond intervals since January 1, 1601 (UTC).
44 https://msdn.microsoft.com
45  
46 ad_timestamp / nanoseconds - offset to 1601
47  
48 =cut
49  
50 1 ▶ sub _ad2unixtimestamp { $_[0] / 10000000 - 11644473600 }
Subroutine _ad2unixtimestamp: called CC=1 SCAR=0.0
51  
52 =head2 _create_connection
53  
54 =cut
55  
56 1 ▶ sub _create_connection {
Subroutine _create_connection: called CC=4 SCAR=18.9
57 1 my ( $host, $port, $timeout ) = @_;
58 0 ▶ return Net::LDAP->new( $host, port => $port || 389, timeout => $timeout || 60 ) || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 die qq/Failed to connect to '$host'. Reason: '$@'/;
60 0 return;
61 1 ▶ };
Condition42%3/7
'Net::LDAP'->new($host, 'port', $port || 389, 'timeout', $timeout || 60) || sub { die "Failed to connect to '${host}'. Reason: '$@'"; return; }
l!l&&r!l&&!r
010
$port || 389
l!l
10
$timeout || 60
l!l
10
MC/DC0%0/2
'Net::LDAP'->new($host, 'port', $port || 389, 'timeout', $timeout || 60) || sub { die "Failed to connect to '${host}'. Reason: '$@'"; return; }
'Net::LDAP'->new($host, 'port', $port || 389, 'timeout', $timeout || 60) sub { die "Failed to connect to '${host}'. Reason: '$@'"; return; }
MC/DC0%0/1
$port || 389
$port
MC/DC0%0/1
$timeout || 60
$timeout
Truth table33%1/3
A || B
A= 'Net::LDAP'->new($host, 'port', $port || 389, 'timeout', $timeout || 60)B= sub { die "Failed to connect to '${host}'. Reason: '$@'"; return; }
ABresult
1X1
011
000
Truth table50%1/2
A
A= $port
Aresult
11
00
Truth table50%1/2
A
A= $timeout
Aresult
11
00
62 }
63  
64 =head2 _v_is_error
65  
66 =cut
67  
68 2 ▶ sub _v_is_error {
Subroutine _v_is_error: called CC=3 SCAR=18.1
69 2 my ( $message, $s_user ) = @_;
70 2 ▶ return 0 if ( !$message->is_error );
Branch: 1 branch
BranchTrueFalseTotal
unless $message->is_error 2 0 2
71 0 my $error = $message->error;
72 0 ▶ my $level = $message->code == LDAP_INVALID_CREDENTIALS ? 'debug' : 'error';
Branch: 1 branch
BranchTrueFalseTotal
$message->code == 49 ? : 0 0 0
73 0 die qq/Failed to authenticate user '$s_user'. Reason: '$error'/;
74 0 return 1;
75 }
76  
77 =head2 _parse_error_message
78  
79 =cut
80  
81 0 ▶ sub _parse_error_message {
Subroutine _parse_error_message: not called CC=1 SCAR=6.9
82 0 my ($message) = @_;
83 0 my ($errorcode) = $message->{errorMessage} =~ m/(?:data\s(.*)),/;
84 0 return $ErrorCodes->{$errorcode};
85 }
86  
87 =head2 _search_users
88  
89 =cut
90  
91 2 ▶ sub _search_users {
Subroutine _search_users: called CC=1 SCAR=0.0
92 2 my ( $self, $filter ) = @_;
93 2 return $self->ldap->search( base => $self->base, filter => $filter );
94 }
95  
96 }
97  
98 =head2 new
99  
100 Constructor
101  
102 =cut
103  
104 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
105 2 my $class = shift;
106 2 my $self = {@_};
107 2 bless $self, $class;
108 2 ▶ $self->{base} = qq/dc=$self->{domain},dc=$self->{principal}/ unless $self->{base};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"base"} 2 0 2
109 2 ▶ $self->{ldap} = _create_connection( $self->{host}, $self->{port}, $self->{timeout} ) unless $self->{ldap};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ldap"} 1 1 2
110 2 return $self;
111 }
112  
113 =head2 authenticate
114  
115 Basicaly the subroutine for authentication in the ActiveDirectory
116  
117 =cut
118  
119 1 ▶sub authenticate {
Subroutine authenticate: called CC=6 SCAR=19.7
Pod documented
120 1 my ( $self, $username, $password ) = @_;
121 1 ▶ return unless $self->ldap;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ldap 0 1 1
122 1 my $user = sprintf( '%s@%s', $username, $self->domain );
123 1 my $message = $self->ldap->bind( $user, password => $password );
124 1 ▶ if ( _v_is_error( $message, $user ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_v_is_error($message, $user)) 0 1 1
125 0 $self->error_message( _parse_error_message($message) );
126 0 return;
127 }
128  
129 1 my $result = $self->_search_users( qq/(&(objectClass=person)(userPrincipalName=$user./ . $self->principal . '))' );
130 1 foreach ( $result->entries ) {
131 1 require Auth::ActiveDirectory::Group;
132 1 require Auth::ActiveDirectory::User;
133 return Auth::ActiveDirectory::User->new(
134 uid => $username,
135 user => $user,
136 firstname => $_->get_value(q/givenName/),
137 surname => $_->get_value(q/sn/),
138 display_name => $_->get_value(q/displayName/),
139 mail => $_->get_value(q/mail/),
140 last_password_set => _ad2unixtimestamp( $_->get_value('pwdLastSet') ),
141  
142 # A value of 0 or 0x7FFFFFFFFFFFFFFF (9223372036854775807) indicates that the account never expires.
143 # https://msdn.microsoft.com/en-us/library/ms675098(v=vs.85).aspx
144 account_expires => ( $_->get_value('accountExpires') != 9223372036854775807 ) ? _ad2unixtimestamp( $_->get_value('accountExpires') ) : undef,
145 1 ▶ groups => [ map { m/^CN=(.*),OU=.*$/ ? Auth::ActiveDirectory::Group->new( name => $1 ) : () } $_->get_value(q/memberOf/) ],
Branch: 2 branches
BranchTrueFalseTotal
$_->get_value('accountExpires') != 9223372036854775807 ? : 0 1 1
/^CN=(.*),OU=.*$/ ? : 4 0 4
146  
147 );
148 }
149 0 return;
150 }
151  
152 =head2 list_users
153  
154 =cut
155  
156 1 ▶sub list_users {
Subroutine list_users: called CC=3 SCAR=11.8
Pod documented
157 1 my ( $self, $user, $password, $search_string ) = @_;
158 1 ▶ my $connection = $self->ldap || return;
Condition50%1/2
$self->ldap || (return)
l!l
10
MC/DC0%0/1
$self->ldap || (return)
$self->ldap
Truth table50%1/2
A
A= $self->ldap
Aresult
11
00
159 1 my $message = $connection->bind( $user, password => $password );
160 1 ▶ if ( _v_is_error( $message, $user ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_v_is_error($message, $user)) 0 1 1
161 0 $self->error_message( _parse_error_message($message) );
162 0 return;
163 }
164 1 my $result = $self->_search_users(qq/(&(objectClass=person)(name=$search_string*))/);
165 1 return [ map { Auth::ActiveDirectory::User->new( name => $_->get_value(q/name/), uid => $_->get_value(q/sAMAccountName/) ) } $result->entries ];
166 }
167  
168 =head2 host
169  
170 Getter/Setter for internal hash key host.
171  
172 =cut
173  
174 2 ▶sub host {
Subroutine host: called CC=2 SCAR=6.9
Pod documented
175 2 ▶ return $_[0]->{host} unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 1 1 2
176 1 $_[0]->{host} = $_[1];
177 1 return $_[0]->{host};
178 }
179  
180 =head2 port
181  
182 Getter/Setter for internal hash key port.
183  
184 =cut
185  
186 2 ▶sub port {
Subroutine port: called CC=2 SCAR=6.9
Pod documented
187 2 ▶ return $_[0]->{port} unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 1 1 2
188 1 $_[0]->{port} = $_[1];
189 1 return $_[0]->{port};
190 }
191  
192 =head2 timeout
193  
194 Getter/Setter for internal hash key timeout.
195  
196 =cut
197  
198 2 ▶sub timeout {
Subroutine timeout: called CC=2 SCAR=6.9
Pod documented
199 2 ▶ return $_[0]->{timeout} unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 1 1 2
200 1 $_[0]->{timeout} = $_[1];
201 1 return $_[0]->{timeout};
202 }
203  
204 =head2 domain
205  
206 Getter/Setter for internal hash key domain.
207  
208 =cut
209  
210 3 ▶sub domain {
Subroutine domain: called CC=2 SCAR=6.9
Pod documented
211 3 ▶ return $_[0]->{domain} unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 2 1 3
212 1 $_[0]->{domain} = $_[1];
213 1 return $_[0]->{domain};
214 }
215  
216 =head2 principal
217  
218 Getter/Setter for internal hash key principal.
219  
220 =cut
221  
222 3 ▶sub principal {
Subroutine principal: called CC=2 SCAR=6.9
Pod documented
223 3 ▶ return $_[0]->{principal} unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 2 1 3
224 1 $_[0]->{principal} = $_[1];
225 1 return $_[0]->{principal};
226 }
227  
228 =head2 ldap
229  
230 Getter/Setter for internal hash key ldap.
231  
232 =cut
233  
234 5 ▶sub ldap {
Subroutine ldap: called CC=2 SCAR=10.5
Pod documented
235 5 ▶ return $_[0]->{ldap} unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 5 0 5
236 0 $_[0]->{ldap} = $_[1];
237 0 return $_[0]->{ldap};
238 }
239  
240 =head2 base
241  
242 Getter/Setter for internal hash key base.
243  
244 =cut
245  
246 2 ▶sub base {
Subroutine base: called CC=2 SCAR=10.5
Pod documented
247 2 ▶ return $_[0]->{base} unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 2 0 2
248 0 $_[0]->{base} = $_[1];
249 0 return $_[0]->{base};
250 }
251  
252 =head2 error_message
253  
254 Get error message if something is going wrong.
255  
256 =cut
257  
258 0 ▶sub error_message {
Subroutine error_message: not called CC=2 SCAR=17.9
Pod documented
259 0 ▶ return $_[0]->{_error_message} unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
260 0 $_[0]->{_error_message} = $_[1];
261 0 return $_[0]->{_error_message};
262 }
263  
264 3 1; # Auth::ActiveDirectory
265  
266 __END__
↑ Summary ActiveDirectory/User.pm »