id.unum.service.DidDocService Maven / Gradle / Ivy
The newest version!
package id.unum.service;
//import id.unum.dto.DidDocument;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.protobuf.InvalidProtocolBufferException;
//import id.unum.dto.DidDocument;
import id.unum.dto.Unum;
import id.unum.error.UnumError;
import id.unum.facade.rest.Client;
import id.unum.facade.rest.UnumAPIService;
//import id.unum.protos.crypto.v1.PublicKeyInfo;
import id.unum.protos.crypto.v1.PublicKeyInfo;
import id.unum.protos.didDocument.v1.DidDocument;
import lombok.extern.log4j.Log4j2;
import retrofit2.Call;
import retrofit2.Response;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static id.unum.converter.DtoToProto.convertPublicKeyInfoListToProto;
@Log4j2
public class DidDocService implements DidDocServiceInterface{
private UnumAPIService _unumService;
public DidDocService (String url) {
Client client = new Client(url);
_unumService = client.getRetrofit().create(UnumAPIService.class);
}
@Override
public Unum getDidDocPublicKeyInfoByKeyId(String authToken, String didWithKeyId) {
if (didWithKeyId == null || didWithKeyId.isEmpty()) {
throw new UnumError(500, "Did doc get needs a did.");
} else if (didWithKeyId.length() != 82 || !didWithKeyId.contains("#")) {
throw new UnumError(500, "Did doc get needs a did with key id.");
}
Call call = _unumService.getDidDocKey(authToken, didWithKeyId);
try {
Response response = call.execute();
if (!response.isSuccessful()) {
String message = response.errorBody() != null
? response.errorBody().string() : "Unknown error";
log.error("Saas error calling get did doc " + didWithKeyId + ": " + message);
throw new UnumError(response.errorBody().hashCode(), message);
}
PublicKeyInfo keyInfo = response.body();
if (keyInfo == null) {
throw new UnumError(404, "Did doc with key id " + didWithKeyId + " not found");
}
String newAuthToken = response.headers().get("X-Auth-Token");
Unum result = new Unum<>();
result.setAuthToken(newAuthToken);
result.setBody(keyInfo);
return result;
} catch (IOException e) {
log.error("IOException calling get did doc with key id: " + e.toString());
e.printStackTrace();
}
throw new UnumError(500, "Unknown error getting did doc with key id.");
}
@Override
public Unum getDIDDoc(String authToken, String did) throws UnumError {
if (did == null || did.isEmpty()) {
throw new UnumError(500, "Did doc get needs a did.");
}
Call call = _unumService.getDidDoc(authToken, did);
try {
Response response = call.execute();
if (!response.isSuccessful()) {
String message = response.errorBody() != null
? response.errorBody().string() : "Unknown error";
log.error("Saas error calling get did doc " + did + ": " + message);
throw new UnumError(response.errorBody().hashCode(), message);
}
id.unum.protos.didDocument.v1.DidDocument didDocument = response.body();
if (didDocument == null) {
throw new UnumError(404, "Did document " + did + " not found");
}
String newAuthToken = response.headers().get("X-Auth-Token");
Unum result = new Unum<>();
result.setAuthToken(newAuthToken);
result.setBody(didDocument);
return result;
} catch (IOException e) {
log.error("IOException calling get did doc: " + e.toString());
e.printStackTrace();
}
throw new UnumError(500, "Unknown error getting did doc.");
}
@Override
public List getKeysFromDIDDoc(DidDocument document, String type) {
// List pubKeys =
// document.getPublicKey().stream().filter(publicKeyInfo -> publicKeyInfo.getType().equals(type)).collect(Collectors.toList());
List pubKeys =
document.getPublicKeyList().stream().filter(publicKeyInfo -> publicKeyInfo.getType().equals(type)).collect(Collectors.toList());
if (pubKeys.isEmpty())
throw new UnumError(404, "Public key not found for the DID");
// try {
//// List result = convertPublicKeyInfoListToProto(pubKeys);
// return result;
// } catch (InvalidProtocolBufferException e) {
// e.printStackTrace();
// } catch (JsonProcessingException e) {
// e.printStackTrace();
// }
return pubKeys;
// return new ArrayList<>();
}
@Override
public id.unum.protos.crypto.v1.PublicKeyInfo getKeyFromDIDDoc(DidDocument document, String type) {
List pubKeys = this.getKeysFromDIDDoc(document, type);
// for now assuming only one key type per did doc
return pubKeys.get(0);
}
}