getScope() {
return scope;
}
diff --git a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADOAuth2AutoConfiguration.java b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADOAuth2AutoConfiguration.java
deleted file mode 100644
index ad7b737d3f7be..0000000000000
--- a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADOAuth2AutoConfiguration.java
+++ /dev/null
@@ -1,147 +0,0 @@
-// Copyright (c) Microsoft Corporation. All rights reserved.
-// Licensed under the MIT License.
-
-package com.azure.spring.autoconfigure.aad;
-
-import com.azure.spring.telemetry.TelemetrySender;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.PropertySource;
-import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
-import org.springframework.security.oauth2.client.registration.ClientRegistration;
-import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
-import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
-import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
-import org.springframework.security.oauth2.core.AuthorizationGrantType;
-import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
-import org.springframework.security.oauth2.core.oidc.user.OidcUser;
-import org.springframework.util.Assert;
-import org.springframework.util.ClassUtils;
-
-import javax.annotation.PostConstruct;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-
-import static com.azure.spring.telemetry.TelemetryData.SERVICE_NAME;
-import static com.azure.spring.telemetry.TelemetryData.getClassPackageSimpleName;
-
-/**
- * {@link EnableAutoConfiguration Auto-configuration} for Azure Active Authentication OAuth 2.0.
- *
- * The configuration will be activated when configured:
- * 1. {@literal azure.activedirectory.client-id}
- * 2. {@literal azure.activedirectory.client-secret}
- * 3. {@literal azure.activedirectory.tenant-id}
- * client-id, client-secret, tenant-id used in ClientRegistration.
- * client-id, client-secret also used to get graphApiToken, then get groups.
- *
- * A OAuth2 user service {@link AADOAuth2UserService} will be auto-configured by specifying {@literal
- * azure.activedirectory.user-group.allowed-groups} property.
- */
-@Configuration
-@ConditionalOnResource(resources = "classpath:aad.enable.config")
-@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
-@ConditionalOnProperty(prefix = "azure.activedirectory", value = {"client-id", "client-secret", "tenant-id"})
-@PropertySource(value = "classpath:service-endpoints.properties")
-@EnableConfigurationProperties({ AADAuthenticationProperties.class, ServiceEndpointsProperties.class })
-public class AADOAuth2AutoConfiguration {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(AADOAuth2AutoConfiguration.class);
- private final AADAuthenticationProperties aadAuthenticationProperties;
- private final ServiceEndpointsProperties serviceEndpointsProperties;
-
- public AADOAuth2AutoConfiguration(AADAuthenticationProperties aadAuthProperties,
- ServiceEndpointsProperties serviceEndpointsProperties) {
- this.aadAuthenticationProperties = aadAuthProperties;
- this.serviceEndpointsProperties = serviceEndpointsProperties;
- }
-
- @Bean
- @ConditionalOnProperty(prefix = "azure.activedirectory.user-group", value = "allowed-groups")
- public OAuth2UserService oidcUserService() {
- return new AADOAuth2UserService(aadAuthenticationProperties, serviceEndpointsProperties);
- }
-
- @Bean
- public ClientRegistrationRepository clientRegistrationRepository() {
- return new InMemoryClientRegistrationRepository(azureClientRegistration());
- }
-
- private ClientRegistration azureClientRegistration() {
- String tenantId = aadAuthenticationProperties.getTenantId().trim();
- Assert.hasText(tenantId, "azure.activedirectory.tenant-id should have text.");
- Assert.doesNotContain(tenantId, " ", "azure.activedirectory.tenant-id should not contain ' '.");
- Assert.doesNotContain(tenantId, "/", "azure.activedirectory.tenant-id should not contain '/'.");
-
- String redirectUriTemplate = Optional.of(aadAuthenticationProperties)
- .map(AADAuthenticationProperties::getRedirectUriTemplate)
- .orElse("{baseUrl}/login/oauth2/code/{registrationId}");
-
- List scope = aadAuthenticationProperties.getScope();
- if (!scope.toString().contains(".default")) {
- if (aadAuthenticationProperties.allowedGroupsConfigured()
- && !scope.contains("https://graph.microsoft.com/user.read")
- ) {
- scope.add("https://graph.microsoft.com/user.read");
- LOGGER.warn("scope 'https://graph.microsoft.com/user.read' has been added.");
- }
- if (!scope.contains("openid")) {
- scope.add("openid");
- LOGGER.warn("scope 'openid' has been added.");
- }
- if (!scope.contains("profile")) {
- scope.add("profile");
- LOGGER.warn("scope 'profile' has been added.");
- }
- }
-
- return ClientRegistration.withRegistrationId("azure")
- .clientId(aadAuthenticationProperties.getClientId())
- .clientSecret(aadAuthenticationProperties.getClientSecret())
- .clientAuthenticationMethod(ClientAuthenticationMethod.POST)
- .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
- .redirectUriTemplate(redirectUriTemplate)
- .scope(scope)
- .authorizationUri(
- String.format(
- "https://login.microsoftonline.com/%s/oauth2/v2.0/authorize",
- tenantId
- )
- )
- .tokenUri(
- String.format(
- "https://login.microsoftonline.com/%s/oauth2/v2.0/token",
- tenantId
- )
- )
- .userInfoUri("https://graph.microsoft.com/oidc/userinfo")
- .userNameAttributeName(AADTokenClaim.NAME)
- .jwkSetUri(
- String.format(
- "https://login.microsoftonline.com/%s/discovery/v2.0/keys",
- tenantId
- )
- )
- .clientName("Azure")
- .build();
- }
-
- @PostConstruct
- private void sendTelemetry() {
- if (aadAuthenticationProperties.isAllowTelemetry()) {
- final Map events = new HashMap<>();
- final TelemetrySender sender = new TelemetrySender();
- events.put(SERVICE_NAME, getClassPackageSimpleName(AADOAuth2AutoConfiguration.class));
- sender.send(ClassUtils.getUserClass(getClass()).getSimpleName(), events);
- }
- }
-}
diff --git a/sdk/spring/azure-spring-boot/src/main/resources/META-INF/spring.factories b/sdk/spring/azure-spring-boot/src/main/resources/META-INF/spring.factories
index 8436a89f9eaea..2f189a2583bed 100644
--- a/sdk/spring/azure-spring-boot/src/main/resources/META-INF/spring.factories
+++ b/sdk/spring/azure-spring-boot/src/main/resources/META-INF/spring.factories
@@ -1,14 +1,15 @@
org.springframework.boot.env.EnvironmentPostProcessor=com.azure.spring.cloudfoundry.environment.VcapProcessor
-org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.azure.spring.autoconfigure.cosmos.CosmosAutoConfiguration,\
-com.azure.spring.autoconfigure.cosmos.CosmosRepositoriesAutoConfiguration,\
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+com.azure.spring.aad.implementation.AzureActiveDirectoryAutoConfiguration, \
+com.azure.spring.autoconfigure.aad.AADAuthenticationFilterAutoConfiguration, \
+com.azure.spring.autoconfigure.b2c.AADB2CAutoConfiguration,\
+com.azure.spring.autoconfigure.cosmos.CosmosAutoConfiguration,\
+com.azure.spring.autoconfigure.cosmos.CosmosHealthConfiguration,\
com.azure.spring.autoconfigure.cosmos.CosmosReactiveRepositoriesAutoConfiguration,\
+com.azure.spring.autoconfigure.cosmos.CosmosRepositoriesAutoConfiguration,\
com.azure.spring.autoconfigure.gremlin.GremlinAutoConfiguration,\
com.azure.spring.autoconfigure.gremlin.GremlinRepositoriesAutoConfiguration,\
-com.azure.spring.autoconfigure.aad.AADAuthenticationFilterAutoConfiguration,\
-com.azure.spring.autoconfigure.aad.AADOAuth2AutoConfiguration,\
-com.azure.spring.autoconfigure.b2c.AADB2CAutoConfiguration,\
com.azure.spring.autoconfigure.jms.ServiceBusJMSAutoConfiguration,\
com.azure.spring.autoconfigure.storage.StorageAutoConfiguration,\
-com.azure.spring.autoconfigure.cosmos.CosmosHealthConfiguration,\
com.azure.spring.autoconfigure.storage.StorageHealthConfiguration,\
com.azure.spring.keyvault.KeyVaultHealthConfiguration
diff --git a/sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/aad/AADOAuth2ConfigTest.java b/sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/aad/AADOAuth2ConfigTest.java
index 295c47928c88b..04f7e428605e6 100644
--- a/sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/aad/AADOAuth2ConfigTest.java
+++ b/sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/aad/AADOAuth2ConfigTest.java
@@ -3,6 +3,8 @@
package com.azure.spring.autoconfigure.aad;
+import com.azure.spring.aad.implementation.AzureActiveDirectoryAutoConfiguration;
+import com.azure.spring.aad.implementation.AzureClientRegistrationRepository;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -16,19 +18,18 @@
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
-import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertEquals;
public class AADOAuth2ConfigTest {
private static final String AAD_OAUTH2_MINIMUM_PROPS = "aad-backend-oauth2-minimum.properties";
@@ -56,7 +57,7 @@ public void clear() {
@Test
public void noOAuth2UserServiceBeanCreatedIfPropsNotConfigured() {
final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
- context.register(AADOAuth2AutoConfiguration.class);
+ context.register(AzureActiveDirectoryAutoConfiguration.class);
context.refresh();
exception.expect(NoSuchBeanDefinitionException.class);
@@ -124,37 +125,38 @@ public void testEndpointsPropertiesLoadAndOverridable() {
@Test
public void testScopePropertyConfiguredWithDynamicPermissions() {
- testContext = initTestContext("azure.activedirectory.scope=email");
-
+ testContext = initTestContext("azure.activedirectory.authorization.graph.scope=email");
final Environment environment = testContext.getEnvironment();
- assertThat(environment.getProperty("azure.activedirectory.scope"))
- .isEqualTo("email");
-
- final ClientRegistrationRepository clientRegistrationRepository =
- testContext.getBean(ClientRegistrationRepository.class);
- final ClientRegistration clientRegistration = clientRegistrationRepository.findByRegistrationId("azure");
- final Set createdScopes = clientRegistration.getScopes();
- final Set expectedScopes = new HashSet<>(Arrays.asList("email", "openid", "profile",
- "https://graph.microsoft.com/user.read"));
- assertTrue(createdScopes.equals(expectedScopes));
-
+ assertThat(environment.getProperty("azure.activedirectory.authorization.graph.scope")).isEqualTo("email");
+
+ final AzureClientRegistrationRepository azureClientRegistrationRepository =
+ testContext.getBean(AzureClientRegistrationRepository.class);
+ final ClientRegistration clientRegistration = azureClientRegistrationRepository.findByRegistrationId("azure");
+ final Set actualScopes = clientRegistration.getScopes();
+ final Set expectedScopes = new HashSet<>(Arrays.asList("openid", "profile", "offline_access", "email"));
+ assertEquals(expectedScopes, actualScopes);
}
@Test
public void testScopePropertyConfiguredWithStaticPermissions() {
- testContext = initTestContext("azure.activedirectory.scope=1111/.default");
-
+ testContext = initTestContext("azure.activedirectory.authorization.graph.scope=1111/.default");
final Environment environment = testContext.getEnvironment();
- assertThat(environment.getProperty("azure.activedirectory.scope"))
- .isEqualTo("1111/.default");
-
- final ClientRegistrationRepository clientRegistrationRepository =
- testContext.getBean(ClientRegistrationRepository.class);
- final ClientRegistration clientRegistration = clientRegistrationRepository.findByRegistrationId("azure");
- final Set createdScopes = clientRegistration.getScopes();
- final Set expectedScopes = new HashSet<>(Arrays.asList("1111/.default"));
- assertTrue(createdScopes.equals(expectedScopes));
-
+ assertThat(environment.getProperty("azure.activedirectory.authorization.graph.scope")).isEqualTo("1111/"
+ + ".default");
+
+ final AzureClientRegistrationRepository azureClientRegistrationRepository =
+ testContext.getBean(AzureClientRegistrationRepository.class);
+ final ClientRegistration clientRegistration = azureClientRegistrationRepository.findByRegistrationId("azure");
+ final Set actualScopes = clientRegistration.getScopes();
+ final Set expectedScopes =
+ new HashSet<>(Arrays.asList("openid", "profile", "offline_access", "1111/.default"));
+ assertEquals(expectedScopes, actualScopes);
+
+ final ClientRegistration graphClientRegistration =
+ azureClientRegistrationRepository.findByRegistrationId("graph");
+ final Set graphActualScopes = graphClientRegistration.getScopes();
+ final Set graphExpectedScopes = new HashSet<>(Collections.singletonList("1111/.default"));
+ assertEquals(graphExpectedScopes, graphActualScopes);
}
private AnnotationConfigWebApplicationContext initTestContext(String... environment) {
@@ -167,7 +169,7 @@ private AnnotationConfigWebApplicationContext initTestContext(String... environm
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, environment);
}
- context.register(AADOAuth2AutoConfiguration.class);
+ context.register(AzureActiveDirectoryAutoConfiguration.class);
context.refresh();
return context;