Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Seamless OAuth 2.0 authentication to Azure services with Credential Chain

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
NotificationsYou must be signed in to change notification settings

pedrobtz/azr

Repository files navigation

PackageVersionR-CMD-checkdownloadsCodecov test coverage

azr implements a credential chain for seamless OAuth 2.0 authentication to Azure services. It builds onhttr2's OAuth framework to provide cache and automatic credential discovery, trying different authentication methods in sequence until one succeeds.

Installation

You can install httr2 from CRAN with:

install.packages("azr")

Overview

The package supports creating Credential chains for Authentication with:

  • Client Secret Credential: Service principal authentication with client ID and secret
  • Azure CLI Credential: Leverages existing Azure CLI (az) login
  • Authorization Code Flow: Interactive browser-based authentication
  • Device Code Flow: Authentication for headless or CLI environments

During interactive development, azr allows browser-based login flows, while in batch/production mode it seamlessly falls back to non-interactive methods.

Usage

The simplest way to authenticate is usingget_token(), which automatically tries different authentication methods until one succeeds:

library(azr)# Get a token using the default credential chaintoken<- get_token(tenant_id="your-tenant-id",scope="https://management.azure.com/.default")# Use the token with httr2library(httr2)req<- request("https://management.azure.com/subscriptions?api-version=2020-01-01")|>  req_auth_bearer_token(token$access_token)resp<- req_perform(req)

Alternatively, useget_request_authorizer() to get a function that adds authentication to requests:

library(azr)library(httr2)# Get a request authorizer for Microsoft Graph APIazr_req_auth<- get_request_authorizer(tenant_id="your-tenant-id",scope="https://graph.microsoft.com/.default")# Use it to authenticate requestsresp<- request("https://graph.microsoft.com/v1.0/me")|>  azr_req_auth()|>  req_perform()

You can customize which authentication methods are tried and in what order:

# Define a custom credential chain with specific credential instancescustom_chain<- credential_chain(ClientSecretCredential$new(# e.g. app://mycompany.onmicrosoft.com/MyAppId/DEV/my-api/.defaultscope= Sys.getenv("APP_SCOPE"),# the 'Application Id' used in production/batch modeclient_id= Sys.getenv("APP_CLIENT_ID"),client_secret= Sys.getenv("APP_CLIENT_SECRET")  ),# during development the developer authenticates via 'az login --use-device-code'AzureCLICredential)# Use the custom chaintoken<- get_token(tenant_id="mycompany-tenant-id",scope="https://management.azure.com/.default",.chain=custom_chain)

Using with Azure OpenAI and elmer

You can useget_credential_auth() to create a chat connection to Azure OpenAI with theelmer package:

library(elmer)# Create an authentication function for Azure OpenAIcredentials<-azr::get_credential_auth(scope="https://cognitiveservices.azure.com/.default")# Create a chat interface to Azure OpenAIchat<- chat_azure_openai(endpoint="https://your-resource.openai.azure.com",model="gpt-4o",credentials=credentials)# Use the chatchat$chat("What is the capital of France?")

Related work

azr is inspired by Python'sazure-identity library, which provides comprehensive coverage of Azure authentication scenarios and introduced the credential chain pattern for automatic authentication method discovery.

The R packageAzureAuth (based onhttr) also provides token acquisition for Azure services, but does not offer an explicit way to define credential chains. This becomes important in scenarios where different authentication methods require different configurations. For example:

  • Client Secret Credentials: Using a service principalclient_id with an application-specificscope
  • Interactive Credentials: Using user authentication with different credentials

azr addresses this by allowing you to define custom credential chains with method-specific configurations, enabling seamless fallback between authentication approaches.

Code of Conduct

Please note that the azr project is released with aContributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

About

Seamless OAuth 2.0 authentication to Azure services with Credential Chain

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp