From 15ca25c29a2403046867c13f726e86ece0ff4181 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Wed, 5 Feb 2025 15:48:31 -0800 Subject: [PATCH] Update pattern for testing scopes (#44040) * Update pattern for testing scopes Fixes #43895 * spotless --- .../implementation/util/ScopeUtil.java | 4 +- .../implementation/ScopeUtilTests.java | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 sdk/identity/azure-identity/src/test/java/com/azure/identity/implementation/ScopeUtilTests.java diff --git a/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/util/ScopeUtil.java b/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/util/ScopeUtil.java index 23fab0926e5b..dbe2c758eda2 100644 --- a/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/util/ScopeUtil.java +++ b/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/util/ScopeUtil.java @@ -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. @@ -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"); } } diff --git a/sdk/identity/azure-identity/src/test/java/com/azure/identity/implementation/ScopeUtilTests.java b/sdk/identity/azure-identity/src/test/java/com/azure/identity/implementation/ScopeUtilTests.java new file mode 100644 index 000000000000..a826e7cdb652 --- /dev/null +++ b/sdk/identity/azure-identity/src/test/java/com/azure/identity/implementation/ScopeUtilTests.java @@ -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 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 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"); + } +}