Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#638 #639

Merged
merged 4 commits into from
Mar 25, 2024
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
34 changes: 15 additions & 19 deletions oxd-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,32 +161,28 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-common</artifactId>
<version>2.0b1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit-cssparser</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit3-driver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
103 changes: 36 additions & 67 deletions oxd-common/src/test/java/org/gluu/oxd/common/SeleniumTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import org.apache.commons.collections.CollectionUtils;
import org.gluu.oxauth.client.AuthorizationRequest;
import org.gluu.oxauth.client.AuthorizationResponse;
import org.gluu.oxauth.model.common.AuthorizationMethod;
import org.gluu.oxauth.model.common.Holder;
import org.gluu.oxauth.model.common.Prompt;
import org.gluu.oxauth.model.common.ResponseType;
import org.gluu.oxauth.model.util.Util;
import org.gluu.oxd.common.model.AuthenticationDetails;
import org.openqa.selenium.*;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.interactions.Actions;
Expand All @@ -31,84 +34,53 @@ public class SeleniumTestUtils {
private static int WAIT_OPERATION_TIMEOUT = 30;
private static final Logger LOG = LoggerFactory.getLogger(SeleniumTestUtils.class);

public static AuthorizationResponse authorizeClient(
String opHost, String userId, String userSecret, String clientId, String redirectUrls, String state, String nonce, List<String> responseTypes, List<String> scopes) {
public static AuthorizationResponse authorizeClient(AuthenticationDetails authenticationDetails, List<String> responseTypes, List<String> scopes) {
WebDriver driver = initWebDriver(true, true);

loginGluuServer(driver, opHost, userId, userSecret, clientId, redirectUrls, state, nonce, responseTypes, scopes);
AuthorizationResponse authorizationResponse = acceptAuthorization(driver);
AuthorizationResponse authorizationResponse = loginGluuServer(driver, authenticationDetails, responseTypes, scopes);
//AuthorizationResponse authorizationResponse = acceptAuthorization(driver);

driver.quit();
return authorizationResponse;
}

private static void loginGluuServer(
WebDriver driver, String opHost, String userId, String userSecret, String clientId, String redirectUrls, String state, String nonce, List<String> responseTypes, List<String> scopes) {
private static AuthorizationResponse loginGluuServer(
WebDriver driver, AuthenticationDetails authenticationDetails, List<String> responseTypes, List<String> scopes) {
//navigate to opHost
driver.navigate().to(getAuthorizationUrl(opHost, clientId, redirectUrls, state, nonce, responseTypes, scopes));

String authzUrl = getAuthorizationUrl(authenticationDetails, responseTypes, scopes);
driver.navigate().to(authzUrl);

//driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(WAIT_OPERATION_TIMEOUT))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class);
WebElement loginButton = wait.until(new Function<WebDriver, WebElement>() {

WebElement allowButton = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver d) {
//System.out.println(d.getCurrentUrl());
//System.out.println(d.getPageSource());
return d.findElement(By.id("loginButton"));
return d.findElement(By.id("authorizeForm:allowButton"));
}
});
String authorizationResponseStr = driver.getCurrentUrl();
// We have to use JavaScript because target is link with onclick
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("scroll(0, 1000)");

LOG.info("Login page loaded. The current url is: " + driver.getCurrentUrl());
//username field
WebElement usernameElement = driver.findElement(By.id("username"));
usernameElement.sendKeys(userId);
//password field
WebElement passwordElement = driver.findElement(By.id("password"));
passwordElement.sendKeys(userSecret);
//click on login button
String previousURL = driver.getCurrentUrl();

loginButton.click();
Actions actions = new Actions(driver);
actions.click(allowButton).perform();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
authorizationResponseStr = driver.getCurrentUrl();
AuthorizationResponse authorizationResponse = new AuthorizationResponse(authorizationResponseStr);

}
LOG.info("Authorization Response url is: " + driver.getCurrentUrl());

private static AuthorizationResponse acceptAuthorization(WebDriver driver) {
String authorizationResponseStr = driver.getCurrentUrl();
AuthorizationResponse authorizationResponse = null;
// Check for authorization form if client has no persistent authorization
if (!authorizationResponseStr.contains("#")) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(WAIT_OPERATION_TIMEOUT))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class);

WebElement allowButton = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver d) {
//System.out.println(d.getCurrentUrl());
//System.out.println(d.getPageSource());
return d.findElement(By.id("authorizeForm:allowButton"));
}
});

// We have to use JavaScript because target is link with onclick
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("scroll(0, 1000)");

String previousURL = driver.getCurrentUrl();

Actions actions = new Actions(driver);
actions.click(allowButton).perform();

authorizationResponseStr = driver.getCurrentUrl();
authorizationResponse = new AuthorizationResponse(authorizationResponseStr);

LOG.info("Authorization Response url is: " + driver.getCurrentUrl());
} else {
fail("The authorization form was expected to be shown.");
}
return authorizationResponse;

}

private static WebDriver initWebDriver(boolean enableJavascript, boolean cleanupCookies) {
Expand All @@ -125,29 +97,26 @@ private static WebDriver initWebDriver(boolean enableJavascript, boolean cleanup
return currentDriver;
}

private static String getAuthorizationUrl(String opHost, String clientId, String redirectUrls, String state, String nonce, List<String> responseTypes, List<String> scopes) {
private static String getAuthorizationUrl(AuthenticationDetails authenticationDetails, List<String> responseTypes, List<String> scopes) {
try {
if(CollectionUtils.isEmpty(responseTypes)) {
if (CollectionUtils.isEmpty(responseTypes)) {
responseTypes = Lists.newArrayList("code", "id_token", "token");
}

if(CollectionUtils.isEmpty(scopes)) {
if (CollectionUtils.isEmpty(scopes)) {
scopes = Lists.newArrayList("openid", "profile", "oxd", "uma_protection");
}
List<ResponseType> resTypes = responseTypes.stream().map(item -> ResponseType.fromString(item)).collect(Collectors.toList());
AuthorizationRequest authorizationRequest = new AuthorizationRequest(resTypes, clientId, scopes, redirectUrls.split(" ")[0], nonce);
AuthorizationRequest authorizationRequest = new AuthorizationRequest(resTypes, authenticationDetails.getClientId(), scopes, authenticationDetails.getRedirectUrls().split(" ")[0], authenticationDetails.getNonce());
authorizationRequest.setResponseTypes(responseTypes.stream().map(item -> ResponseType.fromString(item)).collect(Collectors.toList()));
authorizationRequest.setState(state);
authorizationRequest.setState(authenticationDetails.getState());
authorizationRequest.addCustomParameter("mail", authenticationDetails.getUserEmail());
authorizationRequest.addCustomParameter("inum", authenticationDetails.getUserInum());
authorizationRequest.getPrompts().add(Prompt.NONE);
authorizationRequest.setAuthorizationMethod(AuthorizationMethod.FORM_ENCODED_BODY_PARAMETER);

return URLDecoder.decode(opHost + "/oxauth/restv1/authorize?" +authorizationRequest.getQueryString(), Util.UTF8_STRING_ENCODING);
return URLDecoder.decode(authenticationDetails.getOpHost() + "/oxauth/restv1/authorize?" + authorizationRequest.getQueryString(), Util.UTF8_STRING_ENCODING);

/*return URLDecoder.decode(opHost + "/oxauth/restv1/authorize?" +
"response_type=code+id_token+token" +
"&state=" + state +
"&nonce=" + nonce +
"&client_id=" + clientId +
"&redirect_uri=" + redirectUrls.split(" ")[0] +
"&scope=openid+profile+oxd+uma_protection", Util.UTF8_STRING_ENCODING);*/
} catch (UnsupportedEncodingException ex) {
fail("Failed to decode the authorization URL.");
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.gluu.oxd.common.model;

public class AuthenticationDetails {
private String host;
private String opHost;
private String redirectUrls;
private String userId;
private String userSecret;
private String userInum;
private String userEmail;
private String state;
private String nonce;
private String clientId;

public String getClientId() {
return clientId;
}

public void setClientId(String clientId) {
this.clientId = clientId;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getOpHost() {
return opHost;
}

public void setOpHost(String opHost) {
this.opHost = opHost;
}

public String getRedirectUrls() {
return redirectUrls;
}

public void setRedirectUrls(String redirectUrls) {
this.redirectUrls = redirectUrls;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getUserSecret() {
return userSecret;
}

public void setUserSecret(String userSecret) {
this.userSecret = userSecret;
}

public String getUserInum() {
return userInum;
}

public void setUserInum(String userInum) {
this.userInum = userInum;
}

public String getUserEmail() {
return userEmail;
}

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getNonce() {
return nonce;
}

public void setNonce(String nonce) {
this.nonce = nonce;
}

@Override
public String toString() {
return "AuthenticationDetails{" +
"host='" + host + '\'' +
", opHost='" + opHost + '\'' +
", redirectUrls='" + redirectUrls + '\'' +
", userId='" + userId + '\'' +
", userSecret='" + userSecret + '\'' +
", userInum='" + userInum + '\'' +
", userEmail='" + userEmail + '\'' +
", state='" + state + '\'' +
", nonce='" + nonce + '\'' +
", clientId='" + clientId + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.client.model.*;
import org.apache.commons.lang.StringUtils;
import org.gluu.oxd.common.CoreUtils;
import org.gluu.oxd.common.model.AuthenticationDetails;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
Expand All @@ -20,14 +21,15 @@
//Set `protect_commands_with_access_token` field to true in oxd-server.yml file
public class DifferentAuthServerTest {

@Parameters({"opHost", "redirectUrls", "authServer", "userId", "userSecret"})
@Parameters({"opHost", "redirectUrls", "authServer", "userId", "userSecret", "userInum", "userEmail"})
@Test(enabled = false)
public void getUserInfo_withDifferentAuthServer(String opHost, String redirectUrls, String authServer, String userId, String userSecret) throws Exception {
public void getUserInfo_withDifferentAuthServer(String opHost, String redirectUrls, String authServer, String userId, String userSecret, String userInum, String userEmail) throws Exception {
final DevelopersApi client = api();

final io.swagger.client.model.RegisterSiteResponse site = RegisterSiteTest.registerSite(client, opHost, redirectUrls);
final io.swagger.client.model.RegisterSiteResponse authServerResp = RegisterSiteTest.registerSite(client, authServer, redirectUrls);
final GetTokensByCodeResponse tokens = requestTokens(client, opHost, site, authServerResp, userId, userSecret, site.getClientId(), redirectUrls);
AuthenticationDetails authenticationDetails = TestUtils.setAuthenticationDetails(null, opHost, userId, userSecret, site.getClientId(), redirectUrls, CoreUtils.secureRandomString(), CoreUtils.secureRandomString(), userInum, userEmail);
final GetTokensByCodeResponse tokens = requestTokens(client, site, authServerResp, authenticationDetails);

final io.swagger.client.model.GetUserInfoParams params = new GetUserInfoParams();
params.setOxdId(site.getOxdId());
Expand Down Expand Up @@ -92,14 +94,14 @@ public static UmaRsCheckAccessResponse checkAccess(DevelopersApi client, Registe
return apiResp.getData();
}

private GetTokensByCodeResponse requestTokens(DevelopersApi client, String opHost, io.swagger.client.model.RegisterSiteResponse site, io.swagger.client.model.RegisterSiteResponse authServer, String userId, String userSecret, String clientId, String redirectUrls) throws Exception {
private GetTokensByCodeResponse requestTokens(DevelopersApi client, io.swagger.client.model.RegisterSiteResponse site, io.swagger.client.model.RegisterSiteResponse authServer, AuthenticationDetails authenticationDetails) throws Exception {

final String state = CoreUtils.secureRandomString();
final String nonce = CoreUtils.secureRandomString();

final io.swagger.client.model.GetTokensByCodeParams params = new GetTokensByCodeParams();
params.setOxdId(site.getOxdId());
params.setCode(GetTokensByCodeTest.codeRequest(client, opHost, site.getOxdId(), userId, userSecret, clientId, redirectUrls, state, nonce, getAuthorization(site)));
params.setCode(GetTokensByCodeTest.codeRequest(client, authenticationDetails, site.getOxdId(), getAuthorization(site)));
params.setState(state);

final GetTokensByCodeResponse resp = client.getTokensByCode(params, getAuthorization(authServer), authServer.getOxdId());
Expand Down
Loading
Loading