Skip to content

Commit

Permalink
Merge pull request #104 from chenhaozx/feature/support-ZKP
Browse files Browse the repository at this point in the history
add parameters check for AMOP interface
  • Loading branch information
chenhaozx committed Dec 20, 2019
2 parents 15393ce + d4c2b7d commit e6db6e2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.Setter;

import com.webank.weid.protocol.amop.base.AmopBaseMsgArgs;
import com.webank.weid.protocol.base.CredentialPojo;
import com.webank.weid.protocol.base.PresentationE;

/**
Expand All @@ -35,10 +36,15 @@
public class IssueCredentialArgs extends AmopBaseMsgArgs {

/**
* user's presentation.
* user's credential list,including KYC credential and credential based on CPT111.
*/
private PresentationE presentation;

/**
* credential based on CPT 110 (metadata credential).
*/
private CredentialPojo credentialPojo;

/**
* user claim (decided by issuer in the first amop interface).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,35 @@

package com.webank.weid.protocol.amop;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

import com.webank.weid.protocol.amop.base.AmopBaseMsgArgs;
import com.webank.weid.protocol.base.CredentialPojo;
import com.webank.weid.protocol.base.PolicyAndPreCredential;
import com.webank.weid.protocol.base.WeIdAuthentication;

/**
* args for RequestIssueCredential.
*
* @author tonychen 2019年12月4日
*/
@Getter
@Setter
public class RequestIssueCredentialArgs extends AmopBaseMsgArgs {

//private PresentationE presentation;

/**
* policyAndPreCredential from issuer.
*/
private PolicyAndPreCredential policyAndPreCredential;

/**
* user's credential list.
*/
private List<CredentialPojo> credentialList;

/**
* policy id.
*/
Expand Down
60 changes: 47 additions & 13 deletions src/main/java/com/webank/weid/service/impl/AmopServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.webank.weid.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -45,6 +44,7 @@
import com.webank.weid.protocol.base.PolicyAndChallenge;
import com.webank.weid.protocol.base.PolicyAndPreCredential;
import com.webank.weid.protocol.base.PresentationE;
import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.protocol.response.AmopResponse;
import com.webank.weid.protocol.response.GetEncryptKeyResponse;
import com.webank.weid.protocol.response.GetPolicyAndChallengeResponse;
Expand All @@ -62,6 +62,7 @@
import com.webank.weid.suite.persistence.sql.driver.MysqlDriver;
import com.webank.weid.util.DataToolUtils;
import com.webank.weid.util.JsonUtil;
import com.webank.weid.util.WeIdUtils;


/**
Expand Down Expand Up @@ -206,6 +207,15 @@ public ResponseData<RequestIssueCredentialResponse> requestIssueCredential(
String toOrgId,
RequestIssueCredentialArgs args) {

int checkErrorCode = checkIssueCredentialArgs(args).getCode();
if (checkErrorCode != ErrorCode.SUCCESS.getCode()) {
logger.error(
"[requestIssueCredential] prepareZkpCredential failed. error code :{}",
checkErrorCode);
return new ResponseData<RequestIssueCredentialResponse>(null,
ErrorCode.getTypeByErrorCode(checkErrorCode));
}

//1. user genenerate credential based on CPT111
PolicyAndPreCredential policyAndPreCredential = args.getPolicyAndPreCredential();
String claimJson = policyAndPreCredential.getClaim();
Expand All @@ -217,7 +227,8 @@ public ResponseData<RequestIssueCredentialResponse> requestIssueCredential(
args.getAuth());
int errCode = userCredentialResp.getErrorCode();
if (errCode != ErrorCode.SUCCESS.getCode()) {
logger.error("[requestIssueCredential] prepareZkpCredential failed. error code :{}",
logger.error(
"[requestIssueCredential] prepareZkpCredential failed. error code :{}",
errCode);
return new ResponseData<RequestIssueCredentialResponse>(null,
ErrorCode.getTypeByErrorCode(errCode));
Expand All @@ -228,7 +239,8 @@ public ResponseData<RequestIssueCredentialResponse> requestIssueCredential(
ResponseData<PresentationE> presentationResp = preparePresentation(args, userCredential);
int errorCode = presentationResp.getErrorCode();
if (errorCode != ErrorCode.SUCCESS.getCode()) {
logger.error("[requestIssueCredential] create presentation failed. error code :{}",
logger.error(
"[requestIssueCredential] create presentation failed. error code :{}",
errorCode);
return new ResponseData<RequestIssueCredentialResponse>(null,
ErrorCode.getTypeByErrorCode(errorCode));
Expand All @@ -237,7 +249,7 @@ public ResponseData<RequestIssueCredentialResponse> requestIssueCredential(
//3. send presentataion to issuer and request issue credential.
PresentationE presentation = presentationResp.getResult();
ResponseData<RequestIssueCredentialResponse> resp =
requestIssueCredential(
requestIssueCredentialInner(
toOrgId,
args,
userCredential,
Expand All @@ -252,23 +264,47 @@ public ResponseData<RequestIssueCredentialResponse> requestIssueCredential(
return resp;
}

private ResponseData<RequestIssueCredentialResponse> requestIssueCredential(
private ErrorCode checkIssueCredentialArgs(RequestIssueCredentialArgs args) {

if (args == null
|| args.getAuth() == null
|| args.getPolicyAndPreCredential() == null
|| args.getCredentialList() == null) {

return ErrorCode.ILLEGAL_INPUT;

}
PolicyAndPreCredential policyAndPreCredential = args.getPolicyAndPreCredential();
PolicyAndChallenge policyAndChallenge = policyAndPreCredential.getPolicyAndChallenge();
if (policyAndChallenge == null
|| policyAndChallenge.getChallenge() == null
|| policyAndChallenge.getPresentationPolicyE() == null) {
return ErrorCode.ILLEGAL_INPUT;
}
WeIdAuthentication auth = args.getAuth();
if (!WeIdUtils.isWeIdValid(auth.getWeId())) {
return ErrorCode.WEID_INVALID;
}
if (!WeIdUtils
.isKeypairMatch(auth.getWeIdPrivateKey().getPrivateKey(), auth.getWeIdPublicKeyId())) {
return ErrorCode.WEID_PRIVATEKEY_DOES_NOT_MATCH;
}

return ErrorCode.SUCCESS;
}

private ResponseData<RequestIssueCredentialResponse> requestIssueCredentialInner(
String toOrgId,
RequestIssueCredentialArgs args,
CredentialPojo userCredential,
PresentationE presentation) {

//prepare request args
String claimJson = args.getPolicyAndPreCredential().getClaim();
//Integer cptId = Integer.valueOf((String) (userCredential.getClaim()
//.get(CredentialConstant.CREDENTIAL_META_KEY_CPTID)));
IssueCredentialArgs issueCredentialArgs = new IssueCredentialArgs();
issueCredentialArgs.setClaim(claimJson);
//issueCredentialArgs.setCptId(cptId);
//issueCredentialArgs.setUserWeId(args.getAuth().getWeId());
issueCredentialArgs.setPolicyId(args.getPolicyId());
issueCredentialArgs.setPresentation(presentation);
//JsonTransportation transportation = TransportationFactory.newJsonTransportation();

// AMOP request (issuer to issue credential)
ResponseData<RequestIssueCredentialResponse> resp = this.getImpl(
Expand All @@ -287,12 +323,10 @@ private ResponseData<PresentationE> preparePresentation(
RequestIssueCredentialArgs args,
CredentialPojo userCredential) {

List<CredentialPojo> credentialList = args.getCredentialList();
PolicyAndPreCredential policyAndPreCredential = args.getPolicyAndPreCredential();
CredentialPojo preCredential = policyAndPreCredential.getPreCredential();
PolicyAndChallenge policyAndChallenge = policyAndPreCredential.getPolicyAndChallenge();

List<CredentialPojo> credentialList = new ArrayList<>();
credentialList.add(preCredential);
credentialList.add(userCredential);

//put pre-credential and user-credential(based on CPT 111)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/webank/weid/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static Map<String, String> credentialToMonolayer(CredentialPojo credentia

private static int maxArraySize() {

return Integer.parseInt(PropertyUtils.getProperty("cpt.array.length", "-1"));
return Integer.parseInt(PropertyUtils.getProperty("zkp.cpt.array.length", "-1"));
}

private static Map<String, String> monolayerToMap(String json) throws IOException {
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/weidentity.properties.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ domain.credentialSignature=datasource1:credential_signature
# Salt length for Proof creation.
salt.length=5

# Default length of array value in CPT when creating credential based on ZKP.
zkp.cpt.array.length=5

# AMOP Config
# Timeout for amop request, default: 5000ms
amop.request.timeout=5000
Expand Down

0 comments on commit e6db6e2

Please sign in to comment.