|
20 | 20 |
|
21 | 21 | package com.webank.weid.http.service.impl; |
22 | 22 |
|
| 23 | +import com.webank.weid.protocol.base.CredentialPojo; |
| 24 | +import com.webank.weid.protocol.base.WeIdAuthentication; |
| 25 | +import com.webank.weid.protocol.request.CreateCredentialPojoArgs; |
| 26 | +import com.webank.weid.rpc.CredentialPojoService; |
| 27 | +import com.webank.weid.service.impl.CredentialPojoServiceImpl; |
| 28 | +import com.webank.weid.util.CredentialPojoUtils; |
| 29 | +import com.webank.weid.util.DataToolUtils; |
23 | 30 | import java.io.IOException; |
24 | 31 | import java.util.HashMap; |
25 | 32 | import java.util.Map; |
@@ -58,14 +65,16 @@ public class InvokerCredentialServiceImpl extends BaseService implements Invoker |
58 | 65 | private Logger logger = LoggerFactory.getLogger(InvokerCredentialServiceImpl.class); |
59 | 66 |
|
60 | 67 | private CredentialService credentialService = new CredentialServiceImpl(); |
| 68 | + private CredentialPojoService credentialPojoService = new CredentialPojoServiceImpl(); |
61 | 69 |
|
62 | 70 | /** |
63 | | - * Generate a credential for client to sign. The signature field is null, and both full claim |
64 | | - * and claimHash will be returned. The returned json String is an key-ordered compact json. |
| 71 | + * Generate a credential for client to sign. The signature field is null, and both full claim and claimHash will be returned. The returned json |
| 72 | + * String is an key-ordered compact json. |
65 | 73 | * |
66 | 74 | * @param createCredentialFuncArgs the functionArgs |
67 | 75 | * @return the Map contains Credential content and claimHash. |
68 | 76 | */ |
| 77 | + @Override |
69 | 78 | public HttpResponseData<Object> createCredentialInvoke( |
70 | 79 | InputArg createCredentialFuncArgs) { |
71 | 80 | try { |
@@ -150,7 +159,7 @@ public HttpResponseData<Object> createCredentialInvoke( |
150 | 159 | // this is the server-hosting privkey approach |
151 | 160 | String privateKey = KeyUtil |
152 | 161 | .getPrivateKeyByWeId(KeyUtil.SDK_PRIVKEY_PATH, keyIndexNode.textValue()); |
153 | | - if (StringUtils.isEmpty(privateKey)) { |
| 162 | + if (!KeyUtil.isPrivateKeyLengthValid(privateKey)) { |
154 | 163 | return new HttpResponseData<>(null, HttpReturnCode.INVOKER_ILLEGAL); |
155 | 164 | } |
156 | 165 | Map<String, String> credentialProof = CredentialUtils |
@@ -185,6 +194,7 @@ public HttpResponseData<Object> createCredentialInvoke( |
185 | 194 | * @param verifyCredentialFuncArgs the credential json args |
186 | 195 | * @return the Boolean response data |
187 | 196 | */ |
| 197 | + @Override |
188 | 198 | public HttpResponseData<Object> verifyCredentialInvoke(InputArg verifyCredentialFuncArgs) { |
189 | 199 | Credential credential = null; |
190 | 200 | try { |
@@ -222,4 +232,123 @@ public HttpResponseData<Object> verifyCredentialInvoke(InputArg verifyCredential |
222 | 232 | HttpReturnCode.WEID_SDK_ERROR.getCodeDesc().concat(e.getMessage())); |
223 | 233 | } |
224 | 234 | } |
| 235 | + |
| 236 | + @Override |
| 237 | + public HttpResponseData<Object> createCredentialPojoInvoke(InputArg createCredentialPojoFuncArgs) { |
| 238 | + JsonNode cptIdNode; |
| 239 | + JsonNode issuerNode; |
| 240 | + JsonNode expirationDateNode; |
| 241 | + JsonNode claimNode; |
| 242 | + try { |
| 243 | + JsonNode functionArgNode = new ObjectMapper() |
| 244 | + .readTree(createCredentialPojoFuncArgs.getFunctionArg()); |
| 245 | + cptIdNode = functionArgNode.get(ParamKeyConstant.CPT_ID); |
| 246 | + issuerNode = functionArgNode.get(ParamKeyConstant.ISSUER); |
| 247 | + expirationDateNode = functionArgNode.get(ParamKeyConstant.EXPIRATION_DATE); |
| 248 | + claimNode = functionArgNode.get(ParamKeyConstant.CLAIM); |
| 249 | + if (cptIdNode == null || StringUtils.isEmpty(cptIdNode.toString()) |
| 250 | + || issuerNode == null || StringUtils.isEmpty(issuerNode.textValue()) |
| 251 | + || expirationDateNode == null || StringUtils.isEmpty(expirationDateNode.textValue()) |
| 252 | + || claimNode == null || StringUtils.isEmpty(claimNode.toString())) { |
| 253 | + return new HttpResponseData<>(null, HttpReturnCode.INPUT_NULL); |
| 254 | + } |
| 255 | + } catch (Exception e) { |
| 256 | + logger.error("[createCredentialPojoInvoke]: input args error: {}", createCredentialPojoFuncArgs, e); |
| 257 | + return new HttpResponseData<>(null, HttpReturnCode.VALUE_FORMAT_ILLEGAL); |
| 258 | + } |
| 259 | + |
| 260 | + Integer cptId; |
| 261 | + try { |
| 262 | + cptId = Integer.valueOf(JsonUtil.removeDoubleQuotes(cptIdNode.toString())); |
| 263 | + } catch (Exception e) { |
| 264 | + return new HttpResponseData<>(null, HttpReturnCode.VALUE_FORMAT_ILLEGAL); |
| 265 | + } |
| 266 | + |
| 267 | + Long expirationDate; |
| 268 | + try { |
| 269 | + expirationDate = DateUtils |
| 270 | + .convertUtcDateToTimeStamp(expirationDateNode.textValue()); |
| 271 | + } catch (Exception e) { |
| 272 | + return new HttpResponseData<>(null, |
| 273 | + ErrorCode.CREDENTIAL_EXPIRE_DATE_ILLEGAL.getCode(), |
| 274 | + ErrorCode.CREDENTIAL_EXPIRE_DATE_ILLEGAL.getCodeDesc()); |
| 275 | + } |
| 276 | + |
| 277 | + CredentialPojo credential = new CredentialPojo(); |
| 278 | + credential.setId(UUID.randomUUID().toString()); |
| 279 | + credential.setCptId(cptId); |
| 280 | + credential.setIssuer(issuerNode.textValue()); |
| 281 | + credential.setExpirationDate(expirationDate); |
| 282 | + credential.setContext(CredentialConstant.DEFAULT_CREDENTIAL_CONTEXT); |
| 283 | + credential.setIssuanceDate(DateUtils.getNoMillisecondTimeStamp()); |
| 284 | + Map<String, Object> claimMap; |
| 285 | + try { |
| 286 | + claimMap = (Map<String, Object>) JsonUtil |
| 287 | + .jsonStrToObj(new HashMap<String, Object>(), claimNode.toString()); |
| 288 | + } catch (Exception e) { |
| 289 | + return new HttpResponseData<>(null, |
| 290 | + ErrorCode.CREDENTIAL_CLAIM_DATA_ILLEGAL.getCode(), |
| 291 | + ErrorCode.CREDENTIAL_CLAIM_DATA_ILLEGAL.getCodeDesc()); |
| 292 | + } |
| 293 | + credential.setClaim(claimMap); |
| 294 | + |
| 295 | + WeIdAuthentication weIdAuthentication; |
| 296 | + try { |
| 297 | + JsonNode txnArgNode = new ObjectMapper().readTree(createCredentialPojoFuncArgs.getTransactionArg()); |
| 298 | + JsonNode keyIndexNode = txnArgNode.get(WeIdentityParamKeyConstant.KEY_INDEX); |
| 299 | + String privateKey = KeyUtil |
| 300 | + .getPrivateKeyByWeId(KeyUtil.SDK_PRIVKEY_PATH, keyIndexNode.textValue()); |
| 301 | + weIdAuthentication = KeyUtil.buildWeIdAuthenticationFromPrivKey(privateKey); |
| 302 | + if (weIdAuthentication == null) { |
| 303 | + return new HttpResponseData<>(null, HttpReturnCode.INVOKER_ILLEGAL); |
| 304 | + } |
| 305 | + } catch (Exception e) { |
| 306 | + return new HttpResponseData<>(null, ErrorCode.CREDENTIAL_PRIVATE_KEY_NOT_EXISTS.getCode(), |
| 307 | + ErrorCode.CREDENTIAL_PRIVATE_KEY_NOT_EXISTS.getCodeDesc()); |
| 308 | + } |
| 309 | + |
| 310 | + // Client-side check of validity |
| 311 | + CreateCredentialPojoArgs createArg = new CreateCredentialPojoArgs(); |
| 312 | + createArg.setClaim(claimMap); |
| 313 | + createArg.setCptId(cptId); |
| 314 | + createArg.setIssuer(issuerNode.textValue()); |
| 315 | + createArg.setIssuanceDate(credential.getIssuanceDate()); |
| 316 | + createArg.setExpirationDate(expirationDate); |
| 317 | + createArg.setContext(credential.getContext()); |
| 318 | + createArg.setId(credential.getId()); |
| 319 | + createArg.setWeIdAuthentication(weIdAuthentication); |
| 320 | + ErrorCode errorCode = CredentialPojoUtils.isCreateCredentialPojoArgsValid(createArg); |
| 321 | + if (errorCode.getCode() != ErrorCode.SUCCESS.getCode()) { |
| 322 | + return new HttpResponseData<>(null, errorCode.getCode(), |
| 323 | + errorCode.getCodeDesc()); |
| 324 | + } |
| 325 | + ResponseData<CredentialPojo> createResp = credentialPojoService.createCredential(createArg); |
| 326 | + // TODO unify with Credential |
| 327 | + Map<String, Object> credMap = (Map<String, Object>) JsonUtil.jsonStrToObj(new HashMap<String, Object>(), |
| 328 | + DataToolUtils.serialize(createResp.getResult())); |
| 329 | + return new HttpResponseData<>(credMap, createResp.getErrorCode(), createResp.getErrorMessage()); |
| 330 | + } |
| 331 | + |
| 332 | + @Override |
| 333 | + public HttpResponseData<Boolean> verifyCredentialPojoInvoke(InputArg verifyCredentialPojoFuncArgs) { |
| 334 | + CredentialPojo credential; |
| 335 | + try { |
| 336 | + credential = DataToolUtils.deserialize(verifyCredentialPojoFuncArgs.getFunctionArg(), CredentialPojo.class); |
| 337 | + } catch (Exception e) { |
| 338 | + logger.error("Input credential format illegal: {}", verifyCredentialPojoFuncArgs); |
| 339 | + return new HttpResponseData<>(null, HttpReturnCode.INPUT_ILLEGAL.getCode(), |
| 340 | + HttpReturnCode.INPUT_ILLEGAL.getCodeDesc().concat(e.getMessage())); |
| 341 | + } |
| 342 | + try { |
| 343 | + ResponseData<Boolean> responseData = credentialPojoService.verify(credential.getIssuer(), credential); |
| 344 | + return new HttpResponseData<>(responseData.getResult(), |
| 345 | + responseData.getErrorCode(), responseData.getErrorMessage()); |
| 346 | + } catch (Exception e) { |
| 347 | + logger.error("[verifyCredentialInvoke]: SDK error. reqCredentialArgs:{}", |
| 348 | + verifyCredentialPojoFuncArgs, |
| 349 | + e); |
| 350 | + return new HttpResponseData<>(null, HttpReturnCode.WEID_SDK_ERROR.getCode(), |
| 351 | + HttpReturnCode.WEID_SDK_ERROR.getCodeDesc().concat(e.getMessage())); |
| 352 | + } |
| 353 | + } |
225 | 354 | } |
0 commit comments