Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp jwt token revision #1814

Merged
merged 10 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adapter/internal/oasparser/model/http_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (swagger *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwapiv1b1.HTTPR
resourceAPIPolicy = concatAPIPolicies(resourceAPIPolicy, nil)
resourceAuthScheme = concatAuthSchemes(resourceAuthScheme, nil)
resourceRatelimitPolicy = concatRateLimitPolicies(resourceRatelimitPolicy, nil)
loggers.LoggerAPI.Error(resourceRatelimitPolicy)

addOperationLevelInterceptors(&policies, resourceAPIPolicy, resourceParams.InterceptorServiceMapping, resourceParams.BackendMapping, httpRoute.Namespace)

loggers.LoggerOasparser.Debugf("Calculating auths for API ..., API_UUID = %v", swagger.UUID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.apk.enforcer.commons.dto;

import com.nimbusds.jwt.JWTClaimsSet;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -31,18 +32,16 @@ public class JWTValidationInfo implements Serializable {
private static final long serialVersionUID = 1L;

private String user;
private String issuer;
private long expiryTime;
private long issuedTime;
private String consumerKey;
private boolean valid;
private List<String> scopes = new ArrayList<>();
private Map<String, Object> claims = new HashMap<>();
private String jti;
private int validationCode;
private String rawPayload;
private String keyManager;
private Boolean isAppToken;
private String identifier;
private JWTClaimsSet jwtClaimsSet;
private String token;

public JWTValidationInfo() {

Expand All @@ -51,47 +50,47 @@ public JWTValidationInfo() {
public JWTValidationInfo(JWTValidationInfo jwtValidationInfo) {

this.user = jwtValidationInfo.getUser();
this.issuer = jwtValidationInfo.getIssuer();
this.expiryTime = jwtValidationInfo.getExpiryTime();
this.consumerKey = jwtValidationInfo.getConsumerKey();
this.valid = jwtValidationInfo.isValid();
this.scopes = jwtValidationInfo.getScopes();
this.claims = jwtValidationInfo.getClaims();
this.jti = jwtValidationInfo.getJti();
this.validationCode = jwtValidationInfo.getValidationCode();
this.rawPayload = jwtValidationInfo.getRawPayload();
this.keyManager = jwtValidationInfo.getKeyManager();
this.isAppToken = jwtValidationInfo.getAppToken();
}

public Boolean getAppToken() {

return isAppToken;
public String getToken() {
return token;
}

public void setAppToken(Boolean appToken) {

isAppToken = appToken;
public void setToken(String token) {
this.token = token;
}

public String getUser() {
public String getIdentifier() {
return identifier;
}

return user;
public void setIdentifier(String identifier) {
this.identifier = identifier;
}

public void setUser(String user) {
public JWTClaimsSet getJwtClaimsSet() {
return jwtClaimsSet;
}

this.user = user;
public void setJwtClaimsSet(JWTClaimsSet jwtClaimsSet) {
this.jwtClaimsSet = jwtClaimsSet;
}

public String getIssuer() {
public String getUser() {

return issuer;
return user;
}

public void setIssuer(String issuer) {
public void setUser(String user) {

this.issuer = issuer;
this.user = user;
}

public long getExpiryTime() {
Expand All @@ -104,16 +103,6 @@ public void setExpiryTime(long expiryTime) {
this.expiryTime = expiryTime;
}

public long getIssuedTime() {

return issuedTime;
}

public void setIssuedTime(long issuedTime) {

this.issuedTime = issuedTime;
}

public boolean isValid() {

return valid;
Expand Down Expand Up @@ -144,16 +133,6 @@ public void setClaims(Map<String, Object> claims) {
this.claims = claims;
}

public String getJti() {

return jti;
}

public void setJti(String jti) {

this.jti = jti;
}

public String getConsumerKey() {

return consumerKey;
Expand All @@ -174,16 +153,6 @@ public void setValidationCode(int validationCode) {
this.validationCode = validationCode;
}

public String getRawPayload() {

return rawPayload;
}

public void setRawPayload(String rawPayload) {

this.rawPayload = rawPayload;
}

public String getKeyManager() {

return keyManager;
Expand Down
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
7 changes: 5 additions & 2 deletions gateway/enforcer/org.wso2.apk.enforcer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ dependencies {
// Added as direct dependency for transitive dependency version upgrades
implementation libs.reactor.netty.http
implementation libs.protobuf.java
// Test dependencites
testImplementation libs.junit

test {
implementation libs.junit
implementation libs.mockito.inline
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void addApis(List<Api> apis) {
logger.debug("Total APIs in new cache: {}", newApis.size());
}
this.apis = newApis;
//todo(amali) check if cache is initialized even if the cache is disabled
CacheProviderUtil.initializeCacheHolder(newApis);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.wso2.apk.enforcer.commons.dto.JWTValidationInfo;
import org.wso2.apk.enforcer.config.ConfigHolder;
import org.wso2.apk.enforcer.config.dto.CacheDto;
import org.wso2.apk.enforcer.security.jwt.SignedJWTInfo;
import org.wso2.apk.enforcer.security.jwt.validator.JWTConstants;

import java.util.concurrent.TimeUnit;
Expand All @@ -33,10 +32,13 @@
* Class for initiating and returning caches.
*/
public class CacheProvider {
private LoadingCache<String, SignedJWTInfo> gatewaySignedJWTParseCache;
private LoadingCache<String, String> gatewayTokenCache;
// gatewayKeyCache contains valid tokens -> key: token, value: JWTValidationInfo
private LoadingCache<String, JWTValidationInfo> gatewayKeyCache;
// invalidTokenCache contains invalid tokens -> key: token, value: true
private LoadingCache<String, Boolean> invalidTokenCache;
// gatewayJWTTokenCache contains backendJWT generated by gateway -> key: id, value: JWTValidationInfo

//todo(amali) revisit apikey caches
private LoadingCache<String, JWTValidationInfo> gatewayJWTTokenCache;
private LoadingCache<String, String> getGatewayInternalKeyCache;
private LoadingCache<String, String> getInvalidGatewayInternalKeyCache;
Expand All @@ -51,8 +53,6 @@ public void init() {
cacheEnabled = cacheDto.isEnabled();
int maxSize = cacheDto.getMaximumSize();
int expiryTime = cacheDto.getExpiryTime();
gatewaySignedJWTParseCache = initCache(maxSize, expiryTime);
gatewayTokenCache = initCache(maxSize, expiryTime);
gatewayKeyCache = initCache(maxSize, expiryTime);
invalidTokenCache = initCache(maxSize, expiryTime);
gatewayJWTTokenCache = initCache(maxSize, expiryTime);
Expand Down Expand Up @@ -99,21 +99,6 @@ public LoadingCache getInvalidGatewayInternalKeyCache() {
return getInvalidGatewayInternalKeyCache;
}

/**
*
* @return SignedJWT ParsedCache
*/
public LoadingCache getGatewaySignedJWTParseCache() {
return gatewaySignedJWTParseCache;
}

/**
* @return gateway token cache
*/
public LoadingCache getGatewayTokenCache() {
return gatewayTokenCache;
}

/**
* @return gateway key cache
*/
Expand Down
Loading
Loading