-
Notifications
You must be signed in to change notification settings - Fork 2k
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
BearerTokenChallengeAuthorizationPolicy extracting tenant ID #44280
Open
ibrahimrabab
wants to merge
5
commits into
Azure:main
Choose a base branch
from
ibrahimrabab:bearerTokenTenantIdIssue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
660f37d
added logic to extract tenant id properly
ibrahimrabab b6ea08c
separating logic when parsing authorization
ibrahimrabab 2cdfd8c
fixed pom file with dependency tag
ibrahimrabab f6c6762
using LOGGER
ibrahimrabab 349b89a
closing mock
ibrahimrabab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
.../com/azure/storage/common/policy/StorageBearerTokenChallengeAuthorizationPolicyTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.azure.storage.common.policy; | ||
|
||
import com.azure.core.credential.TokenCredential; | ||
import com.azure.core.http.HttpHeaderName; | ||
import com.azure.core.http.HttpMethod; | ||
import com.azure.core.http.HttpPipelineCallContext; | ||
import com.azure.core.http.HttpRequest; | ||
import com.azure.core.http.HttpResponse; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class StorageBearerTokenChallengeAuthorizationPolicyTests { | ||
|
||
private String[] scopes; | ||
private TokenCredential mockCredential; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
scopes = new String[]{"https://storage.azure.com/.default"}; | ||
mockCredential = mock(TokenCredential.class); | ||
} | ||
|
||
@Test | ||
public void usesTokenProvidedByCredentials() { | ||
StorageBearerTokenChallengeAuthorizationPolicy policy = new StorageBearerTokenChallengeAuthorizationPolicy(mockCredential, scopes); | ||
|
||
HttpPipelineCallContext mockContext = mock(HttpPipelineCallContext.class); | ||
HttpResponse mockResponse = mock(HttpResponse.class); | ||
when(mockResponse.getHeaderValue(HttpHeaderName.WWW_AUTHENTICATE)).thenReturn(null); | ||
|
||
Mono<Boolean> result = policy.authorizeRequestOnChallenge(mockContext, mockResponse); | ||
|
||
StepVerifier.create(result) | ||
.expectNext(false) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
public void doesNotSendUnauthorizedRequestWhenEnableTenantDiscoveryIsFalse() { | ||
StorageBearerTokenChallengeAuthorizationPolicy policy = new StorageBearerTokenChallengeAuthorizationPolicy(mockCredential, scopes); | ||
|
||
HttpPipelineCallContext mockContext = mock(HttpPipelineCallContext.class); | ||
HttpResponse mockResponse = mock(HttpResponse.class); | ||
when(mockResponse.getHeaderValue(HttpHeaderName.WWW_AUTHENTICATE)).thenReturn(null); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
Mono<Boolean> result = policy.authorizeRequestOnChallenge(mockContext, mockResponse); | ||
StepVerifier.create(result) | ||
.expectNext(false) | ||
.verifyComplete(); | ||
} | ||
} | ||
|
||
@Test | ||
public void sendsUnauthorizedRequestWhenEnableTenantDiscoveryIsTrue() { | ||
StorageBearerTokenChallengeAuthorizationPolicy realPolicy = | ||
new StorageBearerTokenChallengeAuthorizationPolicy(mockCredential, scopes); | ||
|
||
// Spy on the real instance | ||
StorageBearerTokenChallengeAuthorizationPolicy policy = spy(realPolicy); | ||
|
||
String expectedTenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47"; | ||
|
||
HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, "https://example.com"); | ||
HttpPipelineCallContext mockContext = mock(HttpPipelineCallContext.class); | ||
when(mockContext.getHttpRequest()).thenReturn(httpRequest); | ||
|
||
HttpResponse mockResponse = mock(HttpResponse.class); | ||
when(mockResponse.getHeaderValue(HttpHeaderName.WWW_AUTHENTICATE)).thenReturn( | ||
"Bearer authorization_uri=https://login.microsoftonline.com/" + expectedTenantId + "/oauth2/authorize resource_id=https://storage.azure.com"); | ||
|
||
// Properly stub the method on the spy | ||
doReturn(Mono.empty()).when(policy).setAuthorizationHeader(any(), any()); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
Mono<Boolean> result = policy.authorizeRequestOnChallenge(mockContext, mockResponse); | ||
|
||
StepVerifier.create(result) | ||
.expectNext(true) // Expect the Mono<Boolean> to emit 'true' | ||
.verifyComplete(); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void usesScopeFromBearerChallenge() { | ||
StorageBearerTokenChallengeAuthorizationPolicy realPolicy = | ||
new StorageBearerTokenChallengeAuthorizationPolicy(mockCredential, "https://disk.compute.azure.com/.default"); | ||
|
||
// Spy on the real instance to allow stubbing setAuthorizationHeader | ||
StorageBearerTokenChallengeAuthorizationPolicy policy = spy(realPolicy); | ||
|
||
String serviceChallengeResponseScope = "https://storage.azure.com"; | ||
|
||
HttpPipelineCallContext mockContext = mock(HttpPipelineCallContext.class); | ||
HttpResponse mockResponse = mock(HttpResponse.class); | ||
when(mockResponse.getHeaderValue(HttpHeaderName.WWW_AUTHENTICATE)).thenReturn( | ||
"Bearer authorization_uri=https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/oauth2/authorize resource_id=" + serviceChallengeResponseScope); | ||
|
||
// Stub the setAuthorizationHeader method so it returns a completed Mono | ||
doReturn(Mono.empty()).when(policy).setAuthorizationHeader(any(), any()); | ||
|
||
for (int i = 0; i < 2; i++) { | ||
Mono<Boolean> result = policy.authorizeRequestOnChallenge(mockContext, mockResponse); | ||
|
||
StepVerifier.create(result) | ||
.expectNext(true) // Expect 'true' instead of failing | ||
.verifyComplete(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
authorization check should be in separate independent if block and then parse the tenant ID.
While its not common but its possible that authorization_uri is not present but scope/resource id is, need to accomodate this scenario.