Update dependencies and enhance GraphApiClient for improved OneDrive integration

- Added @azure/msal-node and @microsoft/microsoft-graph-client to package.json and package-lock.json for enhanced authentication and API interactions.
- Refactored GraphApiClient to improve access token retrieval and logging, ensuring better handling of cookies and token formatting.
- Updated the method for listing folder contents to utilize the new authentication flow, enhancing the overall functionality of the OneDrive integration.
This commit is contained in:
Tiger Ren 2025-01-13 21:07:28 +08:00
parent 7c6e46c77f
commit f09966ecd5
3 changed files with 242 additions and 15 deletions

197
package-lock.json generated
View File

@ -9,7 +9,9 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"@azure/msal-node": "^1.18.4",
"@electron/remote": "^2.1.2",
"@microsoft/microsoft-graph-client": "^3.0.2",
"electron-store": "^10.0.0",
"node-fetch": "^3.3.0",
"uuid": "^11.0.4"
@ -18,6 +20,47 @@
"electron": "^33.2.1"
}
},
"node_modules/@azure/msal-common": {
"version": "13.3.1",
"resolved": "https://registry.npmjs.org/@azure/msal-common/-/msal-common-13.3.1.tgz",
"integrity": "sha512-Lrk1ozoAtaP/cp53May3v6HtcFSVxdFrg2Pa/1xu5oIvsIwhxW6zSPibKefCOVgd5osgykMi5jjcZHv8XkzZEQ==",
"engines": {
"node": ">=0.8.0"
}
},
"node_modules/@azure/msal-node": {
"version": "1.18.4",
"resolved": "https://registry.npmjs.org/@azure/msal-node/-/msal-node-1.18.4.tgz",
"integrity": "sha512-Kc/dRvhZ9Q4+1FSfsTFDME/v6+R2Y1fuMty/TfwqE5p9GTPw08BPbKgeWinE8JRHRp+LemjQbUZsn4Q4l6Lszg==",
"deprecated": "A newer major version of this library is available. Please upgrade to the latest available version.",
"dependencies": {
"@azure/msal-common": "13.3.1",
"jsonwebtoken": "^9.0.0",
"uuid": "^8.3.0"
},
"engines": {
"node": "10 || 12 || 14 || 16 || 18"
}
},
"node_modules/@azure/msal-node/node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/@babel/runtime": {
"version": "7.26.0",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.26.0.tgz",
"integrity": "sha512-FDSOghenHTiToteC/QRlv2q3DhPZ/oOXTBoirfWNx1Cx3TMVcGWQtMMmQcSvb/JjpNeGzx8Pq/b4fKEJuWm1sw==",
"dependencies": {
"regenerator-runtime": "^0.14.0"
},
"engines": {
"node": ">=6.9.0"
}
},
"node_modules/@electron/get": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/@electron/get/-/get-2.0.3.tgz",
@ -46,6 +89,32 @@
"electron": ">= 13.0.0"
}
},
"node_modules/@microsoft/microsoft-graph-client": {
"version": "3.0.7",
"resolved": "https://registry.npmjs.org/@microsoft/microsoft-graph-client/-/microsoft-graph-client-3.0.7.tgz",
"integrity": "sha512-/AazAV/F+HK4LIywF9C+NYHcJo038zEnWkteilcxC1FM/uK/4NVGDKGrxx7nNq1ybspAroRKT4I1FHfxQzxkUw==",
"dependencies": {
"@babel/runtime": "^7.12.5",
"tslib": "^2.2.0"
},
"engines": {
"node": ">=12.0.0"
},
"peerDependenciesMeta": {
"@azure/identity": {
"optional": true
},
"@azure/msal-browser": {
"optional": true
},
"buffer": {
"optional": true
},
"stream-browserify": {
"optional": true
}
}
},
"node_modules/@sindresorhus/is": {
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.6.0.tgz",
@ -172,6 +241,11 @@
"node": "*"
}
},
"node_modules/buffer-equal-constant-time": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
"integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA=="
},
"node_modules/cacheable-lookup": {
"version": "5.0.4",
"resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz",
@ -388,6 +462,14 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/ecdsa-sig-formatter": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
"integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
"dependencies": {
"safe-buffer": "^5.0.1"
}
},
"node_modules/electron": {
"version": "33.2.1",
"resolved": "https://registry.npmjs.org/electron/-/electron-33.2.1.tgz",
@ -734,6 +816,57 @@
"graceful-fs": "^4.1.6"
}
},
"node_modules/jsonwebtoken": {
"version": "9.0.2",
"resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz",
"integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==",
"dependencies": {
"jws": "^3.2.2",
"lodash.includes": "^4.3.0",
"lodash.isboolean": "^3.0.3",
"lodash.isinteger": "^4.0.4",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"lodash.once": "^4.0.0",
"ms": "^2.1.1",
"semver": "^7.5.4"
},
"engines": {
"node": ">=12",
"npm": ">=6"
}
},
"node_modules/jsonwebtoken/node_modules/semver": {
"version": "7.6.3",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz",
"integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==",
"bin": {
"semver": "bin/semver.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/jwa": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
"integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
"dependencies": {
"buffer-equal-constant-time": "1.0.1",
"ecdsa-sig-formatter": "1.0.11",
"safe-buffer": "^5.0.1"
}
},
"node_modules/jws": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
"integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
"dependencies": {
"jwa": "^1.4.1",
"safe-buffer": "^5.0.1"
}
},
"node_modules/keyv": {
"version": "4.5.4",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",
@ -742,6 +875,41 @@
"json-buffer": "3.0.1"
}
},
"node_modules/lodash.includes": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
"integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w=="
},
"node_modules/lodash.isboolean": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
"integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg=="
},
"node_modules/lodash.isinteger": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
"integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA=="
},
"node_modules/lodash.isnumber": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
"integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw=="
},
"node_modules/lodash.isplainobject": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA=="
},
"node_modules/lodash.isstring": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw=="
},
"node_modules/lodash.once": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
"integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg=="
},
"node_modules/lowercase-keys": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz",
@ -890,6 +1058,11 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/regenerator-runtime": {
"version": "0.14.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz",
"integrity": "sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw=="
},
"node_modules/require-from-string": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
@ -931,6 +1104,25 @@
"node": ">=8.0"
}
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
]
},
"node_modules/semver": {
"version": "6.3.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz",
@ -982,6 +1174,11 @@
"node": ">= 8.0"
}
},
"node_modules/tslib": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="
},
"node_modules/type-fest": {
"version": "0.13.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.13.1.tgz",

View File

@ -17,6 +17,8 @@
"@electron/remote": "^2.1.2",
"electron-store": "^10.0.0",
"node-fetch": "^3.3.0",
"uuid": "^11.0.4"
"uuid": "^11.0.4",
"@azure/msal-node": "^1.18.4",
"@microsoft/microsoft-graph-client": "^3.0.2"
}
}

View File

@ -22,11 +22,11 @@ class GraphApiClient {
}
});
const authUrl = `https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?` +
const authUrl = `https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?` +
`client_id=${this.clientId}` +
`&nonce=uv.${uuidv4()}` +
`&response_mode=form_post` +
`&scope=${encodeURIComponent(this.scopes)}` +
`&scope=${this.scopes}` +
`&response_type=code` +
`&redirect_uri=${encodeURIComponent(this.redirectUrl)}`;
@ -53,19 +53,45 @@ class GraphApiClient {
// Get all cookies
const cookies = await authWindow.webContents.session.cookies.get({});
console.log('Found cookies:', cookies.length);
console.log('Cookies:', cookies);
// Find the access token
const accessToken = cookies.find(
cookie => cookie.name === 'AccessToken-OneDrive.ReadWrite'
// Find the access token cookie
const accessTokenCookie = cookies.find(
c => c.name === 'AccessToken-OneDrive.ReadWrite'
);
console.log('Access token cookie:', accessTokenCookie);
if (accessToken) {
console.log('Found access token in cookies');
authWindow.close();
resolve(accessToken.value);
} else {
console.log('Access token not found in cookies, waiting...');
if (!accessTokenCookie) {
console.log('Access token not found in cookies');
return;
}
// Clean and format the access token
let accessToken = accessTokenCookie.value;
console.log('Access token:', accessToken);
// Remove any URL encoding
accessToken = decodeURIComponent(accessToken);
console.log('Access token decoded:', accessToken);
// Remove any extra dots beyond the two expected in a JWT
const tokenParts = accessToken.split('.');
if (tokenParts.length > 3) {
accessToken = tokenParts.slice(0, 3).join('.');
}
console.log('Access token formatted:', accessToken);
// Convert cookies to a cookie string
const cookieString = cookies
.map(cookie => `${cookie.name}=${cookie.value}`)
.join('; ');
authWindow.close();
resolve({
cookies: cookieString,
accessToken: accessToken
});
} catch (error) {
console.error('Error getting cookies:', error);
reject(error);
@ -92,8 +118,9 @@ class GraphApiClient {
async listFolderContents(folderPath) {
try {
console.log('graphApiClient listFolderContents:', folderPath);
const accessToken = await this.getAccessToken();
console.log('graphApiClient Access token:', accessToken);
const auth = await this.getAccessToken();
console.log('graphApiClient Auth received');
const cleanPath = this.cleanPath(folderPath);
console.log('graphApiClient Clean path:', cleanPath);
const url = `${this.baseUrl}/me/drive/root:/${cleanPath}:/children`;
@ -102,8 +129,9 @@ class GraphApiClient {
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Authorization': `Bearer ${auth.accessToken}`,
'Accept': 'application/json',
'Cookie': auth.cookies,
'Origin': 'https://onedrive.live.com',
'Referer': 'https://onedrive.live.com/'
}