| 1 | package Business::TrueLayer::Webhook; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | Business::TrueLayer::Webhook | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | my $Webhook = Business::TrueLayer::Webhook->new({ | ||||||||||||||
| 10 | jwt => $jwt, | ||||||||||||||
| 11 | jwks => $jwkset, # optional | ||||||||||||||
| 12 | }); | ||||||||||||||
| 13 | |||||||||||||||
| 14 | |||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||
| 16 | |||||||||||||||
| 17 | A class for TrueLayer webhooks | ||||||||||||||
| 18 | |||||||||||||||
| 19 | For more details see the TreuLayer API documentation specific to webhooks: | ||||||||||||||
| 20 | https://docs.truelayer.com/docs/mandate-webhooks | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =cut | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 1 | ▶ | use feature qw/ signatures postderef /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 1 | extends 'Business::TrueLayer::Request'; | |||||||||||||
| 30 | 1 | ▶ | no warnings qw/ experimental::signatures experimental::postderef /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | 1 | ▶ | use Business::TrueLayer::Webhook::Mandate; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | 1 | ▶ | use JSON; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 1 | ▶ | use Mojo::JWT; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 1 | ▶ | use Mojo::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | 1 | ▶ | use Carp qw/ croak /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | =head1 ATTRIBUTES | ||||||||||||||
| 42 | |||||||||||||||
| 43 | =head2 jwt | ||||||||||||||
| 44 | |||||||||||||||
| 45 | The JWT as sent in the webhook by TrueLayer - this is required in the object | ||||||||||||||
| 46 | constructor as its signature will be validated and the payload will be used | ||||||||||||||
| 47 | to populate more data on the object. | ||||||||||||||
| 48 | |||||||||||||||
| 49 | =head2 jwkset | ||||||||||||||
| 50 | |||||||||||||||
| 51 | A JWKS used to validate the signature of the JWT. This can be provided as an | ||||||||||||||
| 52 | array reference of keys, however if not provide the jku in the JWT will be used | ||||||||||||||
| 53 | to download the JWKS. | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =head2 jku_accept_list | ||||||||||||||
| 56 | |||||||||||||||
| 57 | A hashref of acceptable URLs that can provide the JWKS. Keyed by URL: | ||||||||||||||
| 58 | |||||||||||||||
| 59 | { | ||||||||||||||
| 60 | $url => 1, | ||||||||||||||
| 61 | $alternative_url => 1, | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | If the jku in the JWT is not amongst the keys of the jku_accept_list then an | ||||||||||||||
| 65 | exception will be thrown. | ||||||||||||||
| 66 | |||||||||||||||
| 67 | =cut | ||||||||||||||
| 68 | |||||||||||||||
| 69 | has jku_accept_list => ( | ||||||||||||||
| 70 | is => 'ro', | ||||||||||||||
| 71 | isa => 'HashRef', | ||||||||||||||
| 72 | required => 0, | ||||||||||||||
| 73 | 3 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 74 | return { | ||||||||||||||
| 75 | 3 | 'https://webhooks.truelayer-sandbox.com/.well-known/jwks' => 1, | |||||||||||||
| 76 | 'https://webhooks.truelayer.com/.well-known/jwks' => 1, | ||||||||||||||
| 77 | }; | ||||||||||||||
| 78 | }, | ||||||||||||||
| 79 | 1 | ); | |||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | has jwks => ( | |||||||||||||
| 82 | is => 'ro', | ||||||||||||||
| 83 | isa => 'ArrayRef', | ||||||||||||||
| 84 | ); | ||||||||||||||
| 85 | |||||||||||||||
| 86 | has jwt => ( | ||||||||||||||
| 87 | is => 'rw', | ||||||||||||||
| 88 | isa => 'Str', | ||||||||||||||
| 89 | required => 1, | ||||||||||||||
| 90 | 3 | ▶ | trigger => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 91 | 3 | my ( $self,$jwt ) = @_; | |||||||||||||
| 92 | |||||||||||||||
| 93 | # we need to peek into the JWT to get the jku to: | ||||||||||||||
| 94 | # a) check it is limited to those on our accept list | ||||||||||||||
| 95 | # b) use it to get the JWKS | ||||||||||||||
| 96 | # c) which is then use by Mojo::JWT to check the signature | ||||||||||||||
| 97 | 2 | ▶ | my $peek = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 98 | 2 | my ( $jwt_instance,$claims ) = @_; | |||||||||||||
| 99 | |||||||||||||||
| 100 | 2 | ▶ | if ( my $jku = $jwt_instance->header->{jku} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 2 | ▶ | $self->jku_accept_list->{ $jku } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | || croak( "$jku is not in the jku_accept_list" ); | ||||||||||||||
| 103 | |||||||||||||||
| 104 | # we're going to GET the jwks for every webhook? | ||||||||||||||
| 105 | # this feels all sorts of wrong... | ||||||||||||||
| 106 | 1 | my $jwkset = Mojo::UserAgent->new->get( $jku ) | |||||||||||||
| 107 | ->result->json; | ||||||||||||||
| 108 | |||||||||||||||
| 109 | # add the jkws to the instance to allow it to check the signature | ||||||||||||||
| 110 | 1 | $jwt_instance->add_jwkset( $jwkset ); | |||||||||||||
| 111 | |||||||||||||||
| 112 | # we need to recall _try_jwks to set the public key from the jwkset | ||||||||||||||
| 113 | # (internally Mojo::JWT calls peek after the call to _try_jwks) | ||||||||||||||
| 114 | 1 | $jwt_instance->_try_jwks( | |||||||||||||
| 115 | $jwt_instance->algorithm, | ||||||||||||||
| 116 | $jwt_instance->header | ||||||||||||||
| 117 | ); | ||||||||||||||
| 118 | } | ||||||||||||||
| 119 | 3 | }; | |||||||||||||
| 120 | |||||||||||||||
| 121 | 3 | ▶ | $self->_payload( | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 122 | Mojo::JWT->new( ( $self->jwks | ||||||||||||||
| 123 | ? ( jwks => $self->jwks ) | ||||||||||||||
| 124 | : () | ||||||||||||||
| 125 | ) )->decode( | ||||||||||||||
| 126 | $jwt, | ||||||||||||||
| 127 | # if jwks is set we can bypass the peek that grabs it | ||||||||||||||
| 128 | ( $self->jwks ? () : ( $peek ) ) | ||||||||||||||
| 129 | ) | ||||||||||||||
| 130 | ); | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 2 | return $jwt; | |||||||||||||
| 133 | }, | ||||||||||||||
| 134 | 1 | ); | |||||||||||||
| 135 | |||||||||||||||
| 136 | 1 | has _payload => ( | |||||||||||||
| 137 | is => 'rw', | ||||||||||||||
| 138 | isa => 'HashRef', | ||||||||||||||
| 139 | ); | ||||||||||||||
| 140 | |||||||||||||||
| 141 | =head1 Operations on a webhook | ||||||||||||||
| 142 | |||||||||||||||
| 143 | =head2 resource | ||||||||||||||
| 144 | |||||||||||||||
| 145 | Returns an object (Business::TrueLayer::Webhook::Payment, ...::Mandate, etc) that | ||||||||||||||
| 146 | the webhook is referring to allowing you to do things with it or update your own | ||||||||||||||
| 147 | data: | ||||||||||||||
| 148 | |||||||||||||||
| 149 | if ( $Webhook->is_payment ) { | ||||||||||||||
| 150 | my $Payment = $Webhook->resource; | ||||||||||||||
| 151 | ... | ||||||||||||||
| 152 | } elsif ( $Webhook->is_mandate ) { | ||||||||||||||
| 153 | my $Mandate = $Webhook->resource; | ||||||||||||||
| 154 | ... | ||||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | =cut | ||||||||||||||
| 158 | |||||||||||||||
| 159 | 1 | ▶ | sub resource { | ||||||||||||
|
Subroutine
resource: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 160 | 1 | my ( $self ) = @_; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 1 | my $payload = $self->_payload; | |||||||||||||
| 163 | |||||||||||||||
| 164 | 1 | ▶ | if ( $self->is_mandate ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | |||||||||||||||
| 166 | 1 | my $status = $payload->{type} =~ s/mandate_//r; | |||||||||||||
| 167 | |||||||||||||||
| 168 | return Business::TrueLayer::Webhook::Mandate->new({ | ||||||||||||||
| 169 | id => $payload->{mandate_id}, | ||||||||||||||
| 170 | 1 | status => $status, | |||||||||||||
| 171 | }); | ||||||||||||||
| 172 | } | ||||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | =head2 is_payment | ||||||||||||||
| 176 | |||||||||||||||
| 177 | =head2 is_mandate | ||||||||||||||
| 178 | |||||||||||||||
| 179 | Shortcut methods to get the type of data in the webhook, and thus the type of | ||||||||||||||
| 180 | objects that will be returned by the call to ->resources | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =cut | ||||||||||||||
| 183 | |||||||||||||||
| 184 | 1 | ▶ | sub is_payment { shift->_is_type( 'payment' ) } | ||||||||||||
|
Subroutine
is_payment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 185 | 2 | ▶ | sub is_mandate { shift->_is_type( 'mandate' ) } | ||||||||||||
|
Subroutine
is_mandate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 186 | |||||||||||||||
| 187 | 3 | ▶ | sub _is_type ( $self,$expected_type ) { | ||||||||||||
|
Subroutine
_is_type: called CC=2 SCAR=6.9
| |||||||||||||||
| 188 | |||||||||||||||
| 189 | 3 | my $type = $self->_payload->{type}; | |||||||||||||
| 190 | 3 | ▶ | return $type =~ /^${expected_type}_/ ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 191 | } | ||||||||||||||
| 192 | |||||||||||||||
| 193 | =head1 SEE ALSO | ||||||||||||||
| 194 | |||||||||||||||
| 195 | L<Business::TrueLayer::Webhook::Payment> | ||||||||||||||
| 196 | |||||||||||||||
| 197 | L<Business::TrueLayer::Webhook::Mandate> | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =cut | ||||||||||||||
| 200 | |||||||||||||||
| 201 | 1 | 1; | |||||||||||||
| 202 | |||||||||||||||
| 203 | # vim: ts=4:sw=4:et | ||||||||||||||