Skip to content

Commit

Permalink
Update pattern for testing scopes (#44040)
Browse files Browse the repository at this point in the history
* Update pattern for testing scopes

Fixes #43895

* spotless
  • Loading branch information
billwert authored Feb 5, 2025
1 parent a7266d9 commit 15ca25c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public final class ScopeUtil {

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.identity.implementation;

import com.azure.identity.implementation.util.ScopeUtil;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ScopeUtilTests {

@ParameterizedTest
@MethodSource("validScopes")
public void validScopes(String scope) {
assertDoesNotThrow(() -> ScopeUtil.validateScope(scope));

}

@ParameterizedTest
@MethodSource("invalidScopes")
public void validInvalidScopes(String scope) {
assertThrows(IllegalArgumentException.class, () -> ScopeUtil.validateScope(scope));

}

static Stream<String> validScopes() {
return Stream.of("https://vaults.azure.net/.default", "https://management.core.windows.net//.default",
"https://graph.microsoft.com/User.Read", "api://app-id-has-hyphens-and-1234567890s/user_impersonation");
}

static Stream<String> invalidScopes() {
return Stream.of("api://app-id-has-hyphens-and-1234567890s/invalid scope",
"api://app-id-has-hyphens-and-1234567890s/invalid\"scope",
"api://app-id-has-hyphens-and-1234567890s/invalid\\scope");
}
}

0 comments on commit 15ca25c

Please sign in to comment.