/
README
412 lines (327 loc) · 13.1 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
NAME
Mojolicious::Plugin::BasicAuthPlus - Basic HTTP Auth Helper Plus
VERSION
Version 0.11.3
SYNOPSIS
# Mojolicious::Lite
plugin 'basic_auth_plus';
get '/' => sub {
my $self = shift;
$self->render(text => 'ok')
if $self->basic_auth(
"Realm Name" => {
username => 'username',
password => 'password'
}
);
};
# Mojolicious
$self->plugin('BasicAuthPlus');
sub index {
my $self = shift;
$self->render(text => 'ok')
if $self->basic_auth(
"My Realm" => {
path => '/path/to/some/passwd/file.txt'
}
);
}
DESCRIPTION
Mojolicious::Plugin::BasicAuthPlus is a helper for basic HTTP
authentication that supports multiple authentication schemes, including
a callback, explicit username and password (plaintext or encrypted)
without a callback, a passwd file, LDAP, and Active Directory.
METHODS
Mojolicious::Plugin::BasicAuthPlus inherits all methods from
Mojolicious::Plugin and implements the following new ones.
"register"
$plugin->register;
Register condition in Mojolicious application.
"basic_auth"
Configure specific auth method (see CONFIGURATION). Returns a
two-element list, where the first element is a hash reference and the
second is an integer (1 for success, 0 for failure).
The hash reference contains one key/value pair for the username used to
authenticate, and when LDAP is used it may also contain the 'ldap' key
whose value is the active LDAP connection handle if requested by setting
the return_ldap_handle option (see options below).
Generally, you can ignore this; thus, for example, both of the following
are valid:
my ($hash_ref, $auth_ok)
= $self->basic_auth(
"My Realm" => {
username => 'zapp',
password => 'brannigan'
}
);
if ($auth_ok) {
$self->app->log->info("Auth success for $hash_ref->{username}");
render(text => 'ok');
}
$self->render(text => 'ok')
if $self->basic_auth(
"My Realm" => {
username => 'zapp',
password => 'brannigan'
}
);
CONFIGURATION
The basic_auth method takes an HTTP Basic Auth realm name that is either
a code ref for a subroutine that will do the authentication check, or a
hash, where the realm is the hash name. When the realm represents a
named hash, the key/value pairs specify the source of user credentials
and determine the method used for authentication (e.g., passwd file,
LDAP, Active Directory).
Realm names may contain whitespace.
If a username and password are defined, then other options pertaining to
a passwd file or LDAP/ActiveDirectory authentication will be ignored,
because it it assumed you intend to compare the defined username and
password against those supplied by the user.
The following options may be set in the hash:
username
Specify the username to match.
password
Specify the password to match. The string may be plaintext or use any of
the formats noted in Authen::Simple::Password.
path
The location of a password file holding user credentials. Per
Authen::Simple::Passwd, "Any standard passwd file that has records
seperated with newline and fields seperated by ":" is supported. First
field is expected to be username and second field, plain or encrypted
password. Required."
host
The hostname or IP address of an LDAP or Active Directory server.
basedn
The base DN to use with LDAP or Active Directory.
binddn
The bind DN to use when doing an authenticated bind against LDAP or
Active Directory.
bindpw
The password to use when doing an authenticated bind to LDAP or Active
Directory.
scope
The search scope for LDAP or Active Directory. Choices are 'base' |
'one' | 'sub' | 'subtree' | 'children', but the default is 'sub'. See
Net::LDAP for further discussion.
filter
The LDAP/ActiveDirectory filter to use when searching a directory.
port
The TCP port to use for an LDAP/ActiveDirectory connection. The default
is 389.
debug
Set the LDAP debug level. See the debug method in Net::LDAP for details.
The default value is 0, debugging off.
timeout
Timeout in seconds passed to IO::Socket when connecting to a remote LDAP
server. The default is 120.
version
Set the LDAP protocol version being used (default is LDAPv3). To talk to
an older server, for example one using LDAPv2, set this to 2. With
modern LDAP implementations, you shouldn't need to bother setting this.
start_tls
Enable TLS support for LDAP. This is the default. If you do not want
TLS, set this to zero, but it's recommended to take the default.
tls_verify
For SSL certificate validation, set tls_verify to 'none' | 'optional' |
'require'. The default is 'optional'. See Net::LDAP for more
information.
cafile
The path to your CA or CA chain certificate file. Required in TLS mode
for LDAP if tls_verify is true.
return_ldap_handle
When authenticating against LDAP, the plugin will do an unbind operation
to close the connection with the LDAP server after an authentication
success or failure. In some cases, it may be useful to return the active
LDAP connection handle to your calling code so that further LDAP
operations can be performed after authentication succeeds. To enable
this, set return_ldap_handle.
Note that the last bind operation on the connection will be that of the
end user you're trying to authenticate, so once you get the handle back
any LDAP operation you attempt to execute will have only the LDAP
privileges granted to the end user who just authenticated. If you need
the LDAP privileges of your administrative bind DN or other user, you'll
need to do a fresh bind using the same handle. Rebinding will probably
work with many modern LDAP implementations, but it is not guaranteed.
The default behavior for the plugin is to close the LDAP connection and
not return a connection handle.
logging
If set, this enables some logging of successes and failures for
authentication, LDAP binding, etc. The default is no logging.
EXAMPLES
# With callback
get '/' => sub {
my $self = shift;
return $self->render(text => 'ok')
if $self->basic_auth(
realm => sub { return 1 if "@_" eq 'username password' }
);
};
# With callback and getting username from return hash ref.
get '/' => sub {
my $self = shift;
my ($href, $auth_ok) = $self->basic_auth(
realm => sub { return 1 if "@_" eq 'username password' }
);
if ($auth_ok) {
return $self->render(
status => 200,
text => 'ok',
msg => "Welcome $href->{username}"
);
}
else {
return $self->render(
status => 401,
text => 'unauthorized',
msg => "Sorry $href->{username}"
);
}
};
# With encrypted password
get '/' => sub {
my $self = shift;
$self->render(text => 'ok')
if $self->basic_auth(
"Realm Name" => {
username => 'username',
password => 'MlQ8OC3xHPIi.'
}
);
};
# Passwd file authentication
get '/' => sub {
my $self = shift;
$self->render(text => 'ok')
if $self->basic_auth(
"Realm Name" => {
path => '/path/to/passwd/file.txt'
}
);
};
# LDAP authentication (with anonymous bind)
get '/' => sub {
my $self = shift;
$self->render(text => 'ok')
if $self->basic_auth(
"Realm Name" => {
host => 'ldap.company.com',
basedn => 'ou=People,dc=company,dc=com'
}
);
};
# LDAP authentication over TLS/SSL (with authenticated bind)
get '/' => sub {
my $self = shift;
my ($hash_ref, $auth_ok)
= $self->basic_auth(
"Realm Name" => {
host => 'ldap.company.com',
basedn => 'ou=People,dc=domain,dc=com',
binddn => 'cn=bender,ou=People,dc=domain,dc=com',
bindpw => 'secret',
filter => '(&(objectClass=person)(cn=%s))',
cafile => '/some/path/to/ca.cert',
tls_verify => 'require'
}
);
$self->render(text => 'ok') if $auth_ok;
};
# LDAP authentication over TLS/SSL (with authentciated bind),
# returning the active LDAP handle and using it to do an additional
# search. Logging is also enabled.
get '/' => sub {
my $self = shift;
my ($hash_ref, $auth_ok)
= $self->basic_auth(
"Realm Name" => {
host => 'ldap.company.com',
basedn => 'ou=People,dc=domain,dc=com',
binddn => 'cn=bender,ou=People,dc=domain,dc=com',
bindpw => 'secret',
filter => '(&(objectClass=person)(cn=%s))',
cafile => '/some/path/to/ca.cert',
tls_verify => 'require',
logging => 1,
return_ldap_handle => 1
}
);
if ($hash_ref->{ldap}) {
my $ldap = $hash_ref->{ldap};
my $username = $hash_ref->{username};
my @fields = qw(cn sn mail);
my $filter = join '', map { "($_=*$username*)" } @fields;
$filter = '(|' . $filter . ')';
my $mesg = $ldap->search(
base => 'dc=domain,dc=com',
scope => 'sub',
filter => $filter,
attrs => [ 'cn', 'sn', 'mail' ]
);
croak $mesg->error if $mesg->code;
my @entries = $mesg->entries;
for my $entry (@entries) {
my $email = $entry->get_value('mail');
$self->app->log->info("Email address for $username is $email.");
}
$ldap->unbind;
}
$self->render(text => 'ok') if $auth_ok;
};
# Active Directory authentication (with authenticated bind)
get '/' => sub {
my $self = shift;
$self->render(text => 'ok')
if $self->basic_auth(
"Realm Name" => {
host => 'ldap.company.com',
basedn => 'dc=company,dc=com',
binddn => 'ou=People,dc=company,dc=com',
bindpw => 'secret',
filter =>
'(&(objectClass=organizationalPerson)(userPrincipalName=%s))'
}
);
};
BUGS
Please report any bugs or feature requests to
"bug-mojolicious-plugin-basicauthplus at rt.cpan.org", or through the
web interface at
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-Basic
AuthPlus>. I will be notified, and then you'll automatically be notified
of progress on your bug as I make changes.
DEVELOPMENT
<http://github.com/stregone/mojolicious-plugin-basicauthplus>
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Mojolicious::Plugin::BasicAuthPlus
You can also look for information at:
* RT: CPAN's request tracker (report bugs here)
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-BasicAu
thPlus>
* AnnoCPAN: Annotated CPAN documentation
<http://annocpan.org/dist/Mojolicious-Plugin-BasicAuthPlus>
* CPAN Ratings
<http://cpanratings.perl.org/d/Mojolicious-Plugin-BasicAuthPlus>
item * Search CPAN
<http://search.cpan.org/dist/Mojolicious-Plugin-BasicAuthPlus/>
ACKNOWLEDGEMENTS
Based on Mojolicious::Plugin::BasicAuth, by Glen Hinkle
<tempire@cpan.org>.
AUTHOR
Brad Robertson <blr@cpan.org>
CONTRIBUTORS
Nicolas Georges
Jay Mortensen
Mark Muldoon
G.Y. Park
Jan Paul Schmidt
SEE ALSO
Mojolicious, Mojolicious::Guides, <http://mojolicio.us>,
Authen::Simple::Password, Authen::Simple::Passwd, Net::LDAP
COPYRIGHT
Copyright (c) 2013-2018 by Brad Robertson.
LICENSE
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.