@goparrot/franchise-mcp-server
Version:
MCP Server for Franchise API
36 lines (35 loc) • 1.31 kB
JavaScript
import * as jwt from 'jsonwebtoken';
/**
* Decode and validate x-access-token
* @param token JWT token from x-access-token header
* @param requiredMerchantId Optional merchantId to validate against token
* @returns Decoded token data
* @throws Error if token is invalid or merchantId doesn't match
*/
export function decodeAndValidateToken(token, requiredMerchantId) {
try {
// Decode token without verification (we may not have secret)
const decoded = jwt.decode(token);
if (!decoded) {
throw new Error('Invalid token format');
}
// Check token expiration
const currentTime = Math.floor(Date.now() / 1000);
if (decoded.exp && decoded.exp < currentTime) {
throw new Error('Token has expired');
}
// If merchantId is required, validate it matches
if (requiredMerchantId) {
if (!decoded.merchantId) {
throw new Error('Token does not contain merchantId');
}
if (decoded.merchantId !== requiredMerchantId) {
throw new Error('Token merchantId does not match required merchantId');
}
}
return decoded;
}
catch (error) {
throw new Error(`Token validation failed: ${error.message}`);
}
}