blib/lib/Lemonldap/NG/Portal/Lib/OneTimeToken.pm

filter: Statementstmt 87.9% 80 / 91 Branchbran 83.3% 25 / 30 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 5 totaltotal 85.1% 120 / 141 CC 17 SCAR 13.7
CC
17
Cov
87%
getToken
8.4
__ANON__
4.5
createToken
4.0
CRAP 3.9
« blib/lib/Lemonldap/NG/Portal/UserDB/Choice.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/Choice.pm »
1 package Lemonldap::NG::Portal::Lib::OneTimeToken;
2  
3 340 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 336 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 329 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 329 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 329 our $VERSION = '2.23.0';
9  
10 329 extends 'Lemonldap::NG::Common::Module';
11  
12 has timeout => (
13 is => 'rw',
14 lazy => 1,
15 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 $_[0]->{conf}->{timeout};
17 }
18 329 );
19  
20 has cache => (
21 is => 'rw',
22 lazy => 1,
23 69 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=15.0
24 69 my $c = $_[0]->{conf};
25 69 ▶ if ( !$c->{tokenUseGlobalStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->{"tokenUseGlobalStorage"}) 68 1 69
26 68 ▶ if ( $c->{localSessionStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'localSessionStorage'}) { } 68 0 68
27 68 ▶ eval "use $c->{localSessionStorage}";
Subroutine BEGIN: called CC=1 SCAR=0.0
28 68 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 68 68
29 0 $_[0]->{p}->logger->error($@);
30 0 return undef;
31 }
32 return $c->{localSessionStorage}
33 68 ->new( $c->{localSessionStorageOptions} );
34 }
35 else {
36 $_[0]->{p}->logger->error(
37 0 'Local storage not defined, token will be stored into global storage'
38 );
39 0 return undef;
40 }
41 }
42 },
43 329 );
44  
45 946 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
46 946 return 1;
47 }
48  
49 182 ▶sub createToken {
Subroutine createToken: called CC=4 SCAR=13.9
Pod undocumented
50 182 my ( $self, $infos ) = @_;
51  
52 # Set _utime for session autoremove
53 # Use default session timeout and register session timeout to compute it
54 182 my $time = time();
55  
56 # Set _utime to remove token after $self->timeout
57 182 $infos->{_utime} = $time + ( $self->timeout - $self->conf->{timeout} );
58  
59 # Store expiration timestamp for further use
60 182 $infos->{tokenTimeoutTimestamp} = $time + $self->timeout;
61  
62 # Store start timestamp for further use
63 182 $infos->{tokenSessionStartTimestamp} = $time;
64  
65 # Store type
66 182 ▶ $infos->{_type} ||= "token";
Condition100%2/2
$infos->{"_type"} ||= "token"
l!l
13052
MC/DC100%1/1
$infos->{"_type"} ||= "token"
$infos->{"_type"}
Truth table100%2/2
A
A= $infos->{"_type"}
Aresult
11
00
67  
68 182 ▶ if ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 173 9 182
69 my $id =
70 173 $infos->{_utime} . '_' . unpack( 'S', Crypt::URandom::urandom(2) );
71  
72 # Dereference $infos
73 173 my %h = %$infos;
74 173 $self->cache->set( $id, to_json( \%h ), $self->timeout . ' s' );
75 173 $self->logger->debug("Token $id created");
76 173 return $id;
77 }
78 else {
79  
80 # Create a new session
81 9 my $tsession =
82 $self->p->getApacheSession( undef, info => $infos, kind => 'TOKEN' );
83 9 ▶ if ( $tsession->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tsession->{'id'}) { } 9 0 9
84 9 $self->logger->debug("Token $tsession->{id} created");
85 9 return $tsession->id;
86 }
87 else {
88 0 $self->logger->error("NO token created");
89 0 return undef;
90 }
91 }
92 }
93  
94 39 ▶sub getToken {
Subroutine getToken: called CC=8 SCAR=21.2
Pod undocumented
95 39 my ( $self, $id, $keep ) = @_;
96 39 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 39 39
97 0 $self->logger->error('getToken called without id');
98 0 return undef;
99 }
100 39 $self->logger->debug("Trying to load token $id");
101  
102 39 ▶ if ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 27 12 39
103 27 my $data;
104 27 my @t = split /_/, $id;
105 27 ▶ if ( $t[0] > time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t[0] > &CORE::GLOBAL::time()) 0 27 27
106 0 $self->logger->notice("Expired token $id");
107 0 $self->cache->remove($id);
108 0 return undef;
109 }
110 27 ▶ unless ( $data = $self->cache->get($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data = $self->cache->get($id)) 5 22 27
111 5 $self->logger->notice("Bad (or expired) token $id");
112 5 return undef;
113 }
114 22 ▶ $self->cache->remove($id) unless ($keep);
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 17 5 22
115 22 return from_json( $data, { allow_nonref => 1 } );
116 }
117 else {
118  
119 # Get token session
120 12 my $tsession = $self->p->getApacheSession( $id, kind => 'TOKEN' );
121 12 ▶ unless ($tsession) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tsession) 5 7 12
122 5 $self->logger->notice("Bad (or expired) token $id");
123 5 return undef;
124 }
125 7 my %h = %{ $tsession->{data} };
126 7 ▶ $tsession->remove unless ($keep);
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 3 4 7
127 7 return \%h;
128 }
129 }
130  
131 8 ▶sub updateToken {
Subroutine updateToken: called CC=4 SCAR=13.9
Pod undocumented
132 8 my ( $self, $id, $k, $v ) = @_;
133 8 ▶ if ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 6 2 8
134 6 my $data;
135 6 ▶ unless ( $data = $self->cache->get($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data = $self->cache->get($id)) 1 5 6
136 1 $self->logger->notice("Bad (or expired) token $id");
137 1 return undef;
138 }
139 5 my $h = from_json( $data, { allow_nonref => 1 } );
140 5 $h->{$k} = $v;
141 $self->cache->set( $id, to_json($h),
142 5 ( $h->{tokenTimeoutTimestamp} - time ) . ' s' );
143 5 return $id;
144 }
145 else {
146 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->p->getApacheSession($id, 'kind', 'TOKEN', 'info', {$k, $v})) { } 1 1 2
147 $self->p->getApacheSession(
148 $id,
149 kind => "TOKEN",
150 info => { $k => $v }
151 )
152 )
153 {
154 1 return $id;
155 }
156 else {
157 1 return undef;
158 }
159 }
160 }
161  
162 15 ▶sub setToken {
Subroutine setToken: called CC=1 SCAR=0.0
Pod undocumented
163 15 my ( $self, $req, $info ) = @_;
164 15 $self->logger->debug('Prepare token');
165 15 $req->token( $self->createToken($info) );
166 }
167  
168 329 1;
« blib/lib/Lemonldap/NG/Portal/UserDB/Choice.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/Choice.pm »