Finance/Bank/Postbank_de/APIv1.pm

filter: Statementstmt 65.7% 75 / 114 Branchbran 25.0% 4 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.0% 22 / 25 Podpod 0.0% 0 / 6 totaltotal 62.7% 101 / 161 CC 13 SCAR 18.1
CC
13
Cov
61%
diagnoseCertificateError
30.0
login
6.0
configure_ua_ssl
4.8
CRAP 6.1
« HAL/Resource.pm ↑ Summary Finance/Bank/Postbank_de/APIv1/Account.pm »
1 package Finance::Bank::Postbank_de::APIv1;
2 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use JSON 'decode_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Filter::signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Mozilla::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use HTTP::CookieJar::LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use IO::Socket::SSL qw(SSL_VERIFY_PEER SSL_VERIFY_NONE);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use HAL::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Finance::Bank::Postbank_de::APIv1::Finanzstatus;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Finance::Bank::Postbank_de::APIv1::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Finance::Bank::Postbank_de::APIv1::Transaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Finance::Bank::Postbank_de::APIv1::Account;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Finance::Bank::Postbank_de::APIv1::Depot;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Finance::Bank::Postbank_de::APIv1::Position;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 10 our $VERSION = '0.57';
22  
23 =head1 NAME
24  
25 Finance::Bank::Postbank_de::APIv1 - Postbank connection
26  
27 =head1 SYNOPSIS
28  
29 my $api = Finance::Bank::Postbank_de::APIv1->new();
30 $api->configure_ua();
31 my $postbank = $api->login( 'Petra.Pfiffig', '11111' );
32  
33 =cut
34  
35 10 has diagnostics => (
36 is => 'ro',
37 default => undef,
38 );
39  
40 10 has logger => (
41 is => 'rw',
42 default => undef,
43 );
44  
45 has ua => (
46 is => 'ro',
47 9 ▶ default => sub( $self ) {
Subroutine __ANON__: called CC=2 SCAR=10.5
48 9 my $ua = WWW::Mechanize->new(
49 autocheck => 0,
50 keep_alive => 1,
51 cookie_jar => HTTP::CookieJar::LWP->new(),
52 );
53  
54 9 ▶ if( $self->diagnostics ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->diagnostics) 0 9 9
55 0 require LWP::ConsoleLogger::Easy;
56 0 my $logger = LWP::ConsoleLogger::Easy::debug_ua( $ua );
57 0 $logger->dump_content(0);
58 0 $logger->dump_text(0);
59 0 $self->logger($logger);
60 };
61 9 $ua
62 }
63 10 );
64  
65 10 has config => (
66 is => 'rw',
67 );
68  
69 has certificate_subject => (
70 is => 'ro',
71 9 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 +{
73 #/jurisdictionC=DE/jurisdictionST=Nordrhein-Westfalen/jurisdictionL=Bonn/businessCategory=Private Organization/serialNumber=HRB6793/C=DE/postalCode=53113/ST=Nordrhein-Westfalen/L=Bonn/street=Friedrich Ebert Allee 114 126/O=Deutsche Postbank AG/OU=PB Systems AG/CN=meine.postbank.de
74 #meine_postbank_de => qr{^/(?:\Q1.3.6.1.4.1.311.60.2.1.3\E|jurisdictionC|jurisdictionCountryName)=DE/(?:\Q1.3.6.1.4.1.311.60.2.1.2\E|jurisdictionST|jurisdictionStateOrProvinceName)=Nordrhein-Westfalen/(?:\Q1.3.6.1.4.1.311.60.2.1.1\E|jurisdictionL|jurisdictionLocalityName)=Bonn/businessCategory=Private Organization/serialNumber=HRB6793/C=DE/postalCode=53113/ST=Nordrhein-Westfalen/L=Bonn/street=Friedrich Ebert Allee 114 126/O=Deutsche Postbank AG/OU=PB Systems AG/CN=meine.postbank.de$},
75 9 api_public_postbank_de => qr{^/(?:\Q2.5.4.15\E|businessCategory)=Private Organization/(?:\Q1.3.6.1.4.1.311.60.2.1.3\E|jurisdictionC|jurisdictionCountryName)=DE/(?:\Q1.3.6.1.4.1.311.60.2.1.2\E|jurisdictionST|jurisdictionStateOrProvinceName)=Hessen/(?:\Q1.3.6.1.4.1.311.60.2.1.1\E|jurisdictionL|jurisdictionLocalityName)=Frankfurt am Main/serialNumber=HRB 47141/C=DE/ST=Nordrhein-Westfalen/L=Bonn/O=DB Privat- und Firmenkundenbank AG/OU=Postbank Systems AG/CN=bankapi-public.postbank.de$}
76 },
77 },
78 10 );
79  
80 0 ▶sub diagnoseCertificateError( $self, $error=$@ ) {
Subroutine diagnoseCertificateError: not called CC=5 SCAR=34.0
Pod undocumented
81 0 ▶ my( $found, $re ) = ($error =~ m#'(.+?)' !~ /\Q(?^:\E(.+?)/ at #)
Branch: 1 branch
BranchTrueFalseTotal
() = $error =~ m['(.+?)' !~ /\(\?\^\:(.+?)/ at ] or die "$error" 0 0 0
82 or die "$error"; # reraise
83 0 warn $found;
84 0 warn $re;
85 0 my @found_parts = split m!/!, $found;
86 0 my @re_parts = split m!/!, $re;
87  
88 0 for my $i (0..$#re_parts ) {
89 0 ▶ if( $found_parts[ $i ] =~ $re_parts[ $i ]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_parts[$i] =~ /$re_parts[$i]/) { } 0 0 0
90 0 warn "'$found_parts[ $i ]' =~ /$re_parts[ $i ]/, OK\n";
91 } else {
92 0 warn "'$found_parts[ $i ]' !~ /$re_parts[ $i ]/, not OK\n";
93 };
94 };
95 0 die "Certificate mismatch";
96 }
97  
98 9 ▶sub fetch_config( $self ) {
Subroutine fetch_config: called CC=3 SCAR=14.7
Pod undocumented
99 # Do an initial fetch to set up cookies
100 9 my $ua = $self->ua;
101 9 $self->configure_ua_ssl;
102 #my $re = join "|", values %{ $self->certificate_subject };
103 #$ua->add_header(
104 # "If-SSL-Cert-Subject" => qr/$re/,
105 #);
106 9 eval {
107 9 $ua->get('https://meine.postbank.de');
108 9 $ua->get('https://meine.postbank.de/configuration.json');
109 };
110 9 ▶ if( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 9 9
111 0 $self->diagnoseCertificateError( "$@ ");
112 };
113 9 my $config = decode_json( $ua->content );
114 0 ▶ if( ! exists $config->{ 'iob5-base' }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{"iob5-base"}) 0 0 0
115 0 require Data::Dumper;
116 0 croak "Invalid config retrieved: " . Data::Dumper::Dumper($config);
117 };
118 0 $self->config( $config );
119 0 $config
120 }
121  
122 9 ▶sub configure_ua_ssl( $self, $ua=$self->ua ) {
Subroutine configure_ua_ssl: called CC=4 SCAR=15.6
Pod undocumented
123 # OpenSSL 1.0.1 doesn't properly scan the certificate chain as supplied
124 # by Mozilla::CA, so we only verify the certificate directly there:
125 9 my @verify;
126  
127 9 ▶ if( IO::Socket::SSL->VERSION <= 1.990 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('IO::Socket::SSL'->VERSION <= 1.99) { } 0 9 9
elsif (&Net::SSLeay::SSLeay() <= 268439743) { } 0 9 9
128 # No OCSP support
129 0 @verify = ();
130 } elsif( Net::SSLeay::SSLeay() <= 0x100010bf ) { # 1.0.1k
131 0 @verify = (
132 SSL_fingerprint => 'sha256$99043D1F58197BDDFAEA3F914A8693588B067D7DC85BF532D7B773A9ED98F915',
133 SSL_ocsp_mode => IO::Socket::SSL::SSL_OCSP_NO_STAPLE(),
134 );
135 } else {
136 # We need no special additional options to verify the certificate chain
137 9 @verify = (
138 SSL_ocsp_mode => IO::Socket::SSL::SSL_OCSP_FULL_CHAIN(),
139 );
140 };
141 9 $ua->ssl_opts(
142 SSL_ca_file => Mozilla::CA::SSL_ca_file(),
143 SSL_verify_mode => SSL_VERIFY_PEER(),
144 @verify,
145 #SSL_verify_callback => sub {
146 #use Data::Dumper;
147 #warn Dumper \@_;
148 #return 1;
149 #},
150 );
151 };
152  
153 9 ▶sub configure_ua( $self, $config = $self->fetch_config ) {
Subroutine configure_ua: called CC=2 SCAR=11.6
Pod undocumented
154 0 my $ua = $self->ua;
155  
156 $ua->add_header(
157 'api-key' => $config->{'iob5-base'}->{apiKey},
158 'device-signature' => $config->{'iob5-base'}->{apiKey},
159 accept => ['application/hal+json', '*/*'],
160 keep_alive => 1,
161 # / businessCategory =Private Organization/ jurisdictionC =DE/ jurisdictionST =Hessen/ jurisdictionL =Frankfurt am Main/serialNumber=HRB 47141/C=DE/ST=Nordrhein-Westfalen/L=Bonn/O=DB Privat- und Firmenkundenbank AG/OU=Postbank Systems AG/CN=(?:banking|bankapi-public).postbank.de
162 "If-SSL-Cert-Subject" => $self->certificate_subject->{ api_public_postbank_de },
163 0 );
164 };
165  
166 0 ▶sub login_url( $self ) {
Subroutine login_url: not called CC=1 SCAR=6.9
Pod undocumented
167 0 my $config = $self->config;
168 0 my $loginUrl = $config->{'iob5-base'}->{loginUrl};
169 0 $loginUrl =~ s!%(\w+)%!$config->{'iob5-base'}->{$1}!ge;
170 0 $loginUrl
171 }
172  
173 0 ▶sub login( $self, $username, $password ) {
Subroutine login: not called CC=2 SCAR=17.9
Pod undocumented
174 0 my $ua = $self->ua;
175 0 my $loginUrl = $self->login_url();
176  
177 0 local $ua->{autocheck};
178 0 my $r = $ua->post(
179 $loginUrl,
180 content => sprintf 'username=%s&password=%s', $username, $password
181 );
182 0 ▶ if( ! $r->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->is_success) 0 0 0
183 0 die sprintf "HTTP Error: %03d %s", $r->code, $r->message;
184 };
185 my $postbank = HAL::Resource->new(
186 ua => $ua,
187 0 %{ decode_json($ua->content)}
188 );
189  
190 };
191  
192 10 1;
193  
194 =head1 RESOURCE HIERARCHY
195  
196 This is the hierarchy of the resources in the API:
197  
198 APIv1
199 Finanzstatus
200 BusinessPartner
201 Account
202 Transaction
203 Message
204 Attachment
205 Depot
206  
207 =head1 AUTHOR
208  
209 Max Maischein, E<lt>corion@cpan.orgE<gt>
210  
211 =head1 SEE ALSO
212  
213 L<perl>, L<WWW::Mechanize>.
214  
215 =head1 REPOSITORY
216  
217 The public repository of this module is
218 L<https://github.com/Corion/Finance-Bank-Postbank_de>.
219  
220 =head1 SUPPORT
221  
222 The public support forum of this module is
223 L<https://perlmonks.org/>.
224  
225 =head1 BUG TRACKER
226  
227 Please report bugs in this module via the RT CPAN bug queue at
228 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Finance-Bank-Postbank_de>
229 or via mail to L<finance-bank-postbank_de-Bugs@rt.cpan.org>.
230  
231 =head1 COPYRIGHT (c)
232  
233 Copyright 2003-2019 by Max Maischein C<corion@cpan.org>.
234  
235 =head1 LICENSE
236  
237 This module is released under the same terms as Perl itself.
238  
239 =cut
240  
« HAL/Resource.pm ↑ Summary Finance/Bank/Postbank_de/APIv1/Account.pm »