@maheidem/linkedin-mcp
Version:
Comprehensive LinkedIn API MCP server with automatic Claude configuration
80 lines • 2.36 kB
JavaScript
;
/**
* Simple Token Manager for LinkedIn MCP
* Handles token persistence at ~/.linkedin-mcp/tokens/linkedin_token.json
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadToken = loadToken;
exports.saveToken = saveToken;
exports.isTokenExpired = isTokenExpired;
exports.getValidToken = getValidToken;
exports.getTokenInfo = getTokenInfo;
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = require("path");
// Storage location - user-wide accessible
const TOKEN_DIR = (0, path_1.join)((0, os_1.homedir)(), '.linkedin-mcp', 'tokens');
const TOKEN_FILE = (0, path_1.join)(TOKEN_DIR, 'linkedin_token.json');
// 1-hour buffer before expiration
const EXPIRATION_BUFFER_MS = 60 * 60 * 1000;
/**
* Load token from disk
*/
async function loadToken() {
try {
const data = await fs_1.promises.readFile(TOKEN_FILE, 'utf-8');
return JSON.parse(data);
}
catch {
return null;
}
}
/**
* Save token to disk
*/
async function saveToken(token) {
// Ensure directory exists
await fs_1.promises.mkdir(TOKEN_DIR, { recursive: true });
await fs_1.promises.writeFile(TOKEN_FILE, JSON.stringify(token, null, 2));
}
/**
* Check if token is expired (with 1-hour buffer)
*/
function isTokenExpired(token) {
const expiresAt = token.created_at + (token.expires_in * 1000);
const now = Date.now();
return now >= (expiresAt - EXPIRATION_BUFFER_MS);
}
/**
* Get a valid access token, or null if expired/missing
*/
async function getValidToken() {
const token = await loadToken();
if (!token) {
return null;
}
if (isTokenExpired(token)) {
console.error('Token expired. Run linkedin_auto_auth to refresh.');
return null;
}
return token.access_token;
}
/**
* Get token expiration info for display
*/
async function getTokenInfo() {
const token = await loadToken();
if (!token) {
return null;
}
const expiresAt = token.created_at + (token.expires_in * 1000);
const now = Date.now();
const remainingMs = expiresAt - now;
const remainingDays = Math.floor(remainingMs / (24 * 60 * 60 * 1000));
return {
valid: !isTokenExpired(token),
expiresAt: new Date(expiresAt).toISOString(),
remainingDays
};
}
//# sourceMappingURL=simple-token-manager.js.map