Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.azure.test.aad.selenium;

import com.azure.test.utils.AppRunner;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import static com.azure.test.aad.AADTestUtils.AAD_MULTI_TENANT_CLIENT_ID;
import static com.azure.test.aad.AADTestUtils.AAD_MULTI_TENANT_CLIENT_SECRET;
import static com.azure.test.aad.AADTestUtils.AAD_TENANT_ID_1;
import static com.azure.test.aad.AADTestUtils.AAD_USER_NAME_1;
import static com.azure.test.aad.AADTestUtils.AAD_USER_PASSWORD_1;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

public class AADSeleniumITHelper {

private final String username;
private final String password;
private final AppRunner app;
private final WebDriver driver;
private static final Map<String, String> DEFAULT_PROPERTIES = new HashMap<>();

static {
DEFAULT_PROPERTIES.put("azure.activedirectory.tenant-id", System.getenv(AAD_TENANT_ID_1));
DEFAULT_PROPERTIES.put("azure.activedirectory.client-id", System.getenv(AAD_MULTI_TENANT_CLIENT_ID));
DEFAULT_PROPERTIES.put("azure.activedirectory.client-secret", System.getenv(AAD_MULTI_TENANT_CLIENT_SECRET));
DEFAULT_PROPERTIES.put("azure.activedirectory.user-group.allowed-groups", "group1");
DEFAULT_PROPERTIES.put("azure.activedirectory.post-logout-redirect-uri", "http://localhost:${server.port}");

final String directory = "src/test/resources/driver/";
final String chromedriverLinux = "chromedriver_linux64";
final String chromedriverWin32 = "chromedriver_win32.exe";
final String chromedriverMac = "chromedriver_mac64";
String osName = System.getProperty("os.name").toLowerCase();
Process process = null;
try {
File dir = new File(directory);
if (Pattern.matches("linux.*", osName)) {
process = Runtime.getRuntime().exec("chmod +x " + chromedriverLinux, null, dir);
process.waitFor();
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverLinux);
} else if (Pattern.matches("windows.*", osName)) {
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverWin32);
} else if (Pattern.matches("mac.*", osName)) {
process = Runtime.getRuntime().exec("chmod +x " + chromedriverMac, null, dir);
process.waitFor();
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverMac);
} else {
throw new IllegalStateException("Unrecognized osName. osName = " + System.getProperty("os.name"));
}
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
} finally {
if (process != null) {
process.destroy();
}
}
}

public AADSeleniumITHelper(Class<?> appClass, Map<String, String> properties) throws InterruptedException {
username = System.getenv(AAD_USER_NAME_1);
password = System.getenv(AAD_USER_PASSWORD_1);
app = new AppRunner(appClass);
DEFAULT_PROPERTIES.forEach(app::property);
properties.forEach(app::property);

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--incognito", "--no-sandbox", "--disable-dev-shm-usage");
this.driver = new ChromeDriver(options);

this.app.start();
login();
}

private void login() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(this.driver, 10);
driver.get(app.root() + "oauth2/authorization/azure");
wait.until(presenceOfElementLocated(By.name("loginfmt"))).sendKeys(username + Keys.ENTER);
Thread.sleep(10000);
driver.findElement(By.name("passwd")).sendKeys(password + Keys.ENTER);
Thread.sleep(10000);
driver.findElement(By.cssSelector("input[type='submit']")).click();
Thread.sleep(10000);
}

public String httpGet(String endpoint) throws InterruptedException {
driver.get((app.root() + endpoint));
Thread.sleep(1000);
return driver.findElement(By.tagName("body")).getText();
}

public void logoutTest() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.get(app.root() + "logout");
wait.until(presenceOfElementLocated(By.cssSelector("button[type='submit']"))).click();
Thread.sleep(10000);
String cssSelector = "div[data-test-id='" + username + "']";
driver.findElement(By.cssSelector(cssSelector)).click();
Thread.sleep(10000);
String id = driver.findElement(By.cssSelector("div[tabindex='0']")).getAttribute("data-test-id");
Assert.assertEquals(username, id);
}
}

This file was deleted.

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

package com.azure.test.aad.selenium.access.token.scopes;

import com.azure.test.aad.selenium.AADSeleniumITHelper;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class AADAccessTokenScopesIT {

@Test
public void testAccessTokenScopes() throws InterruptedException {
Map<String, String> arguments = new HashMap<>();
arguments.put(
"azure.activedirectory.authorization-clients.office.scopes",
"https://manage.office.com/ActivityFeed.Read, https://manage.office.com/ActivityFeed.ReadDlp, "
+ "https://manage.office.com/ServiceHealth.Read");
arguments.put(
"azure.activedirectory.authorization-clients.graph.scopes",
"https://graph.microsoft.com/User.Read, https://graph.microsoft.com/Directory.AccessAsUser.All");
AADSeleniumITHelper aadSeleniumITHelper = new AADSeleniumITHelper(DumbApp.class, arguments);

String httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/azure");
Assert.assertTrue(httpResponse.contains("profile"));
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/Directory.AccessAsUser.All"));
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/User.Read"));

httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/graph");
Assert.assertTrue(httpResponse.contains("profile"));
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/Directory.AccessAsUser.All"));
Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/User.Read"));

httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/office");
Assert.assertFalse(httpResponse.contains("profile"));
Assert.assertTrue(httpResponse.contains("https://manage.office.com/ActivityFeed.Read"));
Assert.assertTrue(httpResponse.contains("https://manage.office.com/ActivityFeed.ReadDlp"));
Assert.assertTrue(httpResponse.contains("https://manage.office.com/ServiceHealth.Read"));

httpResponse = aadSeleniumITHelper.httpGet("arm");
Assert.assertNotEquals(httpResponse, "arm");
}

@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@SpringBootApplication
@RestController
public static class DumbApp {

@GetMapping(value = "accessTokenScopes/azure")
public Set<String> azure(
@RegisteredOAuth2AuthorizedClient("azure") OAuth2AuthorizedClient authorizedClient) {
return Optional.of(authorizedClient)
.map(OAuth2AuthorizedClient::getAccessToken)
.map(OAuth2AccessToken::getScopes)
.orElse(null);
}

@GetMapping(value = "accessTokenScopes/graph")
public Set<String> graph(
@RegisteredOAuth2AuthorizedClient("graph") OAuth2AuthorizedClient authorizedClient) {
return Optional.of(authorizedClient)
.map(OAuth2AuthorizedClient::getAccessToken)
.map(OAuth2AccessToken::getScopes)
.orElse(null);
}

@GetMapping(value = "accessTokenScopes/office")
public Set<String> office(
@RegisteredOAuth2AuthorizedClient("office") OAuth2AuthorizedClient authorizedClient) {
return Optional.of(authorizedClient)
.map(OAuth2AuthorizedClient::getAccessToken)
.map(OAuth2AccessToken::getScopes)
.orElse(null);
}

@GetMapping(value = "arm")
public String arm(
@RegisteredOAuth2AuthorizedClient("arm") OAuth2AuthorizedClient authorizedClient) {
return "arm";
}
}

}
Loading