-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrowdCookieAuth.pm
375 lines (276 loc) · 10.7 KB
/
CrowdCookieAuth.pm
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
package Apache::CrowdCookieAuth;
use 5.008000;
use strict;
use warnings;
use Exporter;
use Cache::FileCache;
use Atlassian::Crowd;
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
use APR::SockAddr ();
$SOAP::Constants::DO_NOT_USE_XML_PARSER = 1;
# Uncomment the following line (and comment out the line below it) to
# enable debug output of the SOAP messages.
# use SOAP::Lite +trace => qw (debug);
use SOAP::Lite;
our @ISA = qw(Exporter);
our $VERSION = '1.2.3';
# namespace attribute used in SOAP call creation.
my $XMLNS = 'http://authentication.integration.crowd.atlassian.com';
# Use correct API for loaded version of mod_perl.
#
BEGIN {
unless ( $INC{'mod_perl.pm'} ) {
my $class = 'mod_perl';
if ( exists $ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2 ) {
$class = 'mod_perl2';
}
eval "require $class";
}
my @import = qw( OK HTTP_UNAUTHORIZED SERVER_ERROR );
if ( $mod_perl::VERSION >= 1.999022 ) { # mod_perl 2.0.0 RC5
require Apache2::RequestRec;
require Apache2::RequestUtil;
require Apache2::RequestIO;
require Apache2::Log;
require Apache2::Connection;
require Apache2::Const;
require Apache2::Access;
Apache2::Const->import(@import);
}
elsif ( $mod_perl::VERSION >= 1.99 ) {
require Apache::RequestRec;
require Apache::RequestUtil;
require Apache::RequestIO;
require Apache::Log;
require Apache::Connection;
require Apache::Const;
require Apache::Access;
Apache::Const->import(@import);
}
else {
require Apache;
require Apache::Log;
require Apache::Constants;
Apache::Constants->import(@import);
}
}
use constant MP2 => $mod_perl::VERSION >= 1.999022 ? 1 : 0;
# ---------------------------------------------------------------------------
# Create the cache
sub init_cache($) {
my $r = shift;
my $cache;
my $cache_location = $r->dir_config('CrowdCacheLocation');
if(!defined $cache_location) {
# use default location $TEMP/FileCache
$cache = new Cache::FileCache( { namespace => $r->auth_name()} );
} else {
$cache = new Cache::FileCache( { cache_root => $cache_location,
namespace => $r->auth_name()} );
}
return $cache;
}
# ---------------------------------------------------------------------------
sub read_options($) {
my $r = shift;
my $rlog = $r->log;
# Get parameters from the apache conf file
my $login_page= $r->dir_config('CrowdLoginPage');
my $useproxy = $r->dir_config('CrowdUseProxy');
my $app_name = $r->dir_config('CrowdAppName');
my $app_credential = $r->dir_config('CrowdAppPassword');
my $cache_enabled = $r->dir_config('CrowdCacheEnabled') || 'on';
my $cache_expiry = $r->dir_config('CrowdCacheExpiry') || '300';
$cache_expiry = $cache_expiry.' seconds';
my $soaphost = $r->dir_config('CrowdSOAPURL') || "http://localhost:8095/crowd/services/SecurityServer";
my $disable_parser = $r->dir_config('CrowdUseInternalXMLParser') || 'yes';
# By default, SOAP::Lite uses XML::Parser, which uses libexpat, which can
# conflict with some apache builds and cause segfaults. This option tells
# SOAP::Lite to use an internal pure-perl parser
if(defined($disable_parser) && ($disable_parser eq 'yes')) {
$SOAP::Constants::DO_NOT_USE_XML_PARSER = 1;
}
return ($app_name, $app_credential, $cache_enabled, $cache_expiry, $soaphost,$login_page,$useproxy);
}
# ---------------------------------------------------------------------------
sub get_app_token($$$$$$) {
my ($r, $app_name, $app_credential, $soaphost, $cache, $cache_expiry) = @_;
my $apptoken;
my $rlog = $r->log;
if(defined $cache) {
$apptoken = $cache->get($app_name.'___APP');
}
if(!defined $apptoken) {
$apptoken = Atlassian::Crowd::authenticate_app($soaphost, $app_name, $app_credential);
if((defined $cache) && (defined $apptoken)) {
$rlog->debug('CrowdAuth: app token cache miss!...');
# purge whenever we re-auth the app to clear out expired entries
$cache->purge();
$cache->set($app_name.'___APP', $apptoken, $cache_expiry);
}
} else {
$rlog->debug('CrowdAuth: app token cache hit!...'.$apptoken);
}
return $apptoken;
}
sub trim
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# ---------------------------------------------------------------------------
# Entry Point
#
sub handler {
my $r = shift;
my $userAgent = $r->headers_in->{'User-Agent'} || '';
my $c = $r->connection;
my $remoteAddress = $c->remote_addr()->ip_get;
my $base_server = "127.0.0.1";
my $rlog = $r->log;
my $cookie = $r->headers_in->{Cookie} || '';
my @cookies = split(/;/,$cookie);
my $token;
foreach my $singlecookie (@cookies) {
my @cookieval = split(/=/,$singlecookie);
my $cookiename = trim($cookieval[0]);
if($cookiename eq "crowd.token_key") {
$token = trim($cookieval[1]);
}
}
my ($app_name, $app_credential, $cache_enabled, $cache_expiry, $soaphost, $login_page, $useproxy) = read_options($r);
my $cache;
if($cache_enabled eq 'on') {
# Initialise the cache
$cache = init_cache($r);
}
my $apptoken = get_app_token($r, $app_name, $app_credential, $soaphost, $cache, $cache_expiry);
my $isValid;
if(defined $token) {
$isValid = isValidPrincipalToken($soaphost,$app_name,$apptoken,$token,$userAgent,$remoteAddress,$base_server,$useproxy);
#$rlog->error("token :$token: userAgent :$userAgent: address :$remoteAddress: useproxy :$useproxy: isValid :$isValid:");
}
if(!defined $isValid) {
$r->status(302);
$r->headers_out->add('Location' => $login_page . "?loc=" . $r->uri());
$r->headers_out->add('Cache-Control' => 'no-cache,no-store');
$r->headers_out->add('Pragma' => 'no-cache');
}
return OK;
}
# ---------------------------------------------------------------------------
# Validate principal token
sub isValidPrincipalToken {
my ($serverURL, $app_name, $apptoken, $userToken,$userAgent,$remote_address,$apache_address,$useproxy) = @_;
my $principal_method = SOAP::Data->name('isValidPrincipalToken');
my $validationFactor1 = SOAP::Data->name('ValidationFactor' =>
\SOAP::Data->value(
SOAP::Data->name('name' => "USER_AGENT")->type('string')->attr({xmlns => $XMLNS}),
SOAP::Data->name('value' => $userAgent)->type('string')->attr({xmlns => $XMLNS})
)
);
my $validationFactor2;
my $validationFactor3;
my $in2_message;
if(defined $useproxy) {
$validationFactor2 = SOAP::Data->name('ValidationFactor' =>
\SOAP::Data->value(
SOAP::Data->name('name' => "REMOTE_ADDRESS")->type('string')->attr({xmlns => $XMLNS}),
SOAP::Data->name('value' => $apache_address)->type('string')->attr({xmlns => $XMLNS})
)
);
$validationFactor3 = SOAP::Data->name('ValidationFactor' =>
\SOAP::Data->value(
SOAP::Data->name('name' => "X-Forwarded-For")->type('string')->attr({xmlns => $XMLNS}),
SOAP::Data->name('value' => $remote_address)->type('string')->attr({xmlns => $XMLNS})
)
);
$in2_message = SOAP::Data->name( 'in2' => \SOAP::Data->value($validationFactor1,$validationFactor2,$validationFactor3));
} else {
$validationFactor2 = SOAP::Data->name('ValidationFactor' =>
\SOAP::Data->value(
SOAP::Data->name('name' => "REMOTE_ADDRESS")->type('string')->attr({xmlns => $XMLNS}),
SOAP::Data->name('value' => $remote_address)->type('string')->attr({xmlns => $XMLNS})
)
);
$in2_message = SOAP::Data->name( 'in2' => \SOAP::Data->value($validationFactor1,$validationFactor2));
}
my @principal_params = (
SOAP::Data->name(
'in0' => \SOAP::Data->value( SOAP::Data->name( 'name' => $app_name )->type('string')->attr( { xmlns => $XMLNS } ), SOAP::Data->name( 'token' => $apptoken )->attr( { xmlns => $XMLNS } ), )
),
SOAP::Data->name( 'in1' => $userToken )->type('string')->attr( { xmlns => $XMLNS } ),
$in2_message
#SOAP::Data->name( 'in2' =>
# \SOAP::Data->value(
# $validationFactor1,
# $validationFactor2
# )
#)
);
my $response = Atlassian::Crowd::make_soap_call( $serverURL, 'isValidPrincipalToken', @principal_params );
if (!defined($response)) {
return undef;
} elsif ($response->fault) {
return undef;
}
my $isvalid = $response->valueof('//isValidPrincipalTokenResponse/out');
if(!defined($isvalid) || $isvalid eq "false") {
return undef;
}
return 1;
}
# ---------------------------------------------------------------------------
1;
__END__
=head1 NAME
Apache::CrowdCookieAuth - Apache authentication handler that uses Atlassian Crowd.
=head1 SYNOPSIS
<Location /location>
AuthName crowd
AuthType Basic
PerlAuthenHandler Apache::CrowdCookieAuth
PerlSetVar CrowdAppName appname
PerlSetVar CrowdAppPassword apppassword
PerlSetVar CrowdSOAPURL http://localhost:8095/crowd/services/SecurityServer
PerlSetVar CrowdCacheEnabled on
PerlSetVar CrowdCacheLocation /tmp/CrowdAuthCache
PerlSetVar CrowdCacheExpiry 300
PerlSetVar CrowdLoginPage http://my.site.com/login.cgi
PerlSetVar CrowdUseProxy true
require valid-user
</Location>
=head1 DESCRIPTION
This Module allows you to configure Apache to use Atlassian Crowd to
handle authentication using the crowd domain cookie. This module is
based on a combination of Atlassian's CrowdAuth code and SourceSense's
modifications. Rather than patch Atlassian's code on-disk, I created
a new self-contained module with a different name.
=head2 EXPORT
None by default.
=head1 SEE ALSO
http://confluence.atlassian.com/x/rgGY
http://www.atlassian.com/crowd
http://opensource.sourcesense.com/confluence/display/CPL/Crowd+Apache+Connector
http://forums.atlassian.com/thread.jspa?threadID=23732
http://jira.atlassian.com/browse/CWD-1440
=head1 AUTHOR
Original Perl library by Atlassian. Cookie code by Gustavo Nalle
Fernandes ([email protected]). login.cgi code by Marko
Nordberg. Packaged into one item and changed to CrowdCookieAuth and
login.cgi enhancements by Jim Browne (http://blog.jbrowne.com)
=head1 COPYRIGHT AND LICENSE
Perl Module:
Copyright (C) 2007 by Atlassian
Copyright (C) 2009 by Gustavo Nalle Fernandes ([email protected])
Copyright (c) 2010 by Jim Browne, assigned to Atlassian
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.
Associated login.cgi script:
Copyright (C) 2008 by Marko Nordberg
"Permission is granted to use [login.cgi] free for any usage"
Copyright (C) 2010 by Jim Browne, assigned to Atlassian, Perl license
=cut