use of org.wso2.carbon.apimgt.gateway.handlers.security.authenticator.InternalAPIKeyAuthenticator in project carbon-apimgt by wso2.
the class InternalAPIKeyAuthenticatorTest method testAuthenticateMissingToken.
@Test
public void testAuthenticateMissingToken() {
PowerMockito.when(GatewayUtils.isInternalKey(Mockito.any(JWTClaimsSet.class))).thenReturn(true);
InternalAPIKeyAuthenticator internalAPIKeyAuthenticator = new InternalAPIKeyAuthenticator(APIMgtGatewayConstants.INTERNAL_KEY);
MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
API api = new API();
PowerMockito.when(GatewayUtils.getAPI(messageContext)).thenReturn(api);
TreeMap transportHeaders = new TreeMap();
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
AuthenticationResponse authenticate = internalAPIKeyAuthenticator.authenticate(messageContext);
Assert.assertNotNull(authenticate);
Assert.assertFalse(authenticate.isMandatoryAuthentication());
Assert.assertFalse(authenticate.isAuthenticated());
Assert.assertTrue(authenticate.isContinueToNextAuthenticator());
Assert.assertEquals(authenticate.getErrorCode(), APISecurityConstants.API_AUTH_INVALID_CREDENTIALS);
Assert.assertEquals(authenticate.getErrorMessage(), APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.authenticator.InternalAPIKeyAuthenticator in project carbon-apimgt by wso2.
the class APIAuthenticationHandler method initializeAuthenticators.
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "LEST_LOST_EXCEPTION_STACK_TRACE", justification = "The exception needs to thrown for fault sequence invocation")
protected void initializeAuthenticators() {
isAuthenticatorsInitialized = true;
boolean isOAuthProtected = false;
boolean isMutualSSLProtected = false;
boolean isBasicAuthProtected = false;
boolean isApiKeyProtected = false;
boolean isMutualSSLMandatory = false;
boolean isOAuthBasicAuthMandatory = false;
// Set security conditions
if (apiSecurity == null) {
isOAuthProtected = true;
} else {
String[] apiSecurityLevels = apiSecurity.split(",");
for (String apiSecurityLevel : apiSecurityLevels) {
if (apiSecurityLevel.trim().equalsIgnoreCase(APIConstants.DEFAULT_API_SECURITY_OAUTH2)) {
isOAuthProtected = true;
} else if (apiSecurityLevel.trim().equalsIgnoreCase(APIConstants.API_SECURITY_MUTUAL_SSL)) {
isMutualSSLProtected = true;
} else if (apiSecurityLevel.trim().equalsIgnoreCase(APIConstants.API_SECURITY_BASIC_AUTH)) {
isBasicAuthProtected = true;
} else if (apiSecurityLevel.trim().equalsIgnoreCase(APIConstants.API_SECURITY_MUTUAL_SSL_MANDATORY)) {
isMutualSSLMandatory = true;
} else if (apiSecurityLevel.trim().equalsIgnoreCase(APIConstants.API_SECURITY_OAUTH_BASIC_AUTH_API_KEY_MANDATORY)) {
isOAuthBasicAuthMandatory = true;
} else if (apiSecurityLevel.trim().equalsIgnoreCase((APIConstants.API_SECURITY_API_KEY))) {
isApiKeyProtected = true;
}
}
}
if (!isMutualSSLProtected && !isOAuthBasicAuthMandatory) {
isOAuthBasicAuthMandatory = true;
}
if (!isBasicAuthProtected && !isOAuthProtected && !isMutualSSLMandatory && !isApiKeyProtected) {
isMutualSSLMandatory = true;
}
// Set authenticators
if (isMutualSSLProtected) {
Authenticator authenticator = new MutualSSLAuthenticator(apiLevelPolicy, isMutualSSLMandatory, certificateInformation);
authenticator.init(synapseEnvironment);
authenticators.add(authenticator);
}
if (isOAuthProtected) {
Authenticator authenticator = new OAuthAuthenticator(authorizationHeader, isOAuthBasicAuthMandatory, removeOAuthHeadersFromOutMessage);
authenticator.init(synapseEnvironment);
authenticators.add(authenticator);
}
if (isBasicAuthProtected) {
Authenticator authenticator = new BasicAuthAuthenticator(authorizationHeader, isOAuthBasicAuthMandatory, apiLevelPolicy);
authenticator.init(synapseEnvironment);
authenticators.add(authenticator);
}
if (isApiKeyProtected) {
Authenticator authenticator = new ApiKeyAuthenticator(APIConstants.API_KEY_HEADER_QUERY_PARAM, apiLevelPolicy, isOAuthBasicAuthMandatory);
authenticator.init(synapseEnvironment);
authenticators.add(authenticator);
}
Authenticator authenticator = new InternalAPIKeyAuthenticator(APIMgtGatewayConstants.INTERNAL_KEY);
authenticator.init(synapseEnvironment);
authenticators.add(authenticator);
authenticators.sort(new Comparator<Authenticator>() {
@Override
public int compare(Authenticator o1, Authenticator o2) {
return (o1.getPriority() - o2.getPriority());
}
});
}
Aggregations