twenty-mcp-server
Version:
Easy-to-install Model Context Protocol server for Twenty CRM. Try instantly with 'npx twenty-mcp-server setup' or install globally for permanent use.
56 lines • 1.82 kB
JavaScript
export class TokenValidator {
clerkClient;
cache;
constructor(clerkClient) {
this.clerkClient = clerkClient;
this.cache = new Map();
}
async validateBearerToken(authHeader) {
if (!authHeader) {
return { valid: false, error: 'Missing Authorization header' };
}
if (!authHeader.startsWith('Bearer ')) {
return { valid: false, error: 'Invalid Authorization header format' };
}
// Extract token from "Bearer <token>"
const token = authHeader.substring(7); // Remove "Bearer " prefix
// Check cache
const cached = this.cache.get(token);
if (cached && cached.expiry > Date.now()) {
return cached.result;
}
try {
// Validate with Clerk
const result = await this.clerkClient.validateToken(token);
if (result.valid) {
// Cache successful validation for 5 minutes
this.cache.set(token, {
result,
expiry: Date.now() + 5 * 60 * 1000,
});
// Clean up old cache entries
this.cleanCache();
}
return result;
}
catch (error) {
console.error('Token validation error:', error);
return {
valid: false,
error: error instanceof Error ? error.message : 'Token validation failed',
};
}
}
cleanCache() {
const now = Date.now();
for (const [token, entry] of this.cache.entries()) {
if (entry.expiry < now) {
this.cache.delete(token);
}
}
}
clearCache() {
this.cache.clear();
}
}
//# sourceMappingURL=token-validator.js.map