| 1 | |||
| 2 | package Net::Simplify::Jws; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Net::Simplify::Jws - Simplify Commerce module for JWS encoding and decoding. | ||
| 7 | |||
| 8 | =head1 SEE ALSO | ||
| 9 | |||
| 10 | L<Net::Simplify>, L<http://www.simplify.com> | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | 1.6.0 | ||
| 15 | |||
| 16 | =head1 LICENSE | ||
| 17 | |||
| 18 | Copyright (c) 2013 - 2022 MasterCard International Incorporated | ||
| 19 | All rights reserved. | ||
| 20 | |||
| 21 | Redistribution and use in source and binary forms, with or without modification, are | ||
| 22 | permitted provided that the following conditions are met: | ||
| 23 | |||
| 24 | Redistributions of source code must retain the above copyright notice, this list of | ||
| 25 | conditions and the following disclaimer. | ||
| 26 | Redistributions in binary form must reproduce the above copyright notice, this list of | ||
| 27 | conditions and the following disclaimer in the documentation and/or other materials | ||
| 28 | provided with the distribution. | ||
| 29 | Neither the name of the MasterCard International Incorporated nor the names of its | ||
| 30 | contributors may be used to endorse or promote products derived from this software | ||
| 31 | without specific prior written permission. | ||
| 32 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | ||
| 33 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| 34 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
| 35 | SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 36 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
| 37 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
| 38 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
| 39 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| 40 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 41 | SUCH DAMAGE. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | use 5.006; | ||
| 46 | use strict; | ||
| 47 | use warnings FATAL => 'all'; | ||
| 48 | |||
| 49 | use Time::HiRes qw(gettimeofday); | ||
| 50 | use Math::Random::Secure qw(irand); | ||
| 51 | use JSON; | ||
| 52 | use MIME::Base64 qw(decode_base64 decode_base64url encode_base64url); | ||
| 53 | use Crypt::Mac::HMAC qw( hmac_b64u ); | ||
| 54 | use Carp; | ||
| 55 | |||
| 56 | our $JWS_NUM_HEADERS = 7; | ||
| 57 | our $JWS_ALGORITHM = 'HS256'; | ||
| 58 | our $JWS_TYPE = 'JWS'; | ||
| 59 | our $JWS_HDR_UNAME = 'uname'; | ||
| 60 | our $JWS_HDR_URI = 'api.simplifycommerce.com/uri'; | ||
| 61 | our $JWS_HDR_TIMESTAMP = 'api.simplifycommerce.com/timestamp'; | ||
| 62 | our $JWS_HDR_NONCE = 'api.simplifycommerce.com/nonce'; | ||
| 63 | our $JWS_HDR_TOKEN = 'api.simplifycommerce.com/token'; | ||
| 64 | our $JWS_TIMESTAMP_MAX_DIFF = 1000 * 60 * 5; # 5 minutes | ||
| 65 | |||
| 66 | |||
| 67 | sub encode { | ||
| 68 | my ($class, $url, $payload, $auth) = @_; | ||
| 69 | |||
| 70 | my $header = { | ||
| 71 | 'typ' => $JWS_TYPE, | ||
| 72 | 'alg' => $JWS_ALGORITHM, | ||
| 73 | 'kid' => $auth->public_key, | ||
| 74 | $JWS_HDR_URI => $url, | ||
| 75 | $JWS_HDR_TIMESTAMP => int(_now()), | ||
| 76 | $JWS_HDR_NONCE => irand() | ||
| 77 | }; | ||
| 78 | |||
| 79 | my $token = $auth->access_token; | ||
| 80 | if (defined $token) { | ||
| 81 | $$header{$JWS_HDR_TOKEN} = $token; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $p1 = encode_base64url(encode_json $header); | ||
| 85 | |||
| 86 | my $p2 = ""; | ||
| 87 | $p2 = encode_base64url($payload) if defined $payload; | ||
| 88 | |||
| 89 | my $msg = "${p1}.${p2}"; | ||
| 90 | my $sig = _sign($msg, $auth->private_key); | ||
| 91 | |||
| 92 | "${msg}.${sig}"; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub decode { | ||
| 96 | my ($class, $message, $url, $auth) = @_; | ||
| 97 | |||
| 98 | # Remove whitespace | ||
| 99 | $message =~ s/\s*//g; | ||
| 100 | my @parts = split(/\./, $message); | ||
| 101 | my $num_parts = @parts; | ||
| 102 | |||
| 103 | if ($num_parts != 3) { | ||
| 104 | croak(Net::Simplify::IllegalArgumentException->new("Invalid JWS message")); | ||
| 105 | } | ||
| 106 | |||
| 107 | my $header = decode_json(decode_base64url($parts[0])); | ||
| 108 | |||
| 109 | _verify_header($header, $url, $auth->public_key); | ||
| 110 | |||
| 111 | if (!_verify_sig($auth->private_key, @parts)) { | ||
| 112 | croak(Net::Simplify::AuthenticationException->new("JWS signature does not match")); | ||
| 113 | } | ||
| 114 | |||
| 115 | decode_json(decode_base64url($parts[1])); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub _verify_header { | ||
| 120 | my ($header, $url, $public_key) = @_; | ||
| 121 | |||
| 122 | my $n = keys %{$header}; | ||
| 123 | if ($n != $JWS_NUM_HEADERS) { | ||
| 124 | _auth_error("Incorrect number of JWS header parameters - found ${n} required ${JWS_NUM_HEADERS}"); | ||
| 125 | } | ||
| 126 | |||
| 127 | my $value = $header->{alg}; | ||
| 128 | if ($value ne $JWS_ALGORITHM) { | ||
| 129 | _auth_error("Incorrect algorithm - found ${value} required ${JWS_ALGORITHM}"); | ||
| 130 | } | ||
| 131 | |||
| 132 | $value = $header->{typ}; | ||
| 133 | if ($value ne $JWS_TYPE) { | ||
| 134 | _auth_error("Incorrect type - found ${value} required ${JWS_TYPE}"); | ||
| 135 | } | ||
| 136 | |||
| 137 | $value = $header->{kid}; | ||
| 138 | if (!defined $value) { | ||
| 139 | _auth_error("Missing Key ID"); | ||
| 140 | } | ||
| 141 | |||
| 142 | if ($value ne $public_key) { | ||
| 143 | if (Net::Simplify::SimplifyApi->is_live_key($public_key)) { | ||
| 144 | _auth_error("Invalid Key ID"); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | $value = $header->{$JWS_HDR_URI}; | ||
| 149 | if (!defined $value) { | ||
| 150 | _auth_error("Missing URI"); | ||
| 151 | } | ||
| 152 | |||
| 153 | if (defined $url && $url ne $value) { | ||
| 154 | _auth_error("Incorrect URL - found ${value} required ${url}"); | ||
| 155 | } | ||
| 156 | |||
| 157 | $value = $header->{$JWS_HDR_TIMESTAMP}; | ||
| 158 | if (!defined $value) { | ||
| 159 | _auth_error("Missing timestamp"); | ||
| 160 | } | ||
| 161 | |||
| 162 | if (!_verify_timestamp($value)) { | ||
| 163 | _auth_error("Invalid timestamp"); | ||
| 164 | } | ||
| 165 | |||
| 166 | $value = $header->{$JWS_HDR_NONCE}; | ||
| 167 | if (!defined $value) { | ||
| 168 | _auth_error("Missing nonce"); | ||
| 169 | } | ||
| 170 | |||
| 171 | $value = $header->{$JWS_HDR_UNAME}; | ||
| 172 | if (!defined $value) { | ||
| 173 | _auth_error("Missing username header"); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | sub _sign { | ||
| 178 | my ($msg, $private_key) = @_; | ||
| 179 | |||
| 180 | my $key = decode_base64($private_key); | ||
| 181 | |||
| 182 | hmac_b64u('SHA256', $key, $msg); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub _verify_sig { | ||
| 186 | my ($private_key, @parts) = @_; | ||
| 187 | |||
| 188 | $parts[2] eq _sign($parts[0] . '.' . $parts[1], $private_key); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub _verify_timestamp { | ||
| 192 | my ($timestamp) = @_; | ||
| 193 | |||
| 194 | abs($timestamp - _now()) < $JWS_TIMESTAMP_MAX_DIFF; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub _now { | ||
| 198 | gettimeofday() * 1000 | ||
| 199 | } | ||
| 200 | |||
| 201 | sub _auth_error { | ||
| 202 | my ($msg) = @_; | ||
| 203 | |||
| 204 | croak(Net::Simplify::AuthenticationException->new($msg)); | ||
| 205 | } |