Skip to main content
Glama
auth.js2.69 kB
import { decryptToken } from './token-decryptor.js'; /** * Authentication service for Monotype API * Requires token-based authentication - no hardcoded fallback values */ export class AuthService { constructor(token) { this.token = null; this.decryptedTokenData = null; // Cache decrypted profileId and customerId // If token provided in constructor, set it immediately if (token) { this.setToken(token); } } /** * Set authentication token and decrypt it to extract profileId and customerId * @param {string} token - The encrypted token (optional) * @throws {Error} If token is provided but decryption fails */ setToken(token) { if (!token || token === '') { // Token is optional, so just return if empty return; } this.token = token; this.decryptedTokenData = decryptToken(token); if (!this.decryptedTokenData) { throw new Error('Token decryption failed. Please provide a valid token.'); } } /** * Get customer ID - extracts from decrypted token * @throws {Error} If token has not been set or decryption failed */ getCustomerId() { if (!this.decryptedTokenData?.globalCustomerId) { throw new Error('Customer ID not available. Token may not be set or decryption failed. Please set MONOTYPE_TOKEN environment variable.'); } return this.decryptedTokenData.globalCustomerId; } /** * Get profile ID - extracts from decrypted token * @throws {Error} If token has not been set or decryption failed */ getProfileId() { if (!this.decryptedTokenData?.profileId) { throw new Error('Profile ID not available. Token may not be set or decryption failed. Please set MONOTYPE_TOKEN environment variable.'); } return this.decryptedTokenData.profileId; } /** * Get authentication headers for API requests */ getAuthHeaders() { const headers = { "accept": "application/json", "x-client-id": "enterprise", "x-correlation-id": this.generateCorrelationId(), "Content-Type": "application/json", "x-profile-id": this.getProfileId(), }; // Add Authorization header when token is available if (this.token) { headers["Authorization"] = `Bearer ${this.token}`; } return headers; } /** * Generate a correlation ID for request tracking */ generateCorrelationId() { return `${this.randomHex(8)}-${this.randomHex(4)}-${this.randomHex(4)}-${this.randomHex(4)}-${this.randomHex(12)}`; } /** * Generate random hex string */ randomHex(length) { return Array.from({ length }, () => Math.floor(Math.random() * 16).toString(16)).join(""); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/manishgadhock-monotype/monotype-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server