Lite/SessionHandler/DH.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Lite/RelyingParty/Associator.pm ↑ Summary Lite/DH.pm »
1 package OpenID::Lite::SessionHandler::DH;
2  
3 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 extends 'OpenID::Lite::SessionHandler';
5  
6 0 ▶use OpenID::Lite::DH;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use MIME::Base64;
8  
9 has '_secret_length' => (
10 is => 'ro',
11 isa => 'Int',
12 required => 1,
13 );
14  
15 has '_dh' => (
16 is => 'ro',
17 lazy_build => 1,
18 );
19  
20 override 'set_request_params' => sub {
21 my ( $self, $service, $params ) = @_;
22 my $dh = $self->_dh;
23 my $dh_consumer_public = $dh->generate_public();
24 $params->set( dh_consumer_public => $dh_consumer_public );
25 $params->set( session_type => $self->_session_type );
26 return $params;
27 };
28  
29 override 'extract_secret' => sub {
30 my ( $self, $params ) = @_;
31 my $dh_server_public = $params->get('dh_server_public')
32 or return $self->ERROR(q{Missing parameter, "dh_server_public".});
33 my $enc_mac_key = $params->get('enc_mac_key')
34 or return $self->ERROR(q{Missing parameter, "enc_mac_key".});
35 my $dh = $self->_dh;
36 my $dh_sec = $dh->compute_public($dh_server_public);
37 my $secret
38 = MIME::Base64::decode_base64($enc_mac_key) ^ $self->_hash($dh_sec);
39  
40 my $secret_length = length $secret;
41 unless ( $secret_length == $self->_secret_length ) {
42 return $self->ERROR(
43 sprintf q{Secret length should be "%d", but got "%d"},
44 $self->_secret_length, $secret_length );
45 }
46 return $secret;
47 };
48  
49 override 'set_response_params' => sub {
50 my ( $self, $req_params, $res_params, $association ) = @_;
51  
52 my $dh_modulus = $req_params->get('dh_modulus');
53 my $dh_gen = $req_params->get('dh_gen');
54  
55 if ( ( !$dh_modulus && $dh_gen ) || ( $dh_modulus && !$dh_gen ) ) {
56 my $missing = $dh_modulus ? 'dh_gen' : 'dh_modulus';
57 return $self->ERROR( sprintf q{Missing parameter, "%s".}, $missing );
58 }
59  
60 my $dh = OpenID::Lite::DH->new;
61 if ( $dh_modulus || $dh_gen ) {
62 $dh->p($dh_modulus);
63 $dh->g($dh_gen);
64 $dh->generate_keys();
65 }
66  
67 my $dh_consumer_public = $req_params->get('dh_consumer_public')
68 or return $self->ERROR(q{Missing parameter, "dh_consumer_public".});
69 my $dh_sec = $dh->compute_public($dh_consumer_public);
70 my $enc_mac_key = MIME::Base64::encode_base64(
71 $association->secret ^ $self->_hash($dh_sec) );
72 $enc_mac_key =~ s/\s+//g;
73 $res_params->set( enc_mac_key => $enc_mac_key );
74  
75 my $dh_server_public = $dh->generate_public();
76  
77 $res_params->set( dh_server_public => $dh_server_public );
78  
79 $res_params->set( session_type => $self->_session_type );
80 };
81  
82 sub _hash {
83 my ( $self, $dh_sec ) = @_;
84 die "abstract method";
85 return $dh_sec;
86 }
87  
88 sub _build__dh {
89 my $self = shift;
90 my $dh = OpenID::Lite::DH->new;
91 return $dh;
92 }
93  
94 no Any::Moose;
95 __PACKAGE__->meta->make_immutable;
96 1;
97  
« Lite/RelyingParty/Associator.pm ↑ Summary Lite/DH.pm »