Skip to content
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

feat (provider/azure): add support for Azure Managed Identity authentication #3124 #3463

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ShervK
Copy link

@ShervK ShervK commented Nov 4, 2024

Fixes: #3124

Added a parameter to pass a bearer token provider with the @azure/identity package to createAzure. This way, its similar to the Azure documentation on how to use managed identity as well as how the OpenAI node package handles Azure Managed Identity.

Example:

import { createAzure } from '@ai-sdk/azure';
import { streamText } from 'ai';
import {
  getBearerTokenProvider,
  DefaultAzureCredential, // Can use any type of Credential module, not just Default
} from '@azure/identity';


const credential = new DefaultAzureCredential();
const scope = 'https://cognitiveservices.azure.com/.default';
const azureADTokenProvider = getBearerTokenProvider(credential, scope);

async function main() {
  const provider = createAzure({
    resourceName: process.env.AZURE_RESOURCE_NAME,
    identityTokenProvider: azureADTokenProvider,
  });
 
   ...
}

Implemented by creating a fetch wrapper and injecting the Authorization token into the headers on each request, similar to how the OpenAI package grabs a new token for each request. I tried implement it in the getHeaders function but ran into issues with the async operation of the token provider, so I went with the fetch wrapper instead since I've had to use this method for managed identity.

I can add a section on how to setup and use managed identity to the azure provider docs if that would help. I also added some tests for the token provider, let me know if this would need more or less tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support azure authentication with managed identity
1 participant