Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisaltennakoon committed Sep 6, 2023
1 parent a8bcbd5 commit d13fdfc
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public InboundWebSocketProcessor() {
* @return InboundProcessorResponseDTO with handshake processing response
*/
public InboundProcessorResponseDTO handleHandshake(FullHttpRequest req, ChannelHandlerContext ctx,
InboundMessageContext inboundMessageContext) throws APIManagementException {
InboundMessageContext inboundMessageContext) {

InboundProcessorResponseDTO inboundProcessorResponseDTO;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,16 @@
import java.util.Map;
import java.util.HashMap;

public class ApiKeyAuthenticator implements Authenticator {

private static final Log log = LogFactory.getLog(org.wso2.carbon.apimgt.gateway.inbound.websocket.utils.ApiKeyAuthenticator.class);
public class ApiKeyAuthenticator {

private static final Log log = LogFactory.getLog(ApiKeyAuthenticator.class);
private Boolean jwtGenerationEnabled = null;
private AbstractAPIMgtGatewayJWTGenerator apiMgtGatewayJWTGenerator = null;
private ExtendedJWTConfigurationDto jwtConfigurationDto = null;
private Boolean isGatewayTokenCacheEnabled = null;
private static volatile long ttl = -1L;

public InboundProcessorResponseDTO authenticate(InboundMessageContext inboundMessageContext, String authenticationType) throws APISecurityException {
public InboundProcessorResponseDTO authenticate(InboundMessageContext inboundMessageContext) throws APISecurityException {

InboundProcessorResponseDTO inboundProcessorResponseDTO = new InboundProcessorResponseDTO();

Expand All @@ -75,20 +74,20 @@ public InboundProcessorResponseDTO authenticate(InboundMessageContext inboundMes
ServiceReferenceHolder.getInstance().getAPIManagerConfiguration().getJwtConfigurationDto();
}

if (jwtGenerationEnabled == null) {
if (jwtGenerationEnabled == null && jwtConfigurationDto != null) {
jwtGenerationEnabled = jwtConfigurationDto.isEnabled();
}


if (apiMgtGatewayJWTGenerator == null) {
if (apiMgtGatewayJWTGenerator == null && jwtConfigurationDto != null) {
apiMgtGatewayJWTGenerator = ServiceReferenceHolder.getInstance().getApiMgtGatewayJWTGenerator()
.get(jwtConfigurationDto.getGatewayJWTGeneratorImpl());
}

String tenantDomain = GatewayUtils.getTenantDomain();
int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);

if (jwtGenerationEnabled) {
if (jwtGenerationEnabled != null && jwtGenerationEnabled) {
// Set certificate to jwtConfigurationDto
if (jwtConfigurationDto.isTenantBasedSigningEnabled()) {
this.jwtConfigurationDto.setPublicCert(SigningUtil.getPublicCertificate(tenantId));
Expand Down Expand Up @@ -516,7 +515,7 @@ private String removeApiKeyFromQueryParameters(String queryParam, String apiKey)
return queryParam;
}

public boolean validateAuthenticationContext(InboundMessageContext inboundMessageContext, AuthenticationContext authenticationContext, String contextHeader) {
private boolean validateAuthenticationContext(InboundMessageContext inboundMessageContext, AuthenticationContext authenticationContext, String contextHeader) {

if (authenticationContext == null || !authenticationContext.isAuthenticated()) {
return false;

Check warning on line 521 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/ApiKeyAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/ApiKeyAuthenticator.java#L521

Added line #L521 was not covered by tests
Expand Down Expand Up @@ -561,7 +560,7 @@ public boolean validateAuthenticationContext(InboundMessageContext inboundMessag
* @param payload The payload of the JWT token
* @return returns true if the JWT token is expired
*/
private static boolean isJwtTokenExpired(JWTClaimsSet payload) {
private boolean isJwtTokenExpired(JWTClaimsSet payload) {

int timestampSkew = (int) OAuthServerConfiguration.getInstance().getTimeStampSkewInSeconds();

Check warning on line 565 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/ApiKeyAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/ApiKeyAuthenticator.java#L565

Added line #L565 was not covered by tests

Expand Down Expand Up @@ -590,7 +589,7 @@ private JWTValidationInfo getJwtValidationInfo(SignedJWTInfo signedJWTInfo) {
return jwtValidationInfo;

Check warning on line 589 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/ApiKeyAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/ApiKeyAuthenticator.java#L586-L589

Added lines #L586 - L589 were not covered by tests
}

public String getContextHeader() {
private String getContextHeader() {
APIManagerConfiguration apimConf = ServiceReferenceHolder.getInstance().getAPIManagerConfiguration();
JWTConfigurationDto jwtConfigDto = apimConf.getJwtConfigurationDto();
return jwtConfigDto.getJwtHeader();

Check warning on line 595 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/ApiKeyAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/ApiKeyAuthenticator.java#L593-L595

Added lines #L593 - L595 were not covered by tests
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.TreeMap;

/**
Expand Down Expand Up @@ -327,12 +326,14 @@ public static InboundProcessorResponseDTO authenticateToken(InboundMessageContex
return authenticateToken(inboundMessageContext, APIConstants.WEBSOCKET_FRAME);
}

public static InboundProcessorResponseDTO authenticateToken(InboundMessageContext inboundMessageContext, String authenticationType) throws APISecurityException {
public static InboundProcessorResponseDTO authenticateToken(InboundMessageContext inboundMessageContext,
String authenticationType) throws APISecurityException {

if (inboundMessageContext.getRequestHeaders().get(WebsocketUtil.authorizationHeader) != null) {
return new OAuthAuthenticator().authenticate(inboundMessageContext, authenticationType);
} else if (inboundMessageContext.getRequestHeaders().get(APIConstants.API_KEY_HEADER_QUERY_PARAM) != null || inboundMessageContext.getApiKeyFromQueryParams() != null) {
return new ApiKeyAuthenticator().authenticate(inboundMessageContext, authenticationType);
} else if (inboundMessageContext.getRequestHeaders().get(APIConstants.API_KEY_HEADER_QUERY_PARAM) != null ||
inboundMessageContext.getApiKeyFromQueryParams() != null) {
return new ApiKeyAuthenticator().authenticate(inboundMessageContext);
} else {
throw new APISecurityException(APISecurityConstants.API_AUTH_GENERAL_ERROR,
APISecurityConstants.API_AUTH_GENERAL_ERROR_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
import java.text.ParseException;
import java.util.List;

public class OAuthAuthenticator implements Authenticator {
public class OAuthAuthenticator {

private static final Log log = LogFactory.getLog(OAuthAuthenticator.class);

public OAuthAuthenticator() {
}

@Override
public InboundProcessorResponseDTO authenticate(InboundMessageContext inboundMessageContext, String authenticationType) throws APISecurityException {

InboundProcessorResponseDTO inboundProcessorResponseDTO = new InboundProcessorResponseDTO();
Expand Down Expand Up @@ -99,8 +98,9 @@ public InboundProcessorResponseDTO authenticate(InboundMessageContext inboundMes
}
validateScopes = !APIConstants.GRAPHQL_API.equals(inboundMessageContext.getElectedAPI().getApiType());
} else {
inboundProcessorResponseDTO.setError(true);
return inboundProcessorResponseDTO;
return InboundWebsocketProcessorUtil.getFrameErrorDTO(

Check warning on line 101 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/OAuthAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/inbound/websocket/utils/OAuthAuthenticator.java#L101

Added line #L101 was not covered by tests
WebSocketApiConstants.FrameErrorConstants.API_AUTH_INVALID_CREDENTIALS,
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE, true);
}
}

Expand Down
Loading

0 comments on commit d13fdfc

Please sign in to comment.