-
Notifications
You must be signed in to change notification settings - Fork 585
improvement: handle azure workload identity authentication #945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,16 @@ import { ProviderAPIConfig } from '../types'; | |
import { | ||
getAccessTokenFromEntraId, | ||
getAzureManagedIdentityToken, | ||
getAzureWorkloadIdentityToken, | ||
} from './utils'; | ||
import { env, getRuntimeKey } from 'hono/adapter'; | ||
|
||
const AzureOpenAIAPIConfig: ProviderAPIConfig = { | ||
getBaseURL: ({ providerOptions }) => { | ||
const { resourceName } = providerOptions; | ||
return `https://${resourceName}.openai.azure.com/openai`; | ||
}, | ||
headers: async ({ providerOptions, fn }) => { | ||
headers: async ({ c, providerOptions, fn }) => { | ||
const { apiKey, azureAuthMode } = providerOptions; | ||
|
||
if (azureAuthMode === 'entra') { | ||
|
@@ -39,6 +41,40 @@ const AzureOpenAIAPIConfig: ProviderAPIConfig = { | |
Authorization: `Bearer ${accessToken}`, | ||
}; | ||
} | ||
if (azureAuthMode === 'workload') { | ||
const { azureWorkloadClientId } = providerOptions; | ||
|
||
const authorityHost = env(c).AZURE_AUTHORITY_HOST; | ||
const tenantId = env(c).AZURE_TENANT_ID; | ||
const clientId = azureWorkloadClientId || env(c).AZURE_CLIENT_ID; | ||
const federatedTokenFile = env(c).AZURE_FEDERATED_TOKEN_FILE; | ||
narengogi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const runtime = getRuntimeKey(); | ||
if ( | ||
authorityHost && | ||
tenantId && | ||
clientId && | ||
federatedTokenFile && | ||
runtime === 'node' | ||
) { | ||
const fs = await import('fs'); | ||
const federatedToken = fs.readFileSync(federatedTokenFile, 'utf8'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @VisargD Please share your opinion here, we can also accept the |
||
|
||
if (federatedToken) { | ||
const scope = 'https://cognitiveservices.azure.com/.default'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove the |
||
const accessToken = await getAzureWorkloadIdentityToken( | ||
authorityHost, | ||
tenantId, | ||
clientId, | ||
federatedToken, | ||
scope | ||
); | ||
return { | ||
Authorization: `Bearer ${accessToken}`, | ||
}; | ||
} | ||
} | ||
} | ||
const headersObj: Record<string, string> = { | ||
'api-key': `${apiKey}`, | ||
}; | ||
|
Uh oh!
There was an error while loading. Please reload this page.