Skip to content

Commit

Permalink
Merge pull request #154 from WeBankFinTech/feature/optimize-weid-auth
Browse files Browse the repository at this point in the history
Feature/change weid auth object storage from cache to db
  • Loading branch information
yanggang-JV committed Mar 31, 2020
2 parents 90a9601 + 55040fc commit 74cffbe
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
44 changes: 23 additions & 21 deletions src/main/java/com/webank/weid/constant/DataDriverConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* constant for DataDriver.
*
*
* @author tonychen 2019年3月25日
*/
public final class DataDriverConstant {
Expand Down Expand Up @@ -50,72 +50,72 @@ public final class DataDriverConstant {
* jdbc user password properties.
*/
public static final String JDBC_USER_PASSWORD = "jdbc.password";

/**
* jdbc maxActive properties.
*/
public static final String JDBC_MAX_ACTIVE = "jdbc.maxActive";

/**
* jdbc minIdle properties.
*/
public static final String JDBC_MIN_IDLE = "jdbc.minIdle";

/**
* jdbc minIdle properties.
*/
public static final String JDBC_MAX_IDLE = "jdbc.maxIdle";

/**
* jdbc maxWait properties.
*/
public static final String JDBC_MAX_WAIT = "jdbc.maxWait";

/**
* jdbc timeBetweenEvictionRunsMillis properties.
*/
public static final String JDBC_TIME_BETWEEN_ERM = "jdbc.timeBetweenEvictionRunsMillis";

/**
* jdbc numTestsPerEvictionRun properties.
*/
public static final String JDBC_NUM_TEST_PER_ER = "jdbc.numTestsPerEvictionRun";

/**
* jdbc maxWait properties.
*/
public static final String JDBC_MIN_EITM = "jdbc.minEvictableIdleTimeMillis";

/**
* jdbc driverClassName.
*/
public static final String JDBC_MYSQL_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";

/**
* jdbc removeAbandoned.
*/
public static final String JDBC_REMOVE_ABANDONED = "true";

/**
* jdbc removeAbandonedTimeout.
*/
public static final String JDBC_REMOVE_ABANDONED_TIMEOUT = "180";

/**
* jdbc testOnBorrow.
*/
public static final String JDBC_TEST_ON_BORROW = "false";

/**
* jdbc testOnWhile.
*/
public static final String JDBC_TEST_ON_WHILE = "true";

/**
* jdbc validationQuery.
*/
public static final String JDBC_VALIDATION_QUERY = "SELECT 1";

/**
* sql execute status.
*/
Expand All @@ -140,7 +140,7 @@ public final class DataDriverConstant {
* sql index.
*/
public static final int SQL_INDEX_THIRD = 3;

/**
* the key of ConnectionPool.
*/
Expand All @@ -160,9 +160,9 @@ public final class DataDriverConstant {
public static final String POOL_NUM_TEST_PER_ER = "numTestsPerEvictionRun";
public static final String POOL_VALIDATION_QUERY = "validationQuery";
public static final String POOL_MIN_EITM = "minEvictableIdleTimeMillis";

/**
* the default value for pool.
* the default value for pool.
*/
public static final String POOL_DRIVER_NAME_DEFAULT_VALUE = "com.mysql.jdbc.Driver";
public static final String POOL_MAX_ACTIVE_DEFAULT_VALUE = "50";
Expand All @@ -178,7 +178,7 @@ public final class DataDriverConstant {
*/
public static final String DOMAIN_DEFAULT_INFO = "domain.defaultInfo";
public static final String DOMAIN_DEFAULT_INFO_TIMEOUT = "domain.defaultInfo.timeout";

/**
* 系统domain之私钥存储domainKey.
*/
Expand All @@ -187,8 +187,10 @@ public final class DataDriverConstant {
public static final String DOMAIN_ISSUER_TEMPLATE_SECRET = "domain.templateSecret";

public static final String DOMAIN_USER_MASTER_SECRET = "domain.masterKey";

public static final String DOMAIN_USER_CREDENTIAL_SIGNATURE = "domain.credentialSignature";

public static final String DOMAIN_RESOURCE_INFO = "domain.resourceInfo";

public static final String DOMAIN_WEID_AUTH = "domain.weIdAuth";
}
45 changes: 42 additions & 3 deletions src/main/java/com/webank/weid/suite/auth/impl/WeIdAuthImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.slf4j.LoggerFactory;

import com.webank.weid.constant.AmopMsgType;
import com.webank.weid.constant.DataDriverConstant;
import com.webank.weid.constant.ErrorCode;
import com.webank.weid.constant.ParamKeyConstant;
import com.webank.weid.protocol.amop.GetWeIdAuthArgs;
Expand All @@ -46,13 +47,16 @@
import com.webank.weid.service.impl.WeIdServiceImpl;
import com.webank.weid.service.impl.callback.RequestVerifyChallengeCallback;
import com.webank.weid.service.impl.callback.WeIdAuthAmopCallback;
import com.webank.weid.suite.api.persistence.Persistence;
import com.webank.weid.suite.auth.inf.WeIdAuth;
import com.webank.weid.suite.auth.inf.WeIdAuthCallback;
import com.webank.weid.suite.auth.protocol.WeIdAuthObj;
import com.webank.weid.suite.persistence.sql.driver.MysqlDriver;
import com.webank.weid.util.DataToolUtils;

/**
* weIdAuth service.
*
* @author tonychen 2020年3月10日
*/
@Setter
Expand All @@ -70,6 +74,8 @@ public class WeIdAuthImpl implements WeIdAuth {
private static WeIdAuthAmopCallback weIdAuthAmopCallback = new WeIdAuthAmopCallback();
private static RequestVerifyChallengeCallback VerifyChallengeCallback =
new RequestVerifyChallengeCallback();

private static Persistence dataDriver;
/**
* specify who has right to get weid auth.
*/
Expand All @@ -86,6 +92,13 @@ public class WeIdAuthImpl implements WeIdAuth {

private WeIdService weIdService = new WeIdServiceImpl();

private static Persistence getDataDriver() {
if (dataDriver == null) {
dataDriver = new MysqlDriver();
}
return dataDriver;
}

/* (non-Javadoc)
* @see com.webank.weid.suite.auth.inf.WeIdAuth#createAuthenticatedChannel(java.lang.String,
* com.webank.weid.protocol.base.WeIdAuthentication)
Expand Down Expand Up @@ -287,8 +300,21 @@ public Integer setWhiteList(List<String> whiteWeIdlist) {
@Override
public Integer addWeIdAuthObj(WeIdAuthObj weIdAuthObj) {

weIdAuthCache.put(weIdAuthObj.getChannelId(), weIdAuthObj);
return 0;
String weIdAuthData = DataToolUtils.serialize(weIdAuthObj);
String channelId = weIdAuthObj.getChannelId();
ResponseData<Integer> dbResp = getDataDriver().saveOrUpdate(
DataDriverConstant.DOMAIN_WEID_AUTH,
channelId,
weIdAuthData);
Integer errorCode = dbResp.getErrorCode();
if (errorCode != ErrorCode.SUCCESS.getCode()) {
logger.error(
"[addWeIdAuthObj] save weIdAuthObj to db failed, channel id:{}, error code is {}",
channelId,
errorCode);
return errorCode;
}
return ErrorCode.SUCCESS.getCode();
}

/* (non-Javadoc)
Expand All @@ -297,7 +323,20 @@ public Integer addWeIdAuthObj(WeIdAuthObj weIdAuthObj) {
@Override
public WeIdAuthObj getWeIdAuthObjByChannelId(String channelId) {

return weIdAuthCache.get(channelId);
ResponseData<String> dbResp = getDataDriver().get(
DataDriverConstant.DOMAIN_WEID_AUTH,
channelId);
Integer errorCode = dbResp.getErrorCode();
if (errorCode != ErrorCode.SUCCESS.getCode()) {
logger.error(
"[addWeIdAuthObj] get weIdAuthObj from db failed, channel id:{}, error code is {}",
channelId,
errorCode);
return null;
}
String weIdAuthJson = dbResp.getResult();
WeIdAuthObj weIdAuthObj = DataToolUtils.deserialize(weIdAuthJson, WeIdAuthObj.class);
return weIdAuthObj;
}

/* (non-Javadoc)
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/weidentity.properties.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ domain.defaultInfo.timeout=86400000
domain.encryptKey=datasource1:encrypt_key_info
domain.encryptKey.timeout=31556908799941

#
domain.zkp=datasource1:zkp_data

#
# zkp credential template secret
domain.templateSecret=datasource1:template_secret
domain.templateSecret.timeout=31556908799941

#
# zkp credential master secret
domain.masterKey=datasource1:master_secret

#
# zkp credential signature
domain.credentialSignature=datasource1:credential_signature

# weid auth info
domain.weIdAuth=datasource1:weid_auth

# the domain for save resource
domain.resourceInfo=datasource1:resource_info
domain.resourceInfo.timeout=31556908799941
Expand Down

0 comments on commit 74cffbe

Please sign in to comment.