ApiOperations
1 year ago
Apps
1 year ago
Billing
1 year ago
BillingPortal
1 year ago
Checkout
1 year ago
Climate
1 year ago
Entitlements
1 year ago
EventData
1 year ago
Events
1 year ago
Exception
1 year ago
FinancialConnections
1 year ago
Forwarding
1 year ago
HttpClient
1 year ago
Identity
1 year ago
Issuing
1 year ago
Radar
1 year ago
Reporting
1 year ago
Service
1 year ago
Sigma
1 year ago
Tax
1 year ago
Terminal
1 year ago
TestHelpers
1 year ago
Treasury
1 year ago
Util
1 year ago
V2
1 year ago
Account.php
1 year ago
AccountLink.php
1 year ago
AccountSession.php
1 year ago
ApiRequestor.php
1 year ago
ApiResource.php
2 years ago
ApiResponse.php
2 years ago
ApplePayDomain.php
1 year ago
Application.php
2 years ago
ApplicationFee.php
1 year ago
ApplicationFeeRefund.php
2 years ago
Balance.php
1 year ago
BalanceTransaction.php
1 year ago
BankAccount.php
1 year ago
BaseStripeClient.php
1 year ago
BaseStripeClientInterface.php
1 year ago
Capability.php
1 year ago
Card.php
1 year ago
CashBalance.php
2 years ago
Charge.php
1 year ago
Collection.php
1 year ago
ConfirmationToken.php
1 year ago
ConnectCollectionTransfer.php
2 years ago
CountrySpec.php
1 year ago
Coupon.php
1 year ago
CreditNote.php
1 year ago
CreditNoteLineItem.php
1 year ago
Customer.php
1 year ago
CustomerBalanceTransaction.php
2 years ago
CustomerCashBalanceTransaction.php
2 years ago
CustomerSession.php
1 year ago
Discount.php
1 year ago
Dispute.php
1 year ago
EphemeralKey.php
1 year ago
ErrorObject.php
1 year ago
Event.php
1 year ago
ExchangeRate.php
1 year ago
File.php
1 year ago
FileLink.php
1 year ago
FundingInstructions.php
2 years ago
Invoice.php
1 year ago
InvoiceItem.php
1 year ago
InvoiceLineItem.php
1 year ago
InvoiceRenderingTemplate.php
1 year ago
LineItem.php
1 year ago
LoginLink.php
1 year ago
Mandate.php
1 year ago
OAuth.php
2 years ago
OAuthErrorObject.php
1 year ago
PaymentIntent.php
1 year ago
PaymentLink.php
1 year ago
PaymentMethod.php
1 year ago
PaymentMethodConfiguration.php
1 year ago
PaymentMethodDomain.php
1 year ago
Payout.php
1 year ago
Person.php
1 year ago
Plan.php
1 year ago
Price.php
1 year ago
Product.php
1 year ago
ProductFeature.php
1 year ago
PromotionCode.php
1 year ago
Quote.php
1 year ago
Reason.php
1 year ago
RecipientTransfer.php
2 years ago
Refund.php
1 year ago
RelatedObject.php
1 year ago
RequestTelemetry.php
2 years ago
ReserveTransaction.php
2 years ago
Review.php
1 year ago
SearchResult.php
1 year ago
SetupAttempt.php
1 year ago
SetupIntent.php
1 year ago
ShippingRate.php
1 year ago
SingletonApiResource.php
2 years ago
Source.php
1 year ago
SourceMandateNotification.php
2 years ago
SourceTransaction.php
2 years ago
Stripe.php
1 year ago
StripeClient.php
1 year ago
StripeClientInterface.php
2 years ago
StripeObject.php
1 year ago
StripeStreamingClientInterface.php
2 years ago
Subscription.php
1 year ago
SubscriptionItem.php
1 year ago
SubscriptionSchedule.php
1 year ago
TaxCode.php
1 year ago
TaxDeductedAtSource.php
2 years ago
TaxId.php
1 year ago
TaxRate.php
1 year ago
ThinEvent.php
1 year ago
Token.php
1 year ago
Topup.php
1 year ago
Transfer.php
1 year ago
TransferReversal.php
1 year ago
UsageRecord.php
1 year ago
UsageRecordSummary.php
1 year ago
Webhook.php
1 year ago
WebhookEndpoint.php
1 year ago
WebhookSignature.php
2 years ago
WebhookSignature.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPForms\Vendor\Stripe; |
| 4 | |
| 5 | abstract class WebhookSignature |
| 6 | { |
| 7 | const EXPECTED_SCHEME = 'v1'; |
| 8 | /** |
| 9 | * Verifies the signature header sent by Stripe. Throws an |
| 10 | * Exception\SignatureVerificationException exception if the verification fails for |
| 11 | * any reason. |
| 12 | * |
| 13 | * @param string $payload the payload sent by Stripe |
| 14 | * @param string $header the contents of the signature header sent by |
| 15 | * Stripe |
| 16 | * @param string $secret secret used to generate the signature |
| 17 | * @param int $tolerance maximum difference allowed between the header's |
| 18 | * timestamp and the current time |
| 19 | * |
| 20 | * @throws Exception\SignatureVerificationException if the verification fails |
| 21 | * |
| 22 | * @return bool |
| 23 | */ |
| 24 | public static function verifyHeader($payload, $header, $secret, $tolerance = null) |
| 25 | { |
| 26 | // Extract timestamp and signatures from header |
| 27 | $timestamp = self::getTimestamp($header); |
| 28 | $signatures = self::getSignatures($header, self::EXPECTED_SCHEME); |
| 29 | if (-1 === $timestamp) { |
| 30 | throw Exception\SignatureVerificationException::factory('Unable to extract timestamp and signatures from header', $payload, $header); |
| 31 | } |
| 32 | if (empty($signatures)) { |
| 33 | throw Exception\SignatureVerificationException::factory('No signatures found with expected scheme', $payload, $header); |
| 34 | } |
| 35 | // Check if expected signature is found in list of signatures from |
| 36 | // header |
| 37 | $signedPayload = "{$timestamp}.{$payload}"; |
| 38 | $expectedSignature = self::computeSignature($signedPayload, $secret); |
| 39 | $signatureFound = \false; |
| 40 | foreach ($signatures as $signature) { |
| 41 | if (Util\Util::secureCompare($expectedSignature, $signature)) { |
| 42 | $signatureFound = \true; |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | if (!$signatureFound) { |
| 47 | throw Exception\SignatureVerificationException::factory('No signatures found matching the expected signature for payload', $payload, $header); |
| 48 | } |
| 49 | // Check if timestamp is within tolerance |
| 50 | if ($tolerance > 0 && \abs(\time() - $timestamp) > $tolerance) { |
| 51 | throw Exception\SignatureVerificationException::factory('Timestamp outside the tolerance zone', $payload, $header); |
| 52 | } |
| 53 | return \true; |
| 54 | } |
| 55 | /** |
| 56 | * Extracts the timestamp in a signature header. |
| 57 | * |
| 58 | * @param string $header the signature header |
| 59 | * |
| 60 | * @return int the timestamp contained in the header, or -1 if no valid |
| 61 | * timestamp is found |
| 62 | */ |
| 63 | private static function getTimestamp($header) |
| 64 | { |
| 65 | $items = \explode(',', $header); |
| 66 | foreach ($items as $item) { |
| 67 | $itemParts = \explode('=', $item, 2); |
| 68 | if ('t' === $itemParts[0]) { |
| 69 | if (!\is_numeric($itemParts[1])) { |
| 70 | return -1; |
| 71 | } |
| 72 | return (int) $itemParts[1]; |
| 73 | } |
| 74 | } |
| 75 | return -1; |
| 76 | } |
| 77 | /** |
| 78 | * Extracts the signatures matching a given scheme in a signature header. |
| 79 | * |
| 80 | * @param string $header the signature header |
| 81 | * @param string $scheme the signature scheme to look for |
| 82 | * |
| 83 | * @return array the list of signatures matching the provided scheme |
| 84 | */ |
| 85 | private static function getSignatures($header, $scheme) |
| 86 | { |
| 87 | $signatures = []; |
| 88 | $items = \explode(',', $header); |
| 89 | foreach ($items as $item) { |
| 90 | $itemParts = \explode('=', $item, 2); |
| 91 | if (\trim($itemParts[0]) === $scheme) { |
| 92 | $signatures[] = $itemParts[1]; |
| 93 | } |
| 94 | } |
| 95 | return $signatures; |
| 96 | } |
| 97 | /** |
| 98 | * Computes the signature for a given payload and secret. |
| 99 | * |
| 100 | * The current scheme used by Stripe ("v1") is HMAC/SHA-256. |
| 101 | * |
| 102 | * @param string $payload the payload to sign |
| 103 | * @param string $secret the secret used to generate the signature |
| 104 | * |
| 105 | * @return string the signature as a string |
| 106 | */ |
| 107 | private static function computeSignature($payload, $secret) |
| 108 | { |
| 109 | return \hash_hmac('sha256', $payload, $secret); |
| 110 | } |
| 111 | } |
| 112 |