Skip to content

Commit

Permalink
Merge pull request #1 from xljiadahao/wechat-littleprogram
Browse files Browse the repository at this point in the history
Wechat littleprogram with multi-tenant feature
  • Loading branch information
xljiadahao authored Nov 26, 2017
2 parents 5a8c202 + 4db1059 commit 76515c2
Show file tree
Hide file tree
Showing 35 changed files with 1,105 additions and 73 deletions.
46 changes: 44 additions & 2 deletions dbscript/db_mysql.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
/**
* Author: xulei
* Created: Nov 06, 2016
* Updated on Nov 15 2017 for multi-tenant by xulei
* Updated on Nov 17 2017 for group certificate one-many relationship
*/

CREATE DATABASE hellocrypto;

USE hellocrypto;

-- ---------------------------------------------------------------
-- Table group
-- Store the group information with admin functionality
-- ---------------------------------------------------------------
CREATE TABLE `group` (
`IDENTIFIER` varchar(10) NOT NULL UNIQUE,
`ORG_NAME` varchar(50) NOT NULL,
`ACTIVITY_NAME` varchar(50) NOT NULL,
`MAX_COUNT` int DEFAULT NULL,
`IS_ACTIVATED` tinyint(1) NOT NULL,
`TIMESTAMP` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`IDENTIFIER`),
CHECK(`IS_ACTIVATED` in(0,1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ---------------------------------------------------------------
-- Table certificate
-- Store the certificate or public key for each client
-- ---------------------------------------------------------------
CREATE TABLE `certificate` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) NOT NULL UNIQUE,
`NAME` varchar(50) NOT NULL,
`PUB_KEY_FINGERPRINT` varchar(100) NOT NULL UNIQUE,
`CERTIFICATE_BINARY` blob NOT NULL,
`TYPE` varchar(10) NOT NULL,
`TIMESTAMP` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
`GROUP_ID` varchar(10) NOT NULL,
PRIMARY KEY (`ID`),
constraint GROUP_IDENTIFIER_FK foreign key(`GROUP_ID`)
references `group`(`IDENTIFIER`) on delete cascade
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ---------------------------------------------------------------
-- Table group_certificate_mapping (@Deprecated),
-- due to group certificate one-many relationship
-- Store the relationship for the client under specific group
-- ---------------------------------------------------------------
-- CREATE TABLE `group_certificate_mapping` (
-- `GROUP_ID` varchar(10) NOT NULL,
-- `CERTIFICATE_ID` int(11) NOT NULL,
-- constraint GROUP_IDENTIFIER_FK foreign key(`GROUP_ID`)
-- references `group`(`IDENTIFIER`) on delete cascade,
-- constraint CERTIFICATE_ID_FK foreign key(`CERTIFICATE_ID`)
-- references `certificate`(`ID`) on delete cascade,
-- CONSTRAINT COMPOSITE_PK PRIMARY KEY(`GROUP_ID`,`CERTIFICATE_ID`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hellocrypto.action;

import com.hellocrypto.cache.LuckyDrawResult;
import com.hellocrypto.constant.GeneralConstant;
import com.hellocrypto.exception.BadReqException;
import com.hellocrypto.handler.CertificateHandler;
import java.io.File;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

/**
*
Expand Down Expand Up @@ -47,7 +49,7 @@ public String execute() {
}

public String prereq() {
if (LuckyDrawResult.getLuckDrawResults() != null && !LuckyDrawResult.getLuckDrawResults().isEmpty()) {
if (!CollectionUtils.isEmpty(LuckyDrawResult.getLuckDrawResults(GeneralConstant.ADHOC_KEY))) {
decryptEnabled = true;
}
return "success";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.hellocrypto.bo.KeystoreBo;
import com.hellocrypto.bo.SecureInfoBo;
import com.hellocrypto.cache.LuckyDrawResult;
import com.hellocrypto.constant.GeneralConstant;
import com.hellocrypto.handler.DecryptionHandler;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

/**
*
Expand Down Expand Up @@ -38,11 +40,11 @@ public class DecryptionController extends BaseAction {

@Override
public String execute() {
if (LuckyDrawResult.getLuckDrawResults() == null || LuckyDrawResult.getLuckDrawResults().isEmpty()) {
if (CollectionUtils.isEmpty(LuckyDrawResult.getLuckDrawResults(GeneralConstant.ADHOC_KEY))) {
return "failure";
}
// prepare encrypted data
secureInfo.addAll(LuckyDrawResult.getLuckDrawResults());
secureInfo.addAll(LuckyDrawResult.getLuckDrawResults(GeneralConstant.ADHOC_KEY));
// prepare participate names
participateNames.addAll(decryptionHandler.preDecryptGetParticipateName());
SecureInfoBo secureInfoBo = decryptionHandler.getSecureInfo(constructKeystoreBo());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hellocrypto.action;

import com.hellocrypto.cache.LuckyDrawResult;
import com.hellocrypto.constant.GeneralConstant;
import com.hellocrypto.handler.DecryptionHandler;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

/**
*
Expand All @@ -24,11 +26,11 @@ public class PreDecryptionController extends BaseAction {

@Override
public String execute() {
if (LuckyDrawResult.getLuckDrawResults() == null || LuckyDrawResult.getLuckDrawResults().isEmpty()) {
if (CollectionUtils.isEmpty(LuckyDrawResult.getLuckDrawResults(GeneralConstant.ADHOC_KEY))) {
return "failure";
}
// prepare encrypted data
secureInfo.addAll(LuckyDrawResult.getLuckDrawResults());
secureInfo.addAll(LuckyDrawResult.getLuckDrawResults(GeneralConstant.ADHOC_KEY));
// prepare participate names
participateNames.addAll(decryptionHandler.preDecryptGetParticipateName());
return "success";
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/hellocrypto/bo/EncryptionTransitionBo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hellocrypto.bo;

/**
*
* @author xulei
*/
public class EncryptionTransitionBo {

private String name;
private String encryptText;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEncryptText() {
return encryptText;
}

public void setEncryptText(String encryptText) {
this.encryptText = encryptText;
}

}
28 changes: 28 additions & 0 deletions src/main/java/com/hellocrypto/bo/GroupGenBo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hellocrypto.bo;

/**
*
* @author xulei
*/
public class GroupGenBo {

private String groupIdentifier;
private long createTime;

public String getGroupIdentifier() {
return groupIdentifier;
}

public void setGroupIdentifier(String groupIdentifier) {
this.groupIdentifier = groupIdentifier;
}

public long getCreateTime() {
return createTime;
}

public void setCreateTime(long createTime) {
this.createTime = createTime;
}

}
55 changes: 55 additions & 0 deletions src/main/java/com/hellocrypto/bo/GroupParticipationBo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.hellocrypto.bo;

/**
*
* @author xulei
*/
public class GroupParticipationBo {

private String name;
private String secureKey;
private String groupIdentifier;
private String orgName;
private String activityName;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSecureKey() {
return secureKey;
}

public void setSecureKey(String secureKey) {
this.secureKey = secureKey;
}

public String getGroupIdentifier() {
return groupIdentifier;
}

public void setGroupIdentifier(String groupIdentifier) {
this.groupIdentifier = groupIdentifier;
}

public String getOrgName() {
return orgName;
}

public void setOrgName(String orgName) {
this.orgName = orgName;
}

public String getActivityName() {
return activityName;
}

public void setActivityName(String activityName) {
this.activityName = activityName;
}

}
20 changes: 19 additions & 1 deletion src/main/java/com/hellocrypto/bo/LuckyDrawBo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
*
* @author leixu2
* @author xulei
*/
public class LuckyDrawBo {

Expand All @@ -13,6 +13,8 @@ public class LuckyDrawBo {
private List<String> names;
private List<String> resultList;
private String timestamp;
private String orgName;
private String activityName;
private String description;

public Boolean getIsSuccess() {
Expand Down Expand Up @@ -55,6 +57,22 @@ public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}

public String getOrgName() {
return orgName;
}

public void setOrgName(String orgName) {
this.orgName = orgName;
}

public String getActivityName() {
return activityName;
}

public void setActivityName(String activityName) {
this.activityName = activityName;
}

public String getDescription() {
return description;
}
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/com/hellocrypto/cache/Constant.java

This file was deleted.

28 changes: 22 additions & 6 deletions src/main/java/com/hellocrypto/cache/LuckyDrawResult.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
package com.hellocrypto.cache;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
*
* @author leixu2
* @author xulei
*/
public class LuckyDrawResult {

private static List<String> luckDrawResults = null;
private static final Logger logger = Logger.getLogger(LuckyDrawResult.class);

private static Map<String, List<String>> luckDrawResults = new HashMap<String, List<String>>();

public static List<String> getLuckDrawResults() {
return luckDrawResults;
public static List<String> getLuckDrawResults(String groupIdentifier) {
return luckDrawResults.get(groupIdentifier);
}

public static void setDrawResult(String groupIdentifier, List<String> list) {
luckDrawResults.put(groupIdentifier, list);
}

public static void setLuckDrawResults(List<String> luckDrawResults) {
LuckyDrawResult.luckDrawResults = luckDrawResults;
public static boolean cleanLuckDrawResults(String groupIdentifier) {
List<String> cleanList = luckDrawResults.remove(groupIdentifier);
if (cleanList == null) {
logger.warn("cannot find the result for group identifier: " + groupIdentifier);
return false;
} else {
logger.info("cleanLuckDrawResults, group id: " + groupIdentifier + ", result size: " + cleanList.size());
return true;
}
}

}
15 changes: 15 additions & 0 deletions src/main/java/com/hellocrypto/constant/GeneralConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.hellocrypto.constant;

/**
*
* @author xulei
*/
public class GeneralConstant {

public static final String AUTH = "hellocrypto";
public static final String SECURITY_CONTEXT = "HelloCrypto-Security-Context";

// group identifier has 5 characters, so the adhoc key need to be different from it
public static final String ADHOC_KEY = "ad-hoc";

}
21 changes: 21 additions & 0 deletions src/main/java/com/hellocrypto/constant/ResponseMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hellocrypto.constant;

/**
*
* @author xulei
*/
public class ResponseMessage {

/**
* general response
*/
public static final String BAD_REQ = "The request is invalid, please try it again";
public static final String INTERNAL_SERVER_ERROR = "Oops, sorry, we got some problems, "
+ "our engineers are working on it, please come back later";

/**
* business logic response
*/
public static final String BAD_DECRYPTION = "Oops, bad decryption, try next time!";

}
Loading

0 comments on commit 76515c2

Please sign in to comment.