As OAuth Token is currently used in most of the REST services for authentication purpose, it is little tricky to have LR VUGEN HTTP Script ready with the test data. So we can go for JAVA protocol and use the below codes. Discuss with the development team and get algorithms which are getting used and change the below code accordingly

 

/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description:
*
*/

//Set ClassPath for below jar file in RunTime Settings
//javax-crypto.jar
//xml-security-impl-1.0.jar
//spring-web-3.1.0.RELEASE.jar

import lrapi.lr;
import lrapi.web;

import javax.crypto.Mac;
import java.util.UUID;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.net.URLEncoder;

public class Actions
{
private static final String HMAC_SHA256_ALGORITHM=”HmacSHA256″;
private static final String AUTHENTICATION = “Authentication”;
private static final String AUTHORIZATION = “Authorization”;
private static final String API_KEY = “X-ABCD-API-KEY”;
private static final String GRANT_TYPE = “grant_type”;
private static final String SCOPE = “scope”;
private static final String HEADER_ACCOUNT_NO = “account_no”;
private static final String DOCUMENT_BODY = “document_body”;
private static final String DOCUMENT_NAME = “document_name”;
private static final String DOCUMENT_TYPE = “document_type”;
private static final String DOCUMENT_FORMAT = “document_format”;
private static final String DOCUMENT_M5_CHECKSUM = “document_md5_checksum”;
private static final String DOCUMENT_DATE = “document_date”;
private static final String DOCUMENT_KEY = “documentKey”;
private static final String DOCUMENT_FILE_NAME = “document_file_name”;

public int init() throws Throwable
{
return 0;
}//end of init
public int action() throws Throwable
{

String client_id = “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx”; //lr.eval_string(“{P_ClientID}”);
String client_secret = “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx”; //lr.eval_string(“{P_ClientSecret}”);
String grant_type = “client_credentials”;
String macToken=””;
String access_token=””;
String authorization_header = null;
String strAccountNumber=null;
String fileName=null;
String date1=null;
String serviceStatusCode = null;

macToken = generateMacToken(client_id,client_secret,grant_type);

web.set_sockets_option(“SSL_VERSION”,”TLS”);
web.add_auto_header(“Content-Type”,”application/x-www-form-urlencoded”);
//web.add_auto_header(“Authentication”,”MAC id=\”xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx\”,ts=\”1458797751846\”,nonce=\”xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx:ABCD\”,mac=\”xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\””);
web.add_auto_header(“Authentication”, macToken.toString());
web.add_auto_header(“X-ABCD-API-KEY”,”xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx”);

web.reg_save_param(“C_AccessToken”,new String[]{“LB=\”access_token\”:\””,”RB=\”,\n”,”LAST”});
web.reg_save_param(“C_MacKey”,new String[]{“LB=\”mac_key\”:\””,”RB=\”,\n”,”LAST”});

web.custom_request(“web.custom_request”,
“Method=POST”,
new String[]{“URL=https://test.environment.com/ssssssssss/v2/oauth/token/mac”,
“TargetFrame=”,
“Resource=0”,
“Referer=”,
“Body=grant_type=client_credentials&scope=”,
“LAST”});

macToken = lr.eval_string(“{C_MacKey}”);
access_token = lr.eval_string(“{C_AccessToken}”);
String service_uri = “/xxxxxxxxxxxxxx/xx/xxxxxxxxxxxxxxx/xxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
String host_name = “test.environment.com”;
authorization_header = generateMacAuthToken(client_id,macToken,access_token,service_uri,host_name,”443″,”POST”);

web.set_sockets_option(“SSL_VERSION”,”TLS”);
web.add_auto_header(“Content-Type”,”multipart/form-data”);
//web.add_auto_header(“Authorization”,”MAC id=\”xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx\”,ts=\”1458797868653\”,nonce=\”xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx:ABCD\”,mac=\”xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx””);
web.add_auto_header(“Authorization”,authorization_header.toString());
web.add_auto_header(“account_no”,”xxxxxxxxxxxxx”);
web.add_auto_header(“X-ABCD-API-KEY”,”xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx”);

web.custom_request(“web.custom_request”,
“Method=POST”,
new String[]{
“URL=https://test.environment.com/ssssssssssssssss/v1/dddddddddd/eeeeeeeee/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,
“TargetFrame=”,
“Resource=0”,
“Referer=”,
“Body=document_body=B@1b15887&document_name=DocumentName&document_file_name=fileName.pdf&document_type=cc&document_format=PDF&document_md5_checksum=ddddddddddddddddddddddddddddddd&document_date=1457931150709”,
“LAST”});

return 0;
}//end of action

public static final String generateMacToken(String client_id,String client_secret,String granttype) throws Exception{
//java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());
String uuid= UUID.randomUUID().toString();
////System.out.println(“uudi – >” + uuid);
String nonce = uuid+”:ABCD”;
////System.out.println(“nonce – >” + nonce);
String ts = String.valueOf(((long)System.currentTimeMillis()));
////System.out.println(“ts – >” + ts);
String tsn = ts + “\n”;
String noncen = nonce + “\n”;
//base string
String baseString = client_id+”\n”+tsn+noncen+granttype+”\n”;
////System.out.println(“baseString – >” + baseString);
//Generate signature using client secret
SecretKeySpec signingKey = new SecretKeySpec(client_secret.getBytes(“UTF-8”), HMAC_SHA256_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
mac.init(signingKey);
byte[] signatureBytes = mac.doFinal(baseString.getBytes(“UTF-8”));
String signatureStr=Base64.encode(signatureBytes);
////System.out.println(“signatureStr – >” + signatureStr);
String mac_authentication_header = “MAC id=\””+client_id+”\”,ts=\””+ts+”\”,nonce=\””+nonce+”\”,mac=\””+signatureStr+”\””;
System.out.println(“The value of mac_authentication_header –>” + mac_authentication_header);
return mac_authentication_header;
}

public static final String generateMacAuthToken(String client_id,String mac_key,String access_token,String resourcePath,String host,String port,String httpMethod ) throws Exception{
String uuid= UUID.randomUUID().toString();
String nonce = uuid+”:AAAA”;
String ts = String.valueOf(((long)System.currentTimeMillis()));
String tsn = ts + “\n”;
String noncen = nonce + “\n”;
String hostn = host+”\n”;
String portn = port+”\n”;
String resourcePathn = URLEncoder.encode(resourcePath)+”\n”;
String httpMethodn = httpMethod+”\n”;
//base string
String baseString = tsn+noncen+httpMethodn+resourcePathn+hostn+portn+”\n”;
//Generate signature using client secret
SecretKeySpec signingKey = new SecretKeySpec(mac_key.getBytes(“UTF-8”), HMAC_SHA256_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
mac.init(signingKey);
byte[] signatureBytes = mac.doFinal(baseString.getBytes(“UTF-8”));
String signatureStr=Base64.encode(signatureBytes);
String mac_authorization_header = “MAC id=\””+access_token+”\”,ts=\””+ts+”\”,nonce=\””+nonce+”\”,mac=\””+signatureStr+”\””;
return mac_authorization_header;
}
public int end() throws Throwable
{
return 0;
}//end of end
}

Advertisements