Skip to content

Commit 15ca25c

Browse files
authored
Update pattern for testing scopes (Azure#44040)
* Update pattern for testing scopes Fixes Azure#43895 * spotless
1 parent a7266d9 commit 15ca25c

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/util/ScopeUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public final class ScopeUtil {
1414

1515
private static final String DEFAULT_SUFFIX = "/.default";
16-
private static final Pattern SCOPE_PATTERN = Pattern.compile("^[0-9a-zA-Z-.:/]+$");
16+
private static final Pattern SCOPE_PATTERN = Pattern.compile("^[0-9a-zA-Z-.:/_]+$");
1717

1818
/**
1919
* Convert a list of scopes to a resource for Microsoft Entra ID.
@@ -55,7 +55,7 @@ public static void validateScope(String scope) {
5555

5656
if (!isScopeMatch) {
5757
throw new IllegalArgumentException("The specified scope is not in expected format."
58-
+ " Only alphanumeric characters, '.', '-', ':', and '/' are allowed");
58+
+ " Only alphanumeric characters, '.', '-', ':', '_', and '/' are allowed");
5959
}
6060
}
6161

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.identity.implementation;
5+
6+
import com.azure.identity.implementation.util.ScopeUtil;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
import java.util.stream.Stream;
11+
12+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
13+
import static org.junit.jupiter.api.Assertions.assertThrows;
14+
15+
public class ScopeUtilTests {
16+
17+
@ParameterizedTest
18+
@MethodSource("validScopes")
19+
public void validScopes(String scope) {
20+
assertDoesNotThrow(() -> ScopeUtil.validateScope(scope));
21+
22+
}
23+
24+
@ParameterizedTest
25+
@MethodSource("invalidScopes")
26+
public void validInvalidScopes(String scope) {
27+
assertThrows(IllegalArgumentException.class, () -> ScopeUtil.validateScope(scope));
28+
29+
}
30+
31+
static Stream<String> validScopes() {
32+
return Stream.of("https://vaults.azure.net/.default", "https://management.core.windows.net//.default",
33+
"https://graph.microsoft.com/User.Read", "api://app-id-has-hyphens-and-1234567890s/user_impersonation");
34+
}
35+
36+
static Stream<String> invalidScopes() {
37+
return Stream.of("api://app-id-has-hyphens-and-1234567890s/invalid scope",
38+
"api://app-id-has-hyphens-and-1234567890s/invalid\"scope",
39+
"api://app-id-has-hyphens-and-1234567890s/invalid\\scope");
40+
}
41+
}

0 commit comments

Comments
 (0)