-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAAF.pm
347 lines (233 loc) · 7.49 KB
/
AAF.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
package Osiris::AAF;
use strict;
use Data::Dumper;
use Log::Log4perl;
use JSON::WebToken;
use Digest::SHA qw(sha256_hex);
use Fcntl qw(:flock SEEK_END);
=head1 NAME
Osiris::AAF
=head1 SYNOPSIS
my $oaaf = Osiris::AAF->new( config => $aafcf );
my $claims = $oaaf->decode(jwt => $jwt);
if( $claims ) {
if( my $attributes = $oaaf->verify(claims => $claims) ) {
if( my $user_id = $oaaf->user_id(attributes => $attributes) ) {
# ... Let them in ...
} else {
template 'error' => {
title => 'Error',
error => 'Authentication failed.'
};
}
} else {
warn("AAF JWT authentication failed");
send_error(403, "Not allowed");
}
} else {
warn("AAF JWT decryption failed");
send_error(403, "Not allowed");
}
=head1 DESCRIPTION
Simple Perl implementation of AAF's Rapid Connect JWT platform.
See https://rapid.aaf.edu.au/developers for details.
=head1 METHODS
=over 4
=item new(config => { ... config ... }, file => $file)
Create a new Osiris::AAF object with the given configuration parameters
=over 4
=item url: this app's AAF URL
=item iss: either the test or live AAF url
=item aud: the URL of your application
=item jtistore: a file used to store jti values to prevent replay attacks
=item secret: the secret key shared with AAF
=item attributes: the attributes key
=back
Returns the new object if successful, otherwise undef.
=cut
my @CONFIG_VARS = qw(iss aud jtistore secret attributes);
sub new {
my ( $class, %params ) = @_;
my $self = {};
bless $self, $class;
$self->{log} = Log::Log4perl->get_logger($class);
if( !$params{config} ) {
$self->{log}->error("No config for $class");
die;
}
my $missing = 0;
for my $val ( @CONFIG_VARS ) {
$self->{$val} = $params{config}->{$val} || do {
$self->{log}->error("Missing config parameter $val");
$missing = 1;
}
}
if( $missing ) {
$self->{log}->error("Incomplete config");
return undef;
}
return $self;
}
=item encode(claims => $claimshash )
Encode a claims set passed in as a hashref. This is used to create
web tokens locally for testing this library and the Osiris endpoint
before registration with the AAF. See get /auth/fakeaff in Osiris.pm.
The claims hashref is of the form:
{
iss =>
aud =>
nbf => $not_before,
exp => $expiry,
jti => $jti_timestamp,
$test_conf->{attributes} => $hash_of_test_atts.
}
Returns a JSON web token.
=cut
sub encode {
my ( $self, %params ) = @_;
my $claims = $params{claims};
if( !$claims ) {
$self->{log}->error(
"encode method needs 'claims' parameter"
);
return undef;
}
my $jwt = encode_jwt($claims, $self->{secret});
return $jwt;
}
=item decode(jwt => $jwt)
Attempt to decode a JWT assertion with the secret key. If successful,
returns the claims as a hashref. This is called on returning from the
AAF, which passes the jwt to a POST method.
=cut
sub decode {
my ( $self, %params ) = @_;
my $jwt = $params{jwt};
if( !$jwt ) {
$self->{log}->error("encode method needs 'jwt'");
return undef;
}
return decode_jwt($jwt, $self->{secret});
}
=item verify(claims => $claims)
Verify the claims hashref against the config values, the current time
and the jti store. Writes errors into the logs for any mismatches.
The AAF attributes (cn, mail, displayname etc) are returned as a claim
called 'https://aaf.edu.au/attributes' - the URL to use for looking this
up is configured as aaf.attributes.
=cut
sub verify {
my ( $self, %params ) = @_;
my $claims = $params{claims};
if( !$claims ) {
$self->{log}->error("verify needs a claims hashref");
return undef;
}
$self->{log}->debug("Claims = " . Dumper({claims => $claims}));
my $now = time;
my $success = 1;
if( $claims->{iss} ne $self->{iss} ) {
$self->{log}->error("iss mismatch: $claims->{iss}");
$success = 0;
}
if( $claims->{aud} ne $self->{aud} ) {
$self->{log}->error("aud mismatch: $claims->{aud}");
$success = 0;
}
if( $now <= $claims->{nbf} ) {
$self->{log}->error("Current time $now is earlier than nbf $claims->{nbf}");
$success = 0;
}
if( $now >= $claims->{exp} ) {
$self->{log}->error("Current time $now is later than exp $claims->{exp}");
$success = 0;
}
if( !$self->store_jti(jti => $claims->{jti}) ) {
$self->{log}->error("JTI storage failed");
$success = 0;
}
if( !$success ) {
$self->{log}->error("JWT authentication failed.");
}
if( $success ) {
return $claims->{$self->{attributes}};
} else {
return undef;
}
}
=item user_id(attributes => $atts)
Takes a set of user attributes and returns a unique string that can
be used as a user ID (and to create working directories).
On AAF's recommendation, we're using a SHA-256 hex digest of the complete
EduPersonTargetedID string.
=cut
sub user_id {
my ( $self, %params ) = @_;
my $atts = $params{attributes} || do {
$self->{log}->error("user_id needs an attributes hashref");
return undef;
};
my $id = $atts->{edupersontargetedid} || do {
$self->{log}->error("No edupersontargetedid in attributes");
return undef;
};
my $user_id = sha256_hex($id);
$self->{log}->debug("Hashed $id\n to $user_id");
return $user_id;
}
=item store_jti(jti => $jti)
Single method for looking up and/or storing the jti unique identifier.
These are a requirement of the AAF - we need to keep a record of timestamps
for each authentication so that we can rule out replay attacks.
Tries to get a lock on the jti file, and when it does, looks up the jti.
If the jti is found, release the file and return undef (because this
is evidence of a replay attack).
If it's not found, add it to the store and write and release the file,
return true.
=cut
sub store_jti {
my ( $self, %params ) = @_;
my $new_jti = $params{jti};
my %jtis;
if( ! -f $self->{jtistore} ) {
$self->{log}->warn("JTI store $self->{jtistore} not found: creating");
%jtis = ();
} else {
open(my $fh, "<$self->{jtistore}") || do {
$self->{log}->error("Couldn't open $self->{jtistore}: $!");
return undef;
};
flock($fh, LOCK_SH) || do {
$self->{log}->error("Can't lock $self->{jtistore}: $!");
return undef;
};
while( my $l = <$fh> ) {
chomp $l;
my ( $jti, $timestamp ) = split(/ /, $l);
if( $jti eq $new_jti ) {
$self->{log}->error("Duplicate jti: $jti $timestamp");
close($fh);
return undef;
}
$jtis{$jti} = $timestamp;
}
close($fh);
}
$jtis{$new_jti} = time;
open(my $fh, ">$self->{jtistore}") || do {
$self->{log}->error("Couldn't open $self->{jtistore}: $!");
return undef;
};
flock($fh, LOCK_EX) || do {
$self->{log}->error("Can't lock_ex $self->{jtistore}: $!");
return undef;
};
for my $k ( sort { $a cmp $b } keys %jtis ) {
print $fh "$k $jtis{$k}\n";
}
close($fh);
return 1;
}
=back
=cut
1;