blib/lib/Lemonldap/NG/Portal/Lib/OpenID/SREG.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 190 CC 25 SCAR 61.2
CC
25
Cov
0%
sregHook
552.0
sregfilter
12.0
BEGIN
2.0
CRAP 454.9
1 ## @file
2 # OpenID SREG extension for Lemonldap::NG::Portal::Issuer::OpenID class
3  
4 ## @class
5 # OpenID SREG extension for Lemonldap::NG::Portal::Issuer::OpenID class
6  
7 package Lemonldap::NG::Portal::Lib::OpenID::SREG;
8  
9 use strict;
10 use Lemonldap::NG::Common::Regexp;
11  
12 our $VERSION = '2.0.0';
13  
14 ## @method protected hash sregHook(hash prm)
15 # Hook called to add SREG parameters to the OpenID response
16 # @return Hash containing wanted parameters
17 sub sregHook {
18 my ( $self, $req, $u, $trust_root, $is_id, $is_trusted, $prm ) = @_;
19 my ( @req, @opt );
20  
21 # Refuse federation if rejected by user
22 if ( $req->param('confirm') and $req->param('confirm') == -1 ) {
23 my %h;
24 $h{$_} = undef
25 foreach (
26 qw(fullname nickname language postcode timezone country gender email dob)
27 );
28 $self->p->updatePersistentSession( $req, \%h );
29 return 0;
30 }
31  
32 # If identity is not trusted, does nothing
33 return ( 0, $prm ) unless ( $is_id and $is_trusted );
34  
35 $self->logger->debug("SREG start");
36  
37 my $accepted = 1;
38  
39 # Check all parameters
40 my @pol;
41 while ( my ( $k, $v ) = each %$prm ) {
42  
43 # Store policy if provided
44 if ( $k eq 'policy_url' ) {
45 if ( $v =~ Lemonldap::NG::Common::Regexp::HTTP_URI ) {
46 push @pol, { url => $v };
47  
48 # Question: is it important to notify policy changes ?
49 # if yes, uncomment this
50 #my $p =
51 # $req->{sessionInfo}->{"_openidTrust$trust_root\_Policy"};
52 #$accepted = 0 unless ( $p and $p eq $v );
53 }
54 else {
55 $self->logger->error("Bad policy url");
56 }
57 }
58  
59 # Parse required attributes
60 elsif ( $k eq 'required' ) {
61 $self->logger->debug("Required attr $v");
62 push @req, split( /,/, $v );
63 }
64  
65 # Parse optional attributes
66 elsif ( $k eq 'optional' ) {
67 $self->logger->debug("Optional attr $v");
68 push @opt,
69 grep { defined $self->conf->{"openIdSreg_$trust_root$_"} }
70 split( /,/, $v );
71 }
72 else {
73 $self->logger->error("Unknown OpenID SREG request $k");
74 }
75 }
76 $req->data->{_openIdTrustExtMsg} .= $self->loadTemplate(
77 $req,
78 'openIdPol',
79 params => {
80 policies => \@pol,
81 }
82 ) if (@pol);
83  
84 # Check if required keys are valid SREG requests
85 # Question: reject bad SREG request ? Not done yet
86 @req = sregfilter( $self, @req );
87 @opt = sregfilter( $self, @opt );
88  
89 # Return if nothing is asked
90 return ( 1, {} ) unless ( @req or @opt );
91  
92 # If a required data is not available, returns nothing
93 foreach my $k (@req) {
94 unless ( $self->conf->{"openIdSreg_$k"} ) {
95 $self->logger->notice(
96 "Parameter $k is required by $trust_root but not defined in configuration"
97 );
98  
99 $req->info(
100 $self->loadTemplate(
101 $req, 'simpleInfo',
102 params => { trspan => "openidRpns,$k" }
103 )
104 );
105 return ( 0, {} );
106 }
107 }
108  
109 # Now set data
110 my ( %r, %msg, %ag, %toStore );
111  
112 # Requested parameters: check if already agreed or confirm is set
113 foreach my $k (@req) {
114 my $agree = $req->{sessionInfo}->{"_openidTrust$trust_root\_$k"};
115 if ($accepted) {
116 unless ( $req->param('confirm') or $agree ) {
117 $accepted = 0;
118 }
119 elsif ( !$agree ) {
120 $toStore{"_openidTrust$trust_root\_$k"} = 1;
121 }
122 }
123 my $tmp = $self->conf->{"openIdSreg_$k"};
124 $tmp =~ s/^\$//;
125 $msg{req}->{$k} = $r{$k} =
126 $req->{sessionInfo}->{ $self->{"openIdSreg_$k"} } || '';
127 }
128  
129 # Optional parameters:
130 foreach my $k (@opt) {
131 my $tmp = $self->conf->{"openIdSreg_$k"};
132 $tmp =~ s/^\$//;
133 my $agree = $req->{sessionInfo}->{"_openidTrust$trust_root\_$k"};
134 if ($accepted) {
135  
136 # First, check if already accepted
137 unless ( $req->param('confirm') or defined($agree) ) {
138 $accepted = 0;
139 $r{$k} = $req->{sessionInfo}->{$tmp}
140 || '';
141 }
142  
143 # If confirmation is returned, check the value for this field
144 elsif ( $req->param('confirm') == 1 ) {
145 my $ck = 0;
146 if ( defined( $req->param("sreg_$k") ) ) {
147 $ck = ( $req->param("sreg_$k") eq 'OK' ) || 0;
148 }
149  
150 # Store the value returned
151 if ( !defined($agree) or $agree != $ck ) {
152 $toStore{"_openidTrust$trust_root\_$k"} = $ck;
153 $agree = $ck;
154 }
155 }
156 }
157  
158 $msg{opt}->{$k} = $req->{sessionInfo}->{$tmp} || '';
159  
160 # Store the value only if user agree it
161 if ($agree) {
162 $r{$k} = $msg{opt}->{$k};
163 $ag{$k} = 1;
164 }
165 elsif ( !defined($agree) ) {
166 $ag{$k} = 1;
167 }
168 else {
169 $ag{$k} = 0;
170 }
171 }
172 $self->p->updatePersistentSession( $req, \%toStore ) if (%toStore);
173  
174 # Check if user has agreed request
175 if ($accepted) {
176 $self->userLogger->info(
177 $req->{sessionInfo}->{ $self->conf->{whatToTrace} }
178 . " has accepted OpenID SREG exchange with $trust_root" );
179 return ( 1, \%r );
180 }
181  
182 # else build message and return 0
183 else {
184 my ( @mopt, @mreq );
185  
186 # No choice for requested parameters: just an information
187 foreach my $k (@req) {
188 utf8::decode( $msg{req}->{$k} );
189 push @mreq, { k => $k, m => $msg{req}->{$k} };
190 }
191  
192 # For optional parameters: checkboxes are displayed
193 foreach my $k (@opt) {
194 utf8::decode( $msg{opt}->{$k} );
195 push @mopt,
196 {
197 k => $k,
198 m => $msg{opt}->{$k},
199 c => ( $ag{$k} ? 'checked' : '' )
200 };
201 }
202  
203 $req->data->{_openIdTrustExtMsg} .= $self->loadTemplate(
204 $req,
205 'openIdTrust',
206 params => {
207 required => \@mreq,
208 optional => \@mopt,
209 }
210 );
211  
212 $self->logger->debug('Building validation form');
213 return ( 0, $prm );
214 }
215 }
216  
217 ## @method private array sregfilter(array attr)
218 # Filter the arguments passed as parameters by checking their compliance with
219 # SREG.
220 # @return fitered data
221 sub sregfilter {
222 my ( $self, @attr ) = @_;
223 my ( @ret, @rej );
224  
225 # Browse attributes
226 foreach my $s (@attr) {
227 if ( $s =~
228 /^(?:(?:(?:full|nick)nam|languag|postcod|timezon)e|country|gender|email|dob)$/
229 )
230 {
231 push @ret, $s;
232 }
233 else {
234 $s =~ s/\W/\./sg;
235 push @rej, $s;
236 }
237 }
238  
239 # Warn if some parameters are rejected
240 if (@rej) {
241 $self->logger->warn( "Requested parameter(s) "
242 . join( ',', @rej )
243 . "is(are) not valid OpenID SREG parameter(s)" );
244 }
245  
246 # Return valid SREG parameters
247 return @ret;
248 }
249 1;