Skip to content

Commit

Permalink
Merge 572ab27 into 48213c7
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy-Lu committed Jan 23, 2019
2 parents 48213c7 + 572ab27 commit c622c34
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public RestClient getRestClient() {
public boolean login() throws Exception {
boolean ret;
try {
ret = AuthenticationTool.authenticate(restClient, almLoginInfo.getUserName(),
ret = AuthenticationTool.getInstance().authenticate(restClient, almLoginInfo.getUserName(),
almLoginInfo.getPassword(), almLoginInfo.getServerUrl(), almLoginInfo.getClientType(), _logger);
} catch (Exception cause) {
ret = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public AUTEnvironmentBuilderPerformer(

public void start() {
try {
if (AuthenticationTool.authenticate(getClient(),
if (AuthenticationTool.getInstance().authenticate(getClient(),
model.getAlmUserName(),
model.getAlmPassword(),
model.getAlmServerUrl(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Testsuites execute(RestClient client, Args args, Logger logger)
Testsuites ret = null;
_logger = logger;
_running = true;
if (AuthenticationTool.authenticate(client, args.getUsername(), args.getPassword(), args.getUrl(), args.getClientType(), logger)) {
if (AuthenticationTool.getInstance().authenticate(client, args.getUsername(), args.getPassword(), args.getUrl(), args.getClientType(), logger)) {
initialize(args, client);
if (start(args)) {
_polling = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
*
* Certain versions of software and/or documents (“Material”) accessible here may contain branding from
* Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017,
* the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP
* and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE
* marks are the property of their respective owners.
* __________________________________________________________________
* MIT License
*
* © Copyright 2012-2018 Micro Focus or one of its affiliates.
*
* The only warranties for products and services of Micro Focus and its affiliates
* and licensors (“Micro Focus”) are set forth in the express warranty statements
* accompanying such products and services. Nothing herein should be construed as
* constituting an additional warranty. Micro Focus shall not be liable for technical
* or editorial errors or omissions contained herein.
* The information contained herein is subject to change without notice.
* ___________________________________________________________________
*
*/

package com.microfocus.application.automation.tools.sse.sdk.authenticator;

import com.microfocus.adm.performancecenter.plugins.common.rest.RESTConstants;
import com.microfocus.application.automation.tools.sse.sdk.Client;
import com.microfocus.application.automation.tools.sse.sdk.Logger;
import com.microfocus.application.automation.tools.sse.sdk.ResourceAccessLevel;
import com.microfocus.application.automation.tools.sse.sdk.Response;

import java.util.HashMap;
import java.util.Map;

/**
* For ALM1260sso and 15sso, there's a different API to authenticated with api key.
*/
public class ApiKeyAuthenticator implements Authenticator {

private static final String APIKEY_LOGIN_API = "rest/oauth2/login";
private static final String CLIENT_TYPE = "ALM-CLIENT-TYPE";

@Override
public boolean login(Client client, String clientId, String secret, String clientType, Logger logger) {
logger.log("Start login to ALM server with APIkey...");
Map<String, String> headers = new HashMap<String, String>();
headers.put(CLIENT_TYPE, clientType);
headers.put(RESTConstants.ACCEPT, "application/json");
headers.put(RESTConstants.CONTENT_TYPE, "application/json");

Response response =
client.httpPost(
client.build(APIKEY_LOGIN_API),
String.format("{clientId:%s, secret:%s}", clientId, secret).getBytes(),
headers,
ResourceAccessLevel.PUBLIC);
boolean result = response.isOk();
logger.log(
result ? String.format(
"Logged in successfully to ALM Server %s using %s",
client.getServerUrl(),
clientId)
: String.format(
"Login to ALM Server at %s failed. Status Code: %s",
client.getServerUrl(),
response.getStatusCode()));
return result;
}

@Override
public boolean logout(Client client, String username) {
// No logout
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,43 @@

package com.microfocus.application.automation.tools.sse.sdk.authenticator;

import com.microfocus.adm.performancecenter.plugins.common.rest.RESTConstants;
import com.microfocus.application.automation.tools.common.SSEException;
import com.microfocus.application.automation.tools.sse.sdk.Client;
import com.microfocus.application.automation.tools.sse.sdk.Logger;
import com.microfocus.application.automation.tools.sse.sdk.ResourceAccessLevel;
import com.microfocus.application.automation.tools.sse.sdk.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Unify the rest authentication process here from separated part, ALMRestTool and RunManager.
* Any authentication change will only need to change here.
* Created by llu2 on 4/5/2017.
*/
public final class AuthenticationTool {
public class AuthenticationTool {

private static List<Authenticator> authenticators;
private List<Authenticator> authenticators;
private static AuthenticationTool instance;

private AuthenticationTool() {
// Add the private constructor to hide the implicit public one.
}

static {
authenticators = new ArrayList<>();
authenticators.add(new RestAuthenticator());
authenticators.add(new ApiKeyAuthenticator());
}

/**
* Try authenticate use a list of authenticators and then create session.
*/
public static boolean authenticate(Client client, String username, String password, String url, String clientType, Logger logger) {
if (login(client, username, password, url, logger)) {
appendQCSessionCookies(client, clientType, logger);
return true;
public static synchronized AuthenticationTool getInstance() {
if (instance == null) {
instance = new AuthenticationTool();
}
return false;
return instance;
}

private static boolean login(Client client, String username, String password, String url, Logger logger) {
/**
* Try authenticate use a list of authenticators and then create session.
*/
public boolean authenticate(Client client, String username, String password, String url, String clientType, Logger logger) {
boolean result = false;
for(Authenticator authenticator : authenticators) {
try {
result = authenticator.login(client, username, password, logger);
result = authenticator.login(client, username, password, clientType, logger);
if (result) {
break;
}
Expand All @@ -80,30 +71,4 @@ private static boolean login(Client client, String username, String password, St
}
return result;
}

private static void appendQCSessionCookies(Client client, String clientType, Logger logger) {
logger.log("Creating session...");

Map<String, String> headers = new HashMap<String, String>();
headers.put(RESTConstants.CONTENT_TYPE, RESTConstants.APP_XML);
headers.put(RESTConstants.ACCEPT, RESTConstants.APP_XML);

// issue a post request so that cookies relevant to the QC Session will be added to the RestClient
Response response =
client.httpPost(
client.build("rest/site-session"),
generateClientTypeData(clientType),
headers,
ResourceAccessLevel.PUBLIC);
if (!response.isOk()) {
throw new SSEException("Cannot append QCSession cookies", response.getFailure());
} else {
logger.log("Session created.");
}
}

private static byte[] generateClientTypeData(String clientType) {
String data = String.format("<session-parameters><client-type>%s</client-type></session-parameters>", clientType);
return data.getBytes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public interface Authenticator {

boolean login(Client client, String username, String password, Logger logger);
boolean login(Client client, String username, String password, String clientType, Logger logger);
boolean logout(Client client, String username);

}
Loading

0 comments on commit c622c34

Please sign in to comment.