Skip to content

Commit

Permalink
Merge pull request #120 from assimbly/develop
Browse files Browse the repository at this point in the history
Release 5.0.0
  • Loading branch information
skin27 authored Oct 11, 2024
2 parents 272546d + b2b329c commit 5e329b3
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.util.ObjectHelper;

import org.assimbly.tenantvariables.domain.TenantVariable;
import org.assimbly.tenantvariables.mongo.MongoDao;

import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -455,7 +456,7 @@ public void setAuthenticationType(String authenticationType) {
* The accessToken for login
*/
public String getAccessToken() {
return MongoDao.interpolatePossibleTenantVariable(accessToken, getTenant());
return MongoDao.interpolatePossibleTenantVariable(accessToken, getTenant(), TenantVariable.TenantVarType.StaticTenantVariable);
}

public void setAccessToken(String accessToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.commons.lang3.StringUtils;
import org.assimbly.tenantvariables.mongo.MongoDao;
import org.assimbly.oauth2token.tenant.TenantVariableManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.assimbly.tenantvariables.TenantVariablesProcessor;
Expand Down Expand Up @@ -39,10 +39,10 @@ public void process(Exchange exchange) {
String refreshFlagVarName = TokenService.OAUTH2_PREFIX + id + TokenService.OAUTH2_REFRESH_FLAG_SUFFIX;

// check if there's a tenant variable inside tenantVar, and return real value
String expireDate = MongoDao.getTenantVariableValue(expireDateVarName, tenant, environment);
String accessToken = MongoDao.getTenantVariableValue(accessTokenVarName, tenant, environment);
String refreshFlag = MongoDao.getTenantVariableValue(refreshFlagVarName, tenant, environment);
String tokenTenantVarValue = MongoDao.getTenantVariableValue(tokenName, tenant, environment);
String expireDate = TenantVariableManager.getTenantVariableValue(expireDateVarName, tenant, environment);
String accessToken = TenantVariableManager.getTenantVariableValue(accessTokenVarName, tenant, environment);
String refreshFlag = TenantVariableManager.getTenantVariableValue(refreshFlagVarName, tenant, environment);
String tokenTenantVarValue = TenantVariableManager.discoverAndGetTenantVariableValue(tokenName, tenant, environment);

Calendar expireCal = Calendar.getInstance();
Calendar expireDelayCal = Calendar.getInstance();
Expand Down Expand Up @@ -73,11 +73,14 @@ public void process(Exchange exchange) {
accessToken = TokenService.refreshTokenInfo(id, environment, tenant);
if(accessToken!=null && (!accessToken.equals(accessTokenOld) || StringUtils.isEmpty(tokenTenantVarValue))) {
// add token to tenant variable
MongoDao.saveTenantVariable(tokenName, accessToken, tenant, environment);
TenantVariableManager.discoverAndSaveTenantVariable(tokenName, accessToken, tenant, environment);
}
}

// add token to the header
exchange.getOut().setHeader(tokenName, accessToken);
if(!TenantVariableManager.isStaticTenantVariable(tokenName)) {
// add token to the header
exchange.getOut().setHeader(tokenName, accessToken);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.assimbly.tenantvariables.mongo.MongoDao;
import org.assimbly.oauth2token.tenant.TenantVariableManager;
import org.assimbly.util.exception.OAuth2TokenException;
import org.json.JSONObject;
import org.assimbly.auth.endpoint.annotation.Secured;
Expand Down Expand Up @@ -73,13 +73,13 @@ public Map<String, String> tokenInfo(
String tokenTenantVarName = OAUTH2_PREFIX + id + OAUTH2_TOKEN_TENANT_VAR_SUFFIX;

// check if there's a tenant variable inside tenantVar, and return real value
String scope = MongoDao.getTenantVariableValue(scopeVarName, tenant, environment);
String clientId = MongoDao.getTenantVariableValue(clientIdVarName, tenant, environment);
String clientSecret = MongoDao.getTenantVariableValue(clientSecretVarName, tenant, environment);
String redirectUri = MongoDao.getTenantVariableValue(redirectUriVarName, tenant, environment);
String uriToken = MongoDao.getTenantVariableValue(uriTokenVarName, tenant, environment);
String credentialsType = MongoDao.getTenantVariableValue(credentialsTypeVarName, tenant, environment);
String tokenTenantVar = MongoDao.getTenantVariableValue(tokenTenantVarName, tenant, environment);
String scope = TenantVariableManager.getTenantVariableValue(scopeVarName, tenant, environment);
String clientId = TenantVariableManager.getTenantVariableValue(clientIdVarName, tenant, environment);
String clientSecret = TenantVariableManager.getTenantVariableValue(clientSecretVarName, tenant, environment);
String redirectUri = TenantVariableManager.getTenantVariableValue(redirectUriVarName, tenant, environment);
String uriToken = TenantVariableManager.getTenantVariableValue(uriTokenVarName, tenant, environment);
String credentialsType = TenantVariableManager.getTenantVariableValue(credentialsTypeVarName, tenant, environment);
String tokenTenantVar = TenantVariableManager.getTenantVariableValue(tokenTenantVarName, tenant, environment);

boolean customCredentialsType = StringUtils.isEmpty(credentialsType) ||
credentialsType.equals(CREDENTIALS_TYPE_CUSTOM);
Expand All @@ -98,20 +98,20 @@ public Map<String, String> tokenInfo(
// save token info into tenant vars
String expiresIn = tokenInfoMap.get(SERVICE_PARAM_EXPIRES_IN);
if(expiresIn!=null && !expiresIn.isEmpty()) {
MongoDao.saveTenantVariable(expireDateVarName, expiresIn, tenant, environment);
TenantVariableManager.saveTenantVariable(expireDateVarName, expiresIn, tenant, environment);
}
String accessToken = tokenInfoMap.get(SERVICE_PARAM_ACCESS_TOKEN);
if(accessToken!=null && !accessToken.isEmpty()) {
MongoDao.saveTenantVariable(accessTokenVarName, accessToken, tenant, environment);
MongoDao.saveTenantVariable(tokenTenantVar, accessToken, tenant, environment);
TenantVariableManager.saveTenantVariable(accessTokenVarName, accessToken, tenant, environment);
TenantVariableManager.discoverAndSaveTenantVariable(tokenTenantVar, accessToken, tenant, environment);
}
String refreshToken = tokenInfoMap.get(SERVICE_PARAM_REFRESH_TOKEN);
if(refreshToken!=null && !refreshToken.isEmpty()) {
MongoDao.saveTenantVariable(refreshTokenVarName, refreshToken, tenant, environment);
TenantVariableManager.saveTenantVariable(refreshTokenVarName, refreshToken, tenant, environment);
}

// set refresh flag to inactive
MongoDao.saveTenantVariable(refreshFlagVarName, "0", tenant, environment);
TenantVariableManager.saveTenantVariable(refreshFlagVarName, "0", tenant, environment);

// return token info hashmap
return tokenInfoMap;
Expand All @@ -136,16 +136,16 @@ public static String refreshTokenInfo(String id, String environment, String tena

try {
// set refresh flag to active
MongoDao.saveTenantVariable(refreshFlagVarName, "1", tenant, environment);
TenantVariableManager.saveTenantVariable(refreshFlagVarName, "1", tenant, environment);

// check if there's a tenant variable inside tenantVar, and return real value
String scope = MongoDao.getTenantVariableValue(scopeVarName, tenant, environment);
String clientId = MongoDao.getTenantVariableValue(clientIdVarName, tenant, environment);
String clientSecret = MongoDao.getTenantVariableValue(clientSecretVarName, tenant, environment);
String redirectUri = MongoDao.getTenantVariableValue(redirectUriVarName, tenant, environment);
String refreshToken = MongoDao.getTenantVariableValue(refreshTokenVarName, tenant, environment);
String uriToken = MongoDao.getTenantVariableValue(uriTokenVarName, tenant, environment);
String credentialsType = MongoDao.getTenantVariableValue(credentialsTypeVarName, tenant, environment);
String scope = TenantVariableManager.getTenantVariableValue(scopeVarName, tenant, environment);
String clientId = TenantVariableManager.getTenantVariableValue(clientIdVarName, tenant, environment);
String clientSecret = TenantVariableManager.getTenantVariableValue(clientSecretVarName, tenant, environment);
String redirectUri = TenantVariableManager.getTenantVariableValue(redirectUriVarName, tenant, environment);
String refreshToken = TenantVariableManager.getTenantVariableValue(refreshTokenVarName, tenant, environment);
String uriToken = TenantVariableManager.getTenantVariableValue(uriTokenVarName, tenant, environment);
String credentialsType = TenantVariableManager.getTenantVariableValue(credentialsTypeVarName, tenant, environment);

boolean customCredentialsType = StringUtils.isEmpty(credentialsType) ||
credentialsType.equals(CREDENTIALS_TYPE_CUSTOM);
Expand All @@ -164,23 +164,23 @@ public static String refreshTokenInfo(String id, String environment, String tena
// save token info into tenant vars
String expiresInResp = tokenInfoMap.get(SERVICE_PARAM_EXPIRES_IN);
if(expiresInResp!=null && !expiresInResp.isEmpty()) {
MongoDao.saveTenantVariable(expireDateVarName, expiresInResp, tenant, environment);
TenantVariableManager.saveTenantVariable(expireDateVarName, expiresInResp, tenant, environment);
}
String accessTokenResp = tokenInfoMap.get(SERVICE_PARAM_ACCESS_TOKEN);
if(accessTokenResp!=null && !accessTokenResp.isEmpty()) {
MongoDao.saveTenantVariable(accessTokenVarName, accessTokenResp, tenant, environment);
TenantVariableManager.saveTenantVariable(accessTokenVarName, accessTokenResp, tenant, environment);
accessToken = accessTokenResp;
}
String refreshTokenResp = tokenInfoMap.get(SERVICE_PARAM_REFRESH_TOKEN);
if(refreshTokenResp!=null && !refreshTokenResp.isEmpty()) {
MongoDao.saveTenantVariable(refreshTokenVarName, refreshTokenResp, tenant, environment);
TenantVariableManager.saveTenantVariable(refreshTokenVarName, refreshTokenResp, tenant, environment);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
// set refresh flag to inactive
MongoDao.saveTenantVariable(refreshFlagVarName, "0", tenant, environment);
TenantVariableManager.saveTenantVariable(refreshFlagVarName, "0", tenant, environment);
}

// return new access token
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.assimbly.oauth2token.tenant;

import org.assimbly.tenantvariables.domain.TenantVariable;
import org.assimbly.tenantvariables.mongo.MongoDao;

public class TenantVariableManager {

private static final String STATIC_TENANT_VARIABLE_SUFFIX = "_"+ TenantVariable.TenantVarType.StaticTenantVariable.name();

public static String getTenantVariableValue(String tokenName, String tenant, String environment) {
return MongoDao.getTenantVariableValue(tokenName, tenant, environment);
}

public static String discoverAndGetTenantVariableValue(String tokenName, String tenant, String environment) {
TenantVariable.TenantVarType tenantVarType = TenantVariable.TenantVarType.TenantVariable;

if(isStaticTenantVariable(tokenName)) {
tokenName = getStaticTenantVariableName(tokenName);
tenantVarType = TenantVariable.TenantVarType.StaticTenantVariable;
}

return MongoDao.getTenantVariableValue(tokenName, tenant, environment, tenantVarType);
}

public static void saveTenantVariable(String tokenName, String accessToken, String tenant, String environment) {
MongoDao.saveTenantVariable(tokenName, accessToken, tenant, environment);
}

public static void discoverAndSaveTenantVariable(String tokenName, String accessToken, String tenant, String environment) {
TenantVariable.TenantVarType tenantVarType = TenantVariable.TenantVarType.TenantVariable;

if(TenantVariableManager.isStaticTenantVariable(tokenName)) {
tokenName = getStaticTenantVariableName(tokenName);
tenantVarType = TenantVariable.TenantVarType.StaticTenantVariable;
}

MongoDao.saveTenantVariable(tokenName, accessToken, tenant, environment, tenantVarType);
}

public static boolean isStaticTenantVariable(String tokenName) {
return tokenName.endsWith(STATIC_TENANT_VARIABLE_SUFFIX);
}

private static String getStaticTenantVariableName(String tokenName) {
int index = tokenName.indexOf(STATIC_TENANT_VARIABLE_SUFFIX);
return tokenName.substring(0, index);
}

}
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@
<jakarta.activation.version>2.1.3</jakarta.activation.version>
<jakarta.mail.version>2.1.3</jakarta.mail.version>
<jcifs.version>2.1.39</jcifs.version>
<kalium.version>0.8.0</kalium.version>
<libsodium-jna.version>1.0.5</libsodium-jna.version>
<mvel.version>2.5.2.Final</mvel.version>
<mime4j.version>0.8.11</mime4j.version>
<mockito.version>3.3.3</mockito.version>
<mockito-core.version>5.13.0</mockito-core.version>
<mockito-core.version>5.14.1</mockito-core.version>
<mockito-inline.version>5.2.0</mockito-inline.version>
<mongo.version>4.7.2</mongo.version>
<osgi.version>6.0.0</osgi.version>
Expand Down
6 changes: 0 additions & 6 deletions tenantvariables/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
</distributionManagement>

<dependencies>
<dependency>
<groupId>org.abstractj.kalium</groupId>
<artifactId>kalium</artifactId>
<version>${kalium.version}</version>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
Expand Down
Loading

0 comments on commit 5e329b3

Please sign in to comment.