Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaliMatharaarachchi committed Oct 19, 2023
1 parent f96524a commit b3dcd3f
Show file tree
Hide file tree
Showing 8 changed files with 554 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ public class AuthenticationContext {
private String subscriberTenantDomain;
private String spikeArrestUnit;
private boolean stopOnQuotaReach;
private String productName;
private String productProvider;
private String apiName;
private String apiPublisher;
private String apiVersion;
private String apiUUID;
private String rawToken;
private String tokenType;
Expand Down Expand Up @@ -267,45 +263,6 @@ public void setStopOnQuotaReach(boolean stopOnQuotaReach) {
this.stopOnQuotaReach = stopOnQuotaReach;
}

public void setProductName(String productName) {
this.productName = productName;
}

/**
* Matched API Product Name (If the request is from API Product)
*
* @return API Product Name
*/
public String getProductName() {
return productName;
}

public void setProductProvider(String productProvider) {
this.productProvider = productProvider;
}

/**
* API Product Provider of the matched API.
*
* @return API Product provider.
*/
public String getProductProvider() {
return productProvider;
}

/**
* API Name of the matched API.
*
* @return API Name
*/
public String getApiName() {
return apiName;
}

public void setApiName(String apiName) {
this.apiName = apiName;
}

/**
* API Publisher of the matched API.
*
Expand All @@ -319,19 +276,6 @@ public void setApiPublisher(String apiPublisher) {
this.apiPublisher = apiPublisher;
}

/**
* API Version of the matched API
*
* @return API Version
*/
public String getApiVersion() {
return apiVersion;
}

public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}

/**
* API UUID of the corresponding API.
*
Expand Down
3 changes: 3 additions & 0 deletions gateway/enforcer/org.wso2.apk.enforcer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ dependencies {
implementation libs.protobuf.java
// Test dependencites
testImplementation libs.junit
testImplementation libs.mockito.core
testImplementation libs.powermock.api.mockito2
testImplementation libs.powermock.module.junit4
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ private JWTValidationInfo getJwtValidationInfo(String jwtToken, String organizat
CacheProviderUtil.getOrganizationCache(organization).getGatewayKeyCache().invalidate(signature);
CacheProviderUtil.getOrganizationCache(organization).getInvalidTokenCache().put(signature, true);
throw new APISecurityException(APIConstants.StatusCodes.UNAUTHENTICATED.getCode(),
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS,
APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
APISecurityConstants.API_AUTH_ACCESS_TOKEN_EXPIRED,
APISecurityConstants.API_AUTH_ACCESS_TOKEN_EXPIRED_MESSAGE);
}
} else if (CacheProviderUtil.getOrganizationCache(organization).getInvalidTokenCache()
.getIfPresent(signature) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.wso2.apk.enforcer.config.ConfigHolder;
import org.wso2.apk.enforcer.config.dto.ExtendedTokenIssuerDto;
import org.wso2.apk.enforcer.constants.APIConstants;
import org.wso2.apk.enforcer.constants.APISecurityConstants;
import org.wso2.apk.enforcer.security.jwt.SignedJWTInfo;
import org.wso2.apk.enforcer.util.JWKSClient;
import org.wso2.apk.enforcer.util.JWTUtils;
Expand Down Expand Up @@ -94,15 +95,16 @@ public JWTValidationInfo validateToken(String token, SignedJWTInfo signedJWTInfo
jwtValidationInfo.setToken(token);
return jwtValidationInfo;
}
jwtValidationInfo.setValidationCode(APISecurityConstants.API_AUTH_ACCESS_TOKEN_EXPIRED);
logger.debug("Token is expired.");
} else {
jwtValidationInfo.setValidationCode(APIConstants.KeyValidationStatus.API_AUTH_INVALID_CREDENTIALS);
logger.debug("Token signature is invalid.");
}
} catch (ParseException | JWTGeneratorException e) {
throw new EnforcerException("Error while parsing JWT", e);
}
jwtValidationInfo.setValid(false);
jwtValidationInfo.setValidationCode(APIConstants.KeyValidationStatus.API_AUTH_INVALID_CREDENTIALS);
return jwtValidationInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ public AuthenticationContext authenticate(RequestContext requestContext) throws
String apiUUID = requestContext.getMatchedAPI().getUuid();

authenticationContext.setAuthenticated(authenticated);
authenticationContext.setApiName(apiName);
authenticationContext.setApiUUID(apiUUID);
authenticationContext.setApiVersion(apiVersion);

return authenticationContext;
} finally {
Expand Down
Loading

0 comments on commit b3dcd3f

Please sign in to comment.