onedrive_sync/renderer/graphApi.js

119 lines
4.2 KiB
JavaScript

const { BrowserWindow } = require('@electron/remote');
const { v4: uuidv4 } = require('uuid'); // Add this for GUID generation
const { protectedResources, msalConfig } = require("./authConfig");
class GraphApiClient {
constructor() {
this.baseUrl = 'https://graph.microsoft.com/v1.0';
this.clientId = "d581ab07-3a21-44d3-84c4-16b06bef6266";
this.scopes = "Files.ReadWrite offline_access";
this.redirectUrl = "https://login.microsoftonline.com/common/oauth2/nativeclient";
}
cleanPath(path) {
return path
.replace(/^\/+|\/+$/g, '') // Remove leading/trailing slashes
.split('/')
.map(segment => encodeURIComponent(segment))
.join('/');
}
async listFolderContents(folderPath) {
const accessToken = await this.getAccessToken();
console.log("listFolderContents accessToken", accessToken);
// Remove AuthProvider usage completely
}
async clearCaches() {
const session = require('@electron/remote').session;
const defaultSession = session.defaultSession;
try {
await Promise.all([
defaultSession.clearStorageData({
storages: [
'cookies',
'localstorage',
'cachestorage',
'appcache',
'filesystem',
'indexdb',
'shadercache',
'websql',
'serviceworkers',
]
}),
defaultSession.clearCache(),
defaultSession.clearAuthCache()
]);
console.log('All caches cleared successfully');
} catch (error) {
console.error('Error clearing caches:', error);
throw error;
}
}
async getAccessToken() {
await this.clearCaches();
return new Promise((resolve, reject) => {
const authWindow = new BrowserWindow({
width: 800,
height: 600,
show: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
webSecurity: false // Allow cross-origin requests
}
});
// Handle navigation events
authWindow.webContents.on('will-redirect', (event, url) => {
console.log('Redirect detected:', url);
this.handleAuthResponse(url, authWindow, resolve, reject);
});
authWindow.webContents.on('did-finish-load', () => {
console.log('Finished loading:', authWindow.webContents.getURL());
});
const authUrl = `https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?` +
`client_id=${this.clientId}` +
`&response_type=token` + // Use token flow directly
`&scope=${encodeURIComponent(this.scopes)}` +
`&redirect_uri=${encodeURIComponent(this.redirectUrl)}` +
`&response_mode=fragment` + // Required for token flow
`&prompt=select_account`;
console.log('Loading auth URL:', authUrl);
authWindow.loadURL(authUrl);
});
}
handleAuthResponse(url, authWindow, resolve, reject) {
if (url.startsWith(this.redirectUrl)) {
try {
const hash = new URL(url).hash.substring(1);
const params = new URLSearchParams(hash);
const accessToken = params.get('access_token');
if (accessToken) {
authWindow.close();
resolve({
accessToken: accessToken,
expiresIn: params.get('expires_in')
});
} else {
reject(new Error('No access token found in response'));
}
} catch (error) {
console.error('Error processing auth response:', error);
reject(error);
}
}
}
}
module.exports = new GraphApiClient();