const { PublicClientApplication } = require('@azure/msal-node');
const keytar = require('keytar');
const clientId = 'c0b0c578-c2dd-4f8c-87fc-2f5e6c2e346f';
const tenantId = '787eb1ee-f302-4444-b243-70222fdf851f';
const serviceKeyName = 'mcp-onedrive-sharepoint';
const scopes = [
'https://graph.microsoft.com/Files.ReadWrite.All',
'https://graph.microsoft.com/Sites.ReadWrite.All',
'https://graph.microsoft.com/User.Read',
'offline_access'
];
async function authenticate() {
const pca = new PublicClientApplication({
auth: {
clientId,
authority: `https://login.microsoftonline.com/${tenantId}`
}
});
console.log('🔑 Starting authentication...\n');
const result = await pca.acquireTokenByDeviceCode({
scopes,
deviceCodeCallback: (response) => {
console.log('=== Microsoft Graph Authentication ===');
console.log(`Please visit: ${response.verificationUri}`);
console.log(`Enter code: ${response.userCode}`);
console.log('Waiting for authentication...\n');
}
});
if (!result) throw new Error('No result');
const tokenInfo = {
accessToken: result.accessToken,
expiresOn: result.expiresOn,
account: {
username: result.account.username,
name: result.account.name,
tenantId: result.account.tenantId
}
};
// Save token using keytar (CommonJS)
await keytar.setPassword(serviceKeyName, 'access_token', JSON.stringify(tokenInfo));
console.log('✅ Token saved to system keychain!');
console.log(`👤 User: ${tokenInfo.account.username}`);
// Verify
const saved = await keytar.getPassword(serviceKeyName, 'access_token');
console.log('✅ Verified:', saved ? 'Token stored' : 'FAILED');
}
authenticate().catch(console.error);