The HMAC Authentication package allows you to add a layer of security to your Wix site's exposed endpoints, by implementing HMAC authentication checks on incoming requests and validating the integrity of their data. The authentication uses 1 or more secret keys defined by you, the host site, and shared with consumer sites that you authorize to make requests to your secured endpoints.
The package defines interactions between 2 types of Wix sites:
Host site: A site containing 1 or more endpoints exposed using the Wix HTTP Functions API. Note that there is only 1 host site for each implementation of the package.
Consumer site: A site that sends HTTP requests to the host site's exposed endpoints. There can be multiple consumer sites for each host site.
This package contains code for setting up the authentication service on a host site, and code for setting up and using the authentication service on consumer sites.
Note: To make things easier, we divided the package setup and content into 2 sections, one relevant for the host site, and the other for consumer sites.
The following setup instructions and package content are relevant for the host site only. For instructions about consumer sites, see the Consumer Sites section below.
Before using the package, set up the following:
hmac-authentication-secret-key
.validateAuth()
function in the code for any endpoint you want to secure. Call the function before executing any other logic.This package includes 1 backend file for use on the host site.
This file contains a function that validates the authenticity of requests received from a consumer site.
To use the function below in your code, import it with the following syntax:
import { validateAuth } from '@velo/wix-http-functions-hmac-authentication-backend';
validateAuth()
Validates incoming requests from the consumer site using HMAC authentication.
Syntax:
async function validateAuth(httpRequest: WixHttpFunctionRequest, [options: object]) : Promise<void>
Parameters:
hmac-authentication-secret-key
.validTimeDiff
to 10000
causes any requests more than 10 seconds old to fail. Note that setting this value adds an extra element of security against man-in-the-middle attacks.Returns:
A Promise that resolves to void
for valid requests.
Below is an example of an endpoint implementing validateAuth()
:
The following setup instructions and package content are relevant for consumer sites only. For instructions about the host site, see the Host Site section above.
Before using the package, set up the following:
hmac-authentication-secret-key
.invoke()
function from the auth.js file into your backend code files.invoke()
to send requests to the host site’s secured endpoints.This package includes 1 backend file for use on consumer sites.
This file contains a function that sends requests to the host site's secured endpoints.
To use the function below in your code, import it with the following syntax:
import { invoke } from '@velo/wix-http-functions-hmac-authentication-backend'
invoke()
Generates an HMAC signature using a secret key and optional data, and sends a request to the URL of the host site’s secured endpoint.
Syntax:
async function invoke(endpointUrl: string, [fetchOptions: WixFetchRequest], [hmacOptions: object]) : Promise<WixFetchResponse>
Parameters:
hmac-authentication-secret-key
.Returns:
A Promise that resolves to a WixFetchResponse object for valid requests.
Below is a an example implementation of invoke()
:
1.0 Initial version
securerequests, hmac, securehttp, httpfunctions