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 332 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 329 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 323 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 323 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 323 our $VERSION = '2.23.0';
9  
10 323 extends 'Lemonldap::NG::Common::Module';
11  
12 has timeout => (
13 is => 'rw',
14 lazy => 1,
15 default => sub {
16 2 ▶ $_[0]->{conf}->{timeout};
Subroutine __ANON__: called CC=1 SCAR=0.0
17 }
18 323 );
19  
20 has cache => (
21 is => 'rw',
22 lazy => 1,
23 default => sub {
24 65 ▶ my $c = $_[0]->{conf};
Subroutine __ANON__: called CC=4 SCAR=15.0
25 65 ▶ if ( !$c->{tokenUseGlobalStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->{"tokenUseGlobalStorage"}) 64 1 65
26 64 ▶ if ( $c->{localSessionStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'localSessionStorage'}) { } 64 0 64
27 64 ▶ eval "use $c->{localSessionStorage}";
Subroutine BEGIN: called CC=1 SCAR=0.0
28 64 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 64 64
29 0 $_[0]->{p}->logger->error($@);
30 0 return undef;
31 }
32 return $c->{localSessionStorage}
33 64 ->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 323 );
44  
45 sub init {
46 933 ▶ return 1;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
47 }
48  
49 sub createToken {
50 173 ▶ my ( $self, $infos ) = @_;
Subroutine createToken: called CC=4 SCAR=13.9
Pod undocumented
51  
52 # Set _utime for session autoremove
53 # Use default session timeout and register session timeout to compute it
54 173 my $time = time();
55  
56 # Set _utime to remove token after $self->timeout
57 173 $infos->{_utime} = $time + ( $self->timeout - $self->conf->{timeout} );
58  
59 # Store expiration timestamp for further use
60 173 $infos->{tokenTimeoutTimestamp} = $time + $self->timeout;
61  
62 # Store start timestamp for further use
63 173 $infos->{tokenSessionStartTimestamp} = $time;
64  
65 # Store type
66 173 ▶ $infos->{_type} ||= "token";
Condition100%2/2
$infos->{"_type"} ||= "token"
l!l
12350
MC/DC100%1/1
$infos->{"_type"} ||= "token"
$infos->{"_type"}
Truth table100%2/2
A
A= $infos->{"_type"}
Aresult
11
00
67  
68 173 ▶ if ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 164 9 173
69 my $id =
70 164 $infos->{_utime} . '_' . unpack( 'S', Crypt::URandom::urandom(2) );
71  
72 # Dereference $infos
73 164 my %h = %$infos;
74 164 $self->cache->set( $id, to_json( \%h ), $self->timeout . ' s' );
75 164 $self->logger->debug("Token $id created");
76 164 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 sub getToken {
95 37 ▶ my ( $self, $id, $keep ) = @_;
Subroutine getToken: called CC=8 SCAR=21.2
Pod undocumented
96 37 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 37 37
97 0 $self->logger->error('getToken called without id');
98 0 return undef;
99 }
100 37 $self->logger->debug("Trying to load token $id");
101  
102 37 ▶ if ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 25 12 37
103 25 my $data;
104 25 my @t = split /_/, $id;
105 25 ▶ if ( $t[0] > time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t[0] > &CORE::GLOBAL::time()) 0 25 25
106 0 $self->logger->notice("Expired token $id");
107 0 $self->cache->remove($id);
108 0 return undef;
109 }
110 25 ▶ unless ( $data = $self->cache->get($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data = $self->cache->get($id)) 5 20 25
111 5 $self->logger->notice("Bad (or expired) token $id");
112 5 return undef;
113 }
114 20 ▶ $self->cache->remove($id) unless ($keep);
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 15 5 20
115 20 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 sub updateToken {
132 8 ▶ my ( $self, $id, $k, $v ) = @_;
Subroutine updateToken: called CC=4 SCAR=13.9
Pod undocumented
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 sub setToken {
163 15 ▶ my ( $self, $req, $info ) = @_;
Subroutine setToken: called CC=1 SCAR=0.0
Pod undocumented
164 15 $self->logger->debug('Prepare token');
165 15 $req->token( $self->createToken($info) );
166 }
167  
168 323 1;
« blib/lib/Lemonldap/NG/Portal/UserDB/Choice.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/Choice.pm »