Request Signature Authentication
XyPriss provides a powerful request signature authentication system that allows you to secure your APIs using cryptographic signatures. This method ensures that only authorized clients can access your API, requests cannot be tampered with in transit, and replay attacks are prevented.
Quick Start
Enable request signature validation globally by providing a shared secret in your security configuration:
import { createServer } from "xypriss";
const server = createServer({
security: {
requestSignature: {
secret: "your-super-secret-api-key-12345",
debug: true, // Enable debug logging in development
},
},
});Client Implementation
Clients must include a cryptographic signature in the XP-Request-Sig header and a timestamp in the X-Timestamp header. The signature is an HMAC-SHA256 hash of the request method, URL, body, and timestamp.
Prepare Data String
METHOD + URL + BODY + TIMESTAMP.Compute HMAC
Send Request
import { Hash } from "xypriss-security";
const signRequest = (
url: string,
method: string,
secret: string,
body = "",
timestamp = Date.now(),
) => ({
signature: Hash.hmac(
secret,
`${method.toUpperCase()}${url}${body}${timestamp}`,
"sha256",
),
timestamp,
});
const { signature, timestamp } = signRequest(
"/api/data",
"POST",
"my-secret",
'{"key":"val"}',
);
console.log("Signature:", signature);
console.log("Timestamp:", timestamp);Configuration Options
Customize the validation behavior to fit your security requirements:
requestSignature: {
secret: "my-secret-key",
algorithm: "sha512", // sha256 or sha512
maxAge: 300000, // 5 minutes validity
clockSkew: 30000, // 30 seconds tolerance
headerName: "X-API-Signature", // Custom header
timestampHeaderName: "X-API-Timestamp",
includeBody: true,
includeQuery: true,
}Advanced Route Filtering
Apply signatures only to specific routes or exclude public endpoints:
security: {
routeConfig: {
requestSignature: {
includeRoutes: ["/api/webhooks/*", "/api/admin/*"],
excludeRoutes: ["/api/public/*"],
},
},
}Security Best Practices
Strong Secrets
Always use cryptographically secure random strings for your shared secrets. Never use weak passwords.
Use HTTPS
Signatures prevent tampering but not interception. Always use HTTPS in production to encrypt the headers.
maxAge + clockSkew.Implement fine-grained access control using IP whitelists and blacklists.
