Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async function ensureAuthenticated(forceNew = false) {

module.exports = {
tokenManager,
tokenStorage,
authTools,
ensureAuthenticated
};
69 changes: 11 additions & 58 deletions auth/token-manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/**
* Token management for Microsoft Graph API authentication
* @deprecated Use TokenStorage (auth/token-storage.js) instead.
* TokenStorage provides automatic refresh, unified scope management,
* and async file operations. This module is retained only for:
* - Test mode token creation (createTestTokens)
* - Backwards compatibility for legacy imports
* Do not add new functionality here. Migrate consumers to TokenStorage.
*/
const fs = require('fs');
const config = require('../config');
Expand Down Expand Up @@ -84,67 +89,17 @@ function getAccessToken() {
* Gets the current Flow API access token
* @returns {string|null} - The Flow access token or null if not available
*/
function getFlowAccessToken() {
const tokens = loadTokenCache();
if (!tokens) return null;

// Check if flow token exists and is not expired
if (tokens.flow_access_token && tokens.flow_expires_at) {
if (Date.now() < tokens.flow_expires_at) {
return tokens.flow_access_token;
}
}

return null;
}

/**
* Saves Flow API tokens alongside existing Graph tokens
* @param {object} flowTokens - The Flow tokens to save
* @returns {boolean} - Whether the save was successful
*/
function saveFlowTokens(flowTokens) {
try {
const tokenPath = config.AUTH_CONFIG.tokenStorePath;

// Load existing tokens
let existingTokens = {};
if (fs.existsSync(tokenPath)) {
const tokenData = fs.readFileSync(tokenPath, 'utf8');
existingTokens = JSON.parse(tokenData);
}

// Merge flow tokens
const mergedTokens = {
...existingTokens,
flow_access_token: flowTokens.access_token,
flow_refresh_token: flowTokens.refresh_token,
flow_expires_at: flowTokens.expires_at || (Date.now() + (flowTokens.expires_in || 3600) * 1000)
};

fs.writeFileSync(tokenPath, JSON.stringify(mergedTokens, null, 2), { mode: 0o600 });
console.log('Flow tokens saved successfully');

// Update cache
cachedTokens = mergedTokens;
return true;
} catch (error) {
console.error('Error saving Flow tokens:', error);
return false;
}
}

/**
* Creates a test access token for use in test mode
* @returns {object} - The test tokens
*/
function createTestTokens() {
const testTokens = {
access_token: "test_access_token_" + Date.now(),
refresh_token: "test_refresh_token_" + Date.now(),
expires_at: Date.now() + (3600 * 1000) // 1 hour
access_token: 'test_access_token_' + Date.now(),
refresh_token: 'test_refresh_token_' + Date.now(),
expires_at: Date.now() + 3600 * 1000, // 1 hour
};

saveTokenCache(testTokens);
return testTokens;
}
Expand All @@ -153,7 +108,5 @@ module.exports = {
loadTokenCache,
saveTokenCache,
getAccessToken,
getFlowAccessToken,
saveFlowTokens,
createTestTokens
createTestTokens,
};
Loading