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

Support Oauth Code Flow #22818

Open
denen99 opened this issue May 1, 2024 · 16 comments
Open

Support Oauth Code Flow #22818

denen99 opened this issue May 1, 2024 · 16 comments
Assignees
Labels
Azure.Identity feature-request This issue requires a new behavior in the product in order be resolved.
Milestone

Comments

@denen99
Copy link

denen99 commented May 1, 2024

Feature Request

@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels May 1, 2024
@jhendrixMSFT jhendrixMSFT added Azure.Identity feature-request This issue requires a new behavior in the product in order be resolved. and removed question The issue doesn't require a change to the product in order to be resolved. Most issues start as that needs-triage This is a new issue that needs to be triaged to the appropriate team. labels May 1, 2024
@github-actions github-actions bot added the needs-team-attention This issue needs attention from Azure service team or SDK team label May 1, 2024
@chlowell
Copy link
Contributor

chlowell commented May 1, 2024

Can you please add some more details about what you're trying to do and the change you would like to see?

@chlowell chlowell added needs-author-feedback More information is needed from author to address the issue. and removed customer-reported Issues that are reported by GitHub users external to the Azure organization. labels May 1, 2024
Copy link

github-actions bot commented May 1, 2024

Hi @denen99. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

@github-actions github-actions bot removed the needs-team-attention This issue needs attention from Azure service team or SDK team label May 1, 2024
@denen99
Copy link
Author

denen99 commented May 1, 2024 via email

@github-actions github-actions bot added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels May 1, 2024
@chlowell
Copy link
Contributor

chlowell commented May 2, 2024

You're right that ClientSecretCredential isn't for delegation. You would use it when you want your app to access resources as itself. It sounds like you want Entra's on-behalf-of flow instead, documented here. A user would authenticate to your app, which would then exchange the user's access token for one allowing it to access resources on that user's behalf. Does that sound like what you're looking for? azidentity.OnBehalfOfCredential implements it.

@chlowell chlowell added needs-author-feedback More information is needed from author to address the issue. and removed needs-team-attention This issue needs attention from Azure service team or SDK team labels May 2, 2024
Copy link

github-actions bot commented May 2, 2024

Hi @denen99. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

@denen99
Copy link
Author

denen99 commented May 2, 2024

What should the value of userAssertion be in that method, is it the accessToken? If so, is it expected that the refresh_token is used to get an updated access_token via a different method?

here is my code:

cred, err := azidentity.NewOnBehalfOfCredentialWithSecret(tenant_id, client_id, access_token, client_secret, &azidentity.OnBehalfOfCredentialOptions{})
	if err != nil {
		fmt.Printf("Error creating OnBehalfOfCredential: %v\n", err)
		return
	}

client, err := msgraphsdkgo.NewGraphServiceClientWithCredentials(cred, []string{"Files.Read", "Files.Read.All", "offline_access"})
if err != nil {
		fmt.Printf("Error creating GraphServiceClient: %v\n", err)
		return
}

result, err := client.Me().Drive().Get(context.Background(), nil)
if err != nil {
		fmt.Printf("Error getting drive: %v\n", err)
		return
}

fmt.Printf("Drive ID: %v\n", *result.GetId())

But its throwing the following error (it works fine when using the HTTP URL raw)

Error getting drive: OnBehalfOfCredential authentication failed

POST https://login.microsoftonline.com/common/oauth2/v2.0/token

RESPONSE 400 Bad Request

{
"error": "invalid_grant",
"error_description": "AADSTS50013: Assertion failed signature validation. [Reason - Key was found, but use of the key to verify the signature failed., Thumbprint of key used by client: 'REDACTED’, Found key 'Start=04/11/2024 16:04:26, End=04/11/2029 16:04:26', Please visit the Azure Portal, Graph Explorer or directly use MS Graph to see configured keys for app Id '00000000-0000-0000-0000-000000000000'. Review the documentation at https://docs.microsoft.com/en-us/graph/deployments to determine the corresponding service endpoint and https://docs.microsoft.com/en-us/graph/api/application-get?view=graph-rest-1.0&tabs=http to build a query request URL, such as 'https://graph.microsoft.com/beta/applications/00000000-0000-0000-0000-000000000000']. Trace ID: cd572808-d568-42e7-8470-38e69494a800 Correlation ID: 5d5362c2-940a-43e7-962c-207ac7d75426 Timestamp: 2024-05-02 13:16:04Z",
"error_codes": [
50013
],
"timestamp": "2024-05-02 13:16:04Z",
"trace_id": "cd572808-d568-42e7-8470-38e69494a800",
"correlation_id": "5d5362c2-940a-43e7-962c-207ac7d75426",
"error_uri": "https://login.microsoftonline.com/error?code=50013"
}

@github-actions github-actions bot added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels May 2, 2024
@chlowell
Copy link
Contributor

chlowell commented May 2, 2024

Your code looks right to me though I'm not familiar with Graph or its SDK, so I may miss something there. userAssertion is the user's access token for your app. OnBehalfOfCredential handles the details of refreshing internally, redeeming the refresh token as needed. I suppose the error must be about the value of userAssertion because that becomes the assertion parameter of your app's token request. I haven't seen this error before but it's saying the signature on userAssertion doesn't verify. Is the value the user's access token for your app?

@chlowell chlowell added needs-author-feedback More information is needed from author to address the issue. and removed needs-team-attention This issue needs attention from Azure service team or SDK team labels May 2, 2024
Copy link

github-actions bot commented May 2, 2024

Hi @denen99. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

@denen99
Copy link
Author

denen99 commented May 2, 2024 via email

@github-actions github-actions bot added the needs-team-attention This issue needs attention from Azure service team or SDK team label May 2, 2024
@github-actions github-actions bot removed the needs-author-feedback More information is needed from author to address the issue. label May 2, 2024
@chlowell
Copy link
Contributor

chlowell commented May 2, 2024

refresh_token is for the user to acquire other access tokens and isn't relevant to delegation. userAssertion would be the value of access_token. However, I see from the scopes that this access token is for Graph? It should be for your app (i.e., have your app as its audience), not the upstream resource you want the user to delegate access to. In this protocol diagram, your application is "Web API A" and Graph is "Web API B". The user acquires an access token for your app and presents it to your app e.g. as part of some HTTP request. That's step 1. OnBehalfOfCredential handles steps 2 and 3; the user's access token for your app is its userAssertion.

@denen99
Copy link
Author

denen99 commented May 2, 2024 via email

@chlowell
Copy link
Contributor

chlowell commented May 2, 2024

No worries, this is a confusing scenario. In your last comment you shared what looks like the body of a response to an authentication request. The "scope" field documents what the access token is for. Those look like Graph scopes to me. If they are, the token is for accessing Graph, not your application, and you couldn't use it for delegation. What scopes do you request in the auth code flow?

@denen99
Copy link
Author

denen99 commented May 2, 2024 via email

@chlowell
Copy link
Contributor

I'm sorry, I assumed your app runs on a server, but I notice you're redirecting to localhost with the authorization code. Does your app run on a user's machine?

@chlowell chlowell removed the needs-team-attention This issue needs attention from Azure service team or SDK team label May 10, 2024
@denen99
Copy link
Author

denen99 commented May 10, 2024 via email

@chlowell
Copy link
Contributor

Got it, thanks for clarifying. I asked about this because client apps should use a different solution.

Returning to authorization code vs. on-behalf-of (OBO), one key difference is that OBO separates a user's authorization to access an app from that app's authorization to access other APIs. In your case this means OBO would separate a user's authorization to access your app from your app's authorization to access Graph on that user's behalf. In the OBO flow a user would acquire an access token for your app, not for Graph, and your app would later exchange that token for an access token for Graph. OnBehalfOfCredential handles this exchange and the user's access token for your app is the userAssertion for its constructors. Currently you're using the auth code flow to acquire an access token for Graph directly--I guess your app has a totally separate login flow or none at all?--and that token doesn't work as the userAssertion because it's for Graph, not your application.

@RickWinter RickWinter added this to the Backlog milestone Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Identity feature-request This issue requires a new behavior in the product in order be resolved.
Projects
Status: In Progress
Development

No branches or pull requests

4 participants