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

Add support for interactive Entra ID authentication to chat_azure() #273

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

atheriel
Copy link
Collaborator

This commit adds support for another major Azure authentication approach: the OAuth authorization code flow, as used by the Azure CLI.

This is a good choice for authentiation during development on desktop, and Microsoft recommends it for Azure OpenAI because it doesn't require storing sensitive long-lived secrets like API keys.

All of this is pretty stock httr2 OAuth stuff, despite the fact that Entra ID has its own... idiosyncrasies. I also went out of the way to add a really specific error message for what I believe to be a common source of problems: misconfiguration of Azure's RBAC. It looks as follows:

Error in `req_perform_connection()` at elmer/R/httr2.R:36:3:
! HTTP 401 Unauthorized.
• PermissionDenied: Principal does not have access to API/Operation.
ℹ Your user or service principal likely needs one of the following
  roles: Cognitive Services OpenAI User, Cognitive Services OpenAI
  Contributor, or Cognitive Services Contributor.

I haven't added any unit tests (I don't know how to do so for this kind of interactive OAuth flow), but at least the help documentation has been updated.

This commit adds support for another major Azure authentication
approach: the OAuth authorization code flow, as used by the Azure CLI.

This is a good choice for authentiation during development on desktop,
and Microsoft recommends it for Azure OpenAI because it doesn't require
storing sensitive long-lived secrets like API keys.

All of this is pretty stock httr2 OAuth stuff, despite the fact that
Entra ID has its own... idiosyncrasies. I also went out of the way to
add a really specific error message for what I believe to be a common
source of problems: misconfiguration of Azure's RBAC. It looks as
follows:

    Error in `req_perform_connection()` at elmer/R/httr2.R:36:3:
    ! HTTP 401 Unauthorized.
    • PermissionDenied: Principal does not have access to API/Operation.
    ℹ Your user or service principal likely needs one of the following
      roles: Cognitive Services OpenAI User, Cognitive Services OpenAI
      Contributor, or Cognitive Services Contributor.

I haven't added any unit tests (I don't know how to do so for this kind
of interactive OAuth flow), but at least the help documentation has been
updated.

Signed-off-by: Aaron Jacobs <aaron.jacobs@posit.co>
# Try to be helpful in the (common) case that the user or service
# principal is missing the necessary role.
# See: https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/role-based-access-control
if (error$message == "Principal does not have access to API/Operation.") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use identical() here? Otherwise if message is every anything other than a string, you'll get a weird error.

roles: {.emph Cognitive Services OpenAI User},
{.emph Cognitive Services OpenAI Contributor}, or
{.emph Cognitive Services Contributor}.",
keep_whitespace = FALSE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need that? I think it will get re-wrapped anyway?

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.

2 participants