TrueLayer/Signer.pm

filter: Statementstmt 100% 31 / 31 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 97.5% 39 / 40 CC 2 SCAR 2.0
CC
2
Cov
100%
sign_request
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« TrueLayer/MerchantAccount.pm ↑ Summary TrueLayer/Mandate.pm »
1 package Business::TrueLayer::Signer;
2  
3 =head1 NAME
4  
5 Business::TrueLayer::Signer - Class to handle request signing TrueLayer
6 requests as described by https://github.com/TrueLayer/truelayer-signing/blob/main/request-signing-v2.md
7  
8 =head1 DESCRIPTION
9  
10 To use the TrueLayer Payments API v3, you need a public and private key
11 pair. You can generate these however you want, but we recommend OpenSSL
12 on Windows or LibreSSL on macOS or Linux. These methods are usually
13 available on these operating systems by default.
14  
15 To generate your private key, run the following command in your terminal.
16 The keys you generate will save to your current directory.
17  
18 openssl ecparam -genkey -name secp521r1 -noout -out ec512-private-key.pem
19  
20 Then, to generate your public key, run this command in your terminal.
21  
22 openssl ec -in ec512-private-key.pem -pubout -out ec512-public-key.pem
23  
24 You then need to upload the public key to the TrueLayer console.
25  
26 Having done that you can supply the path to your private key when using
27 the main L<Business::TrueLayer> module. You shouldn't need to do anything
28 else here, the distribution will handle signing when creating requests.
29  
30 =cut
31  
32 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use feature qw/ signatures postderef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 ▶no warnings qw/ experimental::signatures experimental::postderef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 9 ▶use Business::TrueLayer::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 9 ▶use Crypt::JWT qw/ encode_jwt /;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 9 has 'kid' => (
44 is => 'ro',
45 isa => 'Str',
46 required => 1,
47 );
48  
49 9 has 'private_key' => (
50 is => 'ro',
51 isa => 'EC512:PrivateKey',
52 coerce => 1,
53 required => 1,
54 );
55  
56 2 ▶sub sign_request (
Subroutine sign_request: called CC=2 SCAR=6.9
Pod undocumented
57 $self,
58 $http_verb,
59 $absolute_path,
60 $idempotency_key,
61 2 $serialized_http_request_body = undef,
62 ) {
63 2 $http_verb = uc( $http_verb );
64  
65 my $jws_token = encode_jwt(
66 alg => 'ES512',
67 key => $self->private_key,
68  
69 extra_headers => {
70 kid => $self->kid,
71 tl_version => "2",
72 tl_headers => "Idempotency-Key",
73 },
74  
75 payload => join(
76 2 "\n",grep { defined }
77 "$http_verb $absolute_path",
78 "Idempotency-Key: $idempotency_key",
79 $serialized_http_request_body
80 ),
81 );
82  
83 # we need to "detach" the payload from the JWS, so basically remove
84 # the middle bit
85 2 my ( $header,$payload,$signature) = split( /\./,$jws_token );
86 2 my $jws_token_with_detached_payload = join( '..',$header,$signature );
87  
88 return (
89 2 $jws_token_with_detached_payload,
90 $jws_token
91 );
92 }
93  
94 9 1;
95  
96 # vim: ts=4:sw=4:et
« TrueLayer/MerchantAccount.pm ↑ Summary TrueLayer/Mandate.pm »