@gravitywelluk/square-utils
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Gravitywell Square Utils

A library to assist in the implementation of Square

CI License Downloads Version


Verify and Validate an Event Notification

Because your notification URL is public and can be called by anyone, you must validate each event notification to confirm that it came from Square. A non-Square post can potentially compromise your application. All webhook notifications from Square include an x-square-signature header. The value of this header is an HMAC-SHA1 signature generated using your webhook signature key, the notification URL, and the raw body of the request. You can validate the webhook notification by generating the HMAC-SHA1 in your own code and comparing it to the signature of the event notification you received.

The following function generates an HMAC-SHA1 signature from your signature key, the notification URL, and the event notification body. You can then compare the result with the event notification's x-square-signature.

Usage

import { isFromSquare } from "@gravitywelluk/square-utils";

// The URL where event notifications are sent.
const NOTIFICATION_URL = 'https://example.com/webhook';

// The event notification subscription signature key (sigKey) defined in dev portal for app.
const SIG_KEY = '<SIGNATURE_KEY>';

export const handler = gatewayProxyHandler(async event => {

  const rawBody = event.body;
  const squareSignature = event.headers['x-square-signature'];
  const eventIsFromSquare = isFromSquare(SIG_KEY, NOTIFICATION_URL, squareSignature, rawBody);
})

Package Sidebar

Install

npm i @gravitywelluk/square-utils

Weekly Downloads

0

Version

2.0.0

License

MIT

Unpacked Size

6.85 kB

Total Files

7

Last publish

Collaborators

  • gravitywell.uk