Skip to content

Commit

Permalink
Remove function to create lock file when tokencache.bin is missing. (#…
Browse files Browse the repository at this point in the history
…534)

Instead use withLockFile on the directory name.
  • Loading branch information
pcdeadeasy authored Jan 8, 2025
1 parent c6172f1 commit 6263c8a
Showing 1 changed file with 53 additions and 74 deletions.
127 changes: 53 additions & 74 deletions ts/packages/agents/agentUtils/graphUtils/src/graphClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -70,37 +70,6 @@ function readFileSafely(filePath: string): string | undefined {
}
}

async function createFileSafely(
filePath: string,
content: string = "",
): Promise<void> {
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);
Expand Down Expand Up @@ -182,53 +151,63 @@ export class GraphClient extends EventEmitter {
private async initializeGraphFromDeviceCode(
cb?: DevicePromptCallback,
): Promise<Client> {
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<Client> {
Expand Down

0 comments on commit 6263c8a

Please sign in to comment.