From 6263c8afa26de674b3566090ef39324e1fdec08d Mon Sep 17 00:00:00 2001 From: Piali Choudhury Date: Wed, 8 Jan 2025 11:27:56 -0800 Subject: [PATCH] Remove function to create lock file when tokencache.bin is missing. (#534) Instead use withLockFile on the directory name. --- .../agentUtils/graphUtils/src/graphClient.ts | 127 ++++++++---------- 1 file changed, 53 insertions(+), 74 deletions(-) diff --git a/ts/packages/agents/agentUtils/graphUtils/src/graphClient.ts b/ts/packages/agents/agentUtils/graphUtils/src/graphClient.ts index 15123f305..2d3f02821 100644 --- a/ts/packages/agents/agentUtils/graphUtils/src/graphClient.ts +++ b/ts/packages/agents/agentUtils/graphUtils/src/graphClient.ts @@ -19,7 +19,7 @@ import { User } from "@microsoft/microsoft-graph-types"; import { TokenCredentialAuthenticationProvider } from "@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials/index.js"; import { useIdentityPlugin } from "@azure/identity"; import { cachePersistencePlugin } from "@azure/identity-cache-persistence"; -import { readFileSync, existsSync, writeFileSync, unlinkSync } from "fs"; +import { readFileSync, existsSync, writeFileSync } from "fs"; import path from "path"; import lockfile from "proper-lockfile"; import registerDebug from "debug"; @@ -70,37 +70,6 @@ function readFileSafely(filePath: string): string | undefined { } } -async function createFileSafely( - filePath: string, - content: string = "", -): Promise { - if (!existsSync(filePath)) { - const dir = path.dirname(filePath); - const lockFilePath = path.join(dir, ".create-lock"); - - if (!existsSync(dir)) { - return; - } - - if (!existsSync(lockFilePath)) { - writeFileSync(lockFilePath, ""); - } - - const release = await lockfile.lock(lockFilePath); - try { - if (!existsSync(filePath)) { - writeFileSync(filePath, content); - } - } finally { - await release(); - } - - if (existsSync(lockFilePath)) { - unlinkSync(lockFilePath); - } - } -} - function writeFileSafety(filePath: string, content: string) { try { writeFileSync(filePath, content); @@ -182,53 +151,63 @@ export class GraphClient extends EventEmitter { private async initializeGraphFromDeviceCode( cb?: DevicePromptCallback, ): Promise { - await createFileSafely(this.AUTH_RECORD_PATH); - return withLockFile(this.AUTH_RECORD_PATH, async () => { - const options: DeviceCodeCredentialOptions = { - clientId: this._settings.clientId, - tenantId: this._settings.tenantId, - disableAutomaticAuthentication: true, - - tokenCachePersistenceOptions: { - enabled: true, - name: "typeagent-tokencache", - }, - }; - if (cb) { - options.userPromptCallback = (deviceCodeInfo) => - cb(deviceCodeInfo.message); - } - const fileContent = readFileSafely(this.AUTH_RECORD_PATH); - if (fileContent !== undefined && fileContent != "") { - const authRecord: AuthenticationRecord = - deserializeAuthenticationRecord(fileContent); - if (authRecord.authority !== undefined) { - options.authenticationRecord = authRecord; + return withLockFile( + existsSync(this.AUTH_RECORD_PATH) + ? this.AUTH_RECORD_PATH + : path.dirname(this.AUTH_RECORD_PATH), + async () => { + const options: DeviceCodeCredentialOptions = { + clientId: this._settings.clientId, + tenantId: this._settings.tenantId, + disableAutomaticAuthentication: true, + + tokenCachePersistenceOptions: { + enabled: true, + name: "typeagent-tokencache", + }, + }; + if (cb) { + options.userPromptCallback = (deviceCodeInfo) => + cb(deviceCodeInfo.message); } - } - const credential = new DeviceCodeCredential(options); - if (cb === undefined) { - // getToken to make sure we can authenticate silently - await credential.getToken(this.MSGRAPH_AUTH_URL); - if (options.authenticationRecord !== undefined) { - return this.createClient(credential); + if (existsSync(this.AUTH_RECORD_PATH)) { + const fileContent = readFileSafely(this.AUTH_RECORD_PATH); + if (fileContent !== undefined && fileContent != "") { + const authRecord: AuthenticationRecord = + deserializeAuthenticationRecord(fileContent); + if (authRecord.authority !== undefined) { + options.authenticationRecord = authRecord; + } + } } - } - // This will ask for user interaction - const authRecord = await credential.authenticate( - this.MSGRAPH_AUTH_URL, - ); + const credential = new DeviceCodeCredential(options); + if (cb === undefined) { + // getToken to make sure we can authenticate silently + await credential.getToken(this.MSGRAPH_AUTH_URL); + if (options.authenticationRecord !== undefined) { + return this.createClient(credential); + } + } - if (authRecord) { - const serializedAuthRecord = - serializeAuthenticationRecord(authRecord); - writeFileSafety(this.AUTH_RECORD_PATH, serializedAuthRecord); - debugGraph("Authenticated"); - } - return this.createClient(credential); - }); + // This will ask for user interaction + const authRecord = await credential.authenticate( + this.MSGRAPH_AUTH_URL, + ); + + if (authRecord) { + const serializedAuthRecord = + serializeAuthenticationRecord(authRecord); + writeFileSafety( + this.AUTH_RECORD_PATH, + serializedAuthRecord, + ); + debugGraph("Authenticated"); + } + return this.createClient(credential); + }, + ); } private async initializeGraphFromUserCred(): Promise {