forked from Patrick-Davis-MSFT/chatbot-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "Changes to make api work and also better errors for no manag…
…ed identity" This reverts commit 2b73c33.
- Loading branch information
Showing
1 changed file
with
5 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) : '';; | ||
} | ||
|