Skip to content

Commit

Permalink
Changes to make api work and also better errors for no managed identity
Browse files Browse the repository at this point in the history
  • Loading branch information
andychase committed Oct 2, 2024
1 parent 3822f78 commit 9aaf6b0
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions utils/lib/azure.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// lib/azure.js

import { DefaultAzureCredential } from "@azure/identity";
//import { getCache, setCache } from "./cache";



export async function getAuthToken() {
if (process.env.AZURE_USE_MANAGED_IDENTITY != "true") {
return "";
}
const { ManagedIdentityCredential } = require("@azure/identity");
let cachedToken = process.env.AUTH_TOKEN ? JSON.parse(process.env.AUTH_TOKEN) : '';
if (!cachedToken || cachedToken.expiresOnTimestamp < Date.now()) {
let cachedCredential = new DefaultAzureCredential();
let cachedCredential = new ManagedIdentityCredential();
cachedToken = await cachedCredential.getToken("https://cognitiveservices.azure.com/.default");
process.env.AUTH_TOKEN = JSON.stringify(cachedToken);
return cachedToken;
}
// console.log("cachedToken from memory", cachedToken);
return process.env.AUTH_TOKEN ? JSON.parse(process.env.AUTH_TOKEN) : '';;
}

0 comments on commit 9aaf6b0

Please sign in to comment.