Reputation: 751
I'm trying to Functional test a few APIs that need to be authenticated (OAuth 2.0) and simulate this in JMeter.
I'm trying to authenticate the OAuth service for Azure cloud. Has anyone out there been able to successfully create JMeter HTTP requests to authenticate against OAuth 2.0?
Upvotes: 10
Views: 48528
Reputation: 37
As a part of API Test automation, we did created native client ID, assign the required resources to native client.
All you need adal4j-1.6.X.jar
public static AuthenticationResult getAuthToken(String username, String password,
String clientId, String authority, String tenant, String urii) throws Throwable {
AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
crypToUtil td= new crypToUtil();
crypToUtil cryptoUtil = new crypToUtil();
password = cryptoUtil.decrypt(password);
try {
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(authority + tenant + "/", true,service);
Future<AuthenticationResult> future = context.acquireToken(urii, clientId, username, password,null);
result = future.get();
} catch (ExecutionException | MalformedURLException e) {
throw e.getCause();
} finally {
service.shutdown();
}
if (result == null) {
throw new ServiceUnavailableException("authentication result was null, could be your input data were wrong ...");
}
return result;
}
Upvotes: 2
Reputation: 168092
Basically you need to add HTTP Header Manager to send Authorization
header with the value of Bearer ${ACCESS_TOKEN}
in order to make authenticated OAuth API calls.
Access token can be obtained in 2 major ways:
Implement OAuth2 flow in your test, i.e. :
In regards to implementing option 2 - it will require 3 separate JMeter samplers (or alternatively you can get the access token programmatically via JSR223 Sampler)
References:
Upvotes: 11