line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package UID2::Client::KeyContainer; |
2
|
4
|
|
|
4
|
|
367
|
use strict; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
93
|
|
3
|
4
|
|
|
4
|
|
17
|
use warnings; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
89
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
2041
|
use List::MoreUtils qw(upper_bound); |
|
4
|
|
|
|
|
45108
|
|
|
4
|
|
|
|
|
24
|
|
6
|
4
|
|
|
4
|
|
3438
|
use UID2::Client::Timestamp; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
1379
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
56
|
|
|
56
|
0
|
361
|
my ($class, @keys) = @_; |
10
|
56
|
|
|
|
|
75
|
my $latest_expires = 0; |
11
|
56
|
|
|
|
|
68
|
my (%keys, %keys_by_site); |
12
|
56
|
|
|
|
|
85
|
for my $key (@keys) { |
13
|
135
|
|
|
|
|
486
|
$keys{$key->id} = $key; |
14
|
135
|
100
|
|
|
|
675
|
if ($key->site_id > 0) { |
15
|
109
|
|
100
|
|
|
459
|
push @{$keys_by_site{$key->site_id} //= []}, $key; |
|
109
|
|
|
|
|
157
|
|
16
|
|
|
|
|
|
|
} |
17
|
135
|
100
|
100
|
|
|
872
|
if (!$latest_expires or $key->expires > $latest_expires) { |
18
|
57
|
|
|
|
|
89
|
$latest_expires = $key->expires; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
} |
21
|
56
|
|
|
|
|
408
|
while (my ($site_id, $keys) = each %keys_by_site) { |
22
|
54
|
|
|
|
|
211
|
$keys_by_site{$site_id} = [sort { $a->activates <=> $b->activates } @$keys]; |
|
127
|
|
|
|
|
778
|
|
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
bless { |
25
|
56
|
|
|
|
|
446
|
keys => \%keys, |
26
|
|
|
|
|
|
|
keys_by_site => \%keys_by_site, |
27
|
|
|
|
|
|
|
latest_expires => $latest_expires, |
28
|
|
|
|
|
|
|
}, $class; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub get { |
32
|
50
|
|
|
50
|
0
|
80
|
my ($self, $id) = @_; |
33
|
50
|
|
|
|
|
114
|
$self->{keys}->{$id}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub is_valid { |
37
|
40
|
|
|
40
|
0
|
362
|
my ($self, $now) = @_; |
38
|
40
|
|
66
|
|
|
64
|
$now //= UID2::Client::Timestamp->now; |
39
|
40
|
|
|
|
|
89
|
$self->{latest_expires} > $now->get_epoch_second; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub get_active_site_key { |
43
|
19
|
|
|
19
|
0
|
34
|
my ($self, $site_id, $now) = @_; |
44
|
19
|
100
|
|
|
|
42
|
my $keys = $self->{keys_by_site}->{$site_id} or return; |
45
|
18
|
|
66
|
|
|
33
|
$now //= UID2::Client::Timestamp->now; |
46
|
18
|
|
|
|
|
31
|
my $second = $now->get_epoch_second; |
47
|
18
|
|
|
36
|
|
150
|
my $i = upper_bound { $_->activates <=> $second } @$keys; |
|
36
|
|
|
|
|
137
|
|
48
|
18
|
|
|
|
|
133
|
while ($i > 0) { |
49
|
18
|
|
|
|
|
35
|
$i--; |
50
|
18
|
100
|
|
|
|
38
|
return $keys->[$i] if $keys->[$i]->is_active($now); |
51
|
|
|
|
|
|
|
} |
52
|
4
|
|
|
|
|
20
|
return; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
1; |
56
|
|
|
|
|
|
|
__END__ |