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 14.4
CC
17
Cov
87%
getToken
8.4
__ANON__
4.5
createToken
4.0
CRAP 4.2
« blib/lib/Lemonldap/NG/Portal/Lib/Okta.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/UserDB/Choice.pm »
1 package Lemonldap::NG::Portal::Lib::OneTimeToken;
2  
3 314 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 311 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 311 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 311 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 311 our $VERSION = '2.22.0';
9  
10 311 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 311 );
19  
20 has cache => (
21 is => 'rw',
22 lazy => 1,
23 default => sub {
24 42 ▶ my $c = $_[0]->{conf};
Subroutine __ANON__: called CC=4 SCAR=15.0
25 42 ▶ if ( !$c->{tokenUseGlobalStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->{"tokenUseGlobalStorage"}) 41 1 42
26 41 ▶ if ( $c->{localSessionStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'localSessionStorage'}) { } 41 0 41
27 41 ▶ eval "use $c->{localSessionStorage}";
Subroutine BEGIN: called CC=1 SCAR=0.0
28 41 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 41 41
29 0 $_[0]->{p}->logger->error($@);
30 0 return undef;
31 }
32 return $c->{localSessionStorage}
33 41 ->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 311 );
44  
45 sub init {
46 560 ▶ return 1;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
47 }
48  
49 sub createToken {
50 113 ▶ 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 113 my $time = time();
55  
56 # Set _utime to remove token after $self->timeout
57 113 $infos->{_utime} = $time + ( $self->timeout - $self->conf->{timeout} );
58  
59 # Store expiration timestamp for further use
60 113 $infos->{tokenTimeoutTimestamp} = $time + $self->timeout;
61  
62 # Store start timestamp for further use
63 113 $infos->{tokenSessionStartTimestamp} = $time;
64  
65 # Store type
66 113 ▶ $infos->{_type} ||= "token";
Condition100%2/2
$infos->{"_type"} ||= "token"
l!l
8924
MC/DC100%1/1
$infos->{"_type"} ||= "token"
$infos->{"_type"}
Truth table100%2/2
A
A= $infos->{"_type"}
Aresult
11
00
67  
68 113 ▶ if ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 104 9 113
69 my $id =
70 104 $infos->{_utime} . '_' . unpack( 'S', Crypt::URandom::urandom(2) );
71  
72 # Dereference $infos
73 104 my %h = %$infos;
74 104 $self->cache->set( $id, to_json( \%h ), $self->timeout . ' s' );
75 104 $self->logger->debug("Token $id created");
76 104 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 23 ▶ my ( $self, $id, $keep ) = @_;
Subroutine getToken: called CC=8 SCAR=21.2
Pod undocumented
96 23 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 23 23
97 0 $self->logger->error('getToken called without id');
98 0 return undef;
99 }
100 23 $self->logger->debug("Trying to load token $id");
101  
102 23 ▶ if ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 11 12 23
103 11 my $data;
104 11 my @t = split /_/, $id;
105 11 ▶ if ( $t[0] > time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t[0] > &CORE::GLOBAL::time()) 0 11 11
106 0 $self->logger->notice("Expired token $id");
107 0 $self->cache->remove($id);
108 0 return undef;
109 }
110 11 ▶ unless ( $data = $self->cache->get($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data = $self->cache->get($id)) 5 6 11
111 5 $self->logger->notice("Bad (or expired) token $id");
112 5 return undef;
113 }
114 6 ▶ $self->cache->remove($id) unless ($keep);
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 2 4 6
115 6 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 4 ▶ my ( $self, $id, $k, $v ) = @_;
Subroutine updateToken: called CC=4 SCAR=13.9
Pod undocumented
133 4 ▶ if ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 2 2 4
134 2 my $data;
135 2 ▶ unless ( $data = $self->cache->get($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data = $self->cache->get($id)) 1 1 2
136 1 $self->logger->notice("Bad (or expired) token $id");
137 1 return undef;
138 }
139 1 my $h = from_json( $data, { allow_nonref => 1 } );
140 1 $h->{$k} = $v;
141 1 $self->cache->set( $id, to_json($h), ($h->{tokenTimeoutTimestamp} - time) . ' s' );
142 1 return $id;
143 }
144 else {
145 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->p->getApacheSession($id, 'kind', 'TOKEN', 'info', {$k, $v})) { } 1 1 2
146 $self->p->getApacheSession(
147 $id,
148 kind => "TOKEN",
149 info => { $k => $v }
150 )
151 )
152 {
153 1 return $id;
154 }
155 else {
156 1 return undef;
157 }
158 }
159 }
160  
161 sub setToken {
162 1 ▶ my ( $self, $req, $info ) = @_;
Subroutine setToken: called CC=1 SCAR=0.0
Pod undocumented
163 1 $self->logger->debug('Prepare token');
164 1 $req->token( $self->createToken($info) );
165 }
166  
167 311 1;
« blib/lib/Lemonldap/NG/Portal/Lib/Okta.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/UserDB/Choice.pm »