Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
Added support for jwt tokens
Browse files Browse the repository at this point in the history
- Enabled e2e compute test
  • Loading branch information
r-marques committed Dec 11, 2020
1 parent dc207b0 commit 8f0b9d3
Show file tree
Hide file tree
Showing 12 changed files with 744 additions and 525 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<final-name>nevermined-sdk</final-name>

<web3j.version>4.5.18</web3j.version>
<common-utils.version>0.3.2</common-utils.version>
<common-utils.version>0.4.0</common-utils.version>

<nevermined.contracts.version>0.5.2</nevermined.contracts.version>
<secretstore.version>0.1.0</secretstore.version>
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/keyko/nevermined/api/AssetsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,24 +363,22 @@ public interface AssetsAPI {
* Get the logs for the compute job with executionId and serviceAgreementId
* @param serviceAgreementId The service agreement id for the compute service
* @param executionId The execution id of the compute job
* @param consumerAddress The address of the consumer that executed the compute job
* @param providerConfig Object encapsulating the configuration of the provider
* @return a list of compute logs
* @throws ServiceException ServiceException
*/
List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId, String consumerAddress,
List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId,
ProviderConfig providerConfig) throws ServiceException;

/**
* Get the status for the compute job with executionId and serviceAgreementId
* @param serviceAgreementId The service agreement id for the compute service
* @param executionId The execution id of the compute job
* @param consumerAddress The address of the consumer that executed the compute job
* @param providerConfig Object encapsulating the configuration of the provider
* @return The current status of the compute job
* @throws ServiceException ServiceException
*/
ComputeStatus getComputeStatus(String serviceAgreementId, String executionId, String consumerAddress,
ComputeStatus getComputeStatus(String serviceAgreementId, String executionId,
ProviderConfig providerConfig) throws ServiceException;

}
8 changes: 4 additions & 4 deletions src/main/java/io/keyko/nevermined/api/impl/AssetsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public DDO createComputeService(AssetMetadata metadata, ProviderConfig providerC
}

@Override
public List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId, String consumerAddress,
public List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId,
ProviderConfig providerConfig) throws ServiceException {
return neverminedManager.getComputeLogs(serviceAgreementId, executionId, consumerAddress, providerConfig);
return neverminedManager.getComputeLogs(serviceAgreementId, executionId, providerConfig);
}

@Override
public ComputeStatus getComputeStatus(String serviceAgreementId, String executionId, String consumerAddress,
public ComputeStatus getComputeStatus(String serviceAgreementId, String executionId,
ProviderConfig providerConfig) throws ServiceException {
return neverminedManager.getComputeStatus(serviceAgreementId, executionId, consumerAddress, providerConfig);
return neverminedManager.getComputeStatus(serviceAgreementId, executionId, providerConfig);
}

@Override
Expand Down
302 changes: 194 additions & 108 deletions src/main/java/io/keyko/nevermined/external/GatewayService.java

Large diffs are not rendered by default.

83 changes: 51 additions & 32 deletions src/main/java/io/keyko/nevermined/manager/BaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import io.keyko.common.exceptions.CryptoException;
import io.keyko.common.helpers.EncodingHelper;
import io.keyko.common.helpers.EthereumHelper;
import io.keyko.common.helpers.JwtHelper;
import io.keyko.common.helpers.UrlHelper;
import io.keyko.common.web3.KeeperService;
import io.keyko.nevermined.contracts.*;
Expand Down Expand Up @@ -96,11 +99,10 @@ public void setAccessSecretStoreConditionAddress(String address) {
}
}


/**
* Constructor
*
* @param keeperService KeeperService
* @param keeperService KeeperService
* @param metadataApiService MetadataApiService
*/
public BaseManager(KeeperService keeperService, MetadataApiService metadataApiService) {
Expand All @@ -124,20 +126,24 @@ protected DDO buildDDO(MetadataService metadataService, String address) throws D
}
}

public List<AssetMetadata.File> getDecriptedSecretStoreMetadataFiles(DDO ddo) throws IOException, EncryptionException, InterruptedException {
public List<AssetMetadata.File> getDecriptedSecretStoreMetadataFiles(DDO ddo)
throws IOException, EncryptionException, InterruptedException {
return getDecriptedSecretStoreMetadataFiles(ddo, MAX_SS_RETRIES);
}

public List<AssetMetadata.File> getDecriptedSecretStoreMetadataFiles(DDO ddo, int retries) throws IOException, EncryptionException, InterruptedException {
public List<AssetMetadata.File> getDecriptedSecretStoreMetadataFiles(DDO ddo, int retries)
throws IOException, EncryptionException, InterruptedException {
int counter = 0;
AuthorizationService authorizationService = ddo.getAuthorizationService();
SecretStoreManager secretStoreManager = getSecretStoreInstance(authorizationService);

String jsonFiles= null;
String jsonFiles = null;
while (counter < retries) {
try {
jsonFiles = secretStoreManager.decryptDocument(ddo.getDid().getHash(), ddo.getMetadataService().attributes.encryptedFiles);
return DDO.fromJSON(new TypeReference<ArrayList<AssetMetadata.File>>() {}, jsonFiles);
jsonFiles = secretStoreManager.decryptDocument(ddo.getDid().getHash(),
ddo.getMetadataService().attributes.encryptedFiles);
return DDO.fromJSON(new TypeReference<ArrayList<AssetMetadata.File>>() {
}, jsonFiles);
} catch (EncryptionException e) {
log.warn("Unable to decrypt [" + counter + "]");
counter++;
Expand All @@ -148,16 +154,14 @@ public List<AssetMetadata.File> getDecriptedSecretStoreMetadataFiles(DDO ddo, in

}

public boolean tokenApprove(NeverminedToken tokenContract, String spenderAddress, String price) throws TokenApproveException {
public boolean tokenApprove(NeverminedToken tokenContract, String spenderAddress, String price)
throws TokenApproveException {

String checksumAddress = Keys.toChecksumAddress(spenderAddress);

try {

TransactionReceipt receipt = tokenContract.approve(
checksumAddress,
new BigInteger(price)
).send();
TransactionReceipt receipt = tokenContract.approve(checksumAddress, new BigInteger(price)).send();

if (!receipt.getStatus().equals("0x1")) {
String msg = "The Status received is not valid executing Token Approve: " + receipt.getStatus();
Expand All @@ -183,14 +187,13 @@ public boolean tokenApprove(NeverminedToken tokenContract, String spenderAddress
*
* @param did the did
* @return DDO
* @throws DDOException DDOException
* @throws DDOException DDOException
*/
public DDO resolveDID(DID did) throws DDOException {


try {
final Tuple6<String, byte[], String, String, BigInteger, List<String>> didAttributes =
didRegistry.getDIDRegister(EncodingHelper.hexStringToBytes(did.getHash())).send();
final Tuple6<String, byte[], String, String, BigInteger, List<String>> didAttributes = didRegistry
.getDIDRegister(EncodingHelper.hexStringToBytes(did.getHash())).send();

String didUrl = didAttributes.component3();

Expand All @@ -204,20 +207,17 @@ public DDO resolveDID(DID did) throws DDOException {
}

/**
* Given a DID, scans the DIDRegistry events to resolve the
* Metadata API url and return the DDO found
* Given a DID, scans the DIDRegistry events to resolve the Metadata API url and
* return the DDO found
*
* @param did the did
* @return DDO
* @throws DDOException DDOException
* @throws DDOException DDOException
*/
public DDO resolveDIDFromEvent(DID did) throws DDOException {

EthFilter didFilter = new EthFilter(
DefaultBlockParameterName.EARLIEST,
DefaultBlockParameterName.LATEST,
didRegistry.getContractAddress()
);
EthFilter didFilter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST,
didRegistry.getContractAddress());

try {

Expand All @@ -243,7 +243,8 @@ public DDO resolveDIDFromEvent(DID did) throws DDOException {
throw new DDOException("No events found for " + did.toString());

EthLog.LogResult logResult = logs.get(numLogs - 1);
List<Type> nonIndexed = FunctionReturnDecoder.decode(((EthLog.LogObject) logResult).getData(), event.getNonIndexedParameters());
List<Type> nonIndexed = FunctionReturnDecoder.decode(((EthLog.LogObject) logResult).getData(),
event.getNonIndexedParameters());
String didUrl = nonIndexed.get(0).getValue().toString();

MetadataApiService ddoMetadataDto = MetadataApiService.getInstance(UrlHelper.getBaseUrl(didUrl));
Expand All @@ -255,7 +256,6 @@ public DDO resolveDIDFromEvent(DID did) throws DDOException {
}
}


public ContractAddresses getContractAddresses() {
return contractAddresses;
}
Expand Down Expand Up @@ -350,7 +350,8 @@ public EvmDto getEvmDto() {
}

/**
* Set the EvmDto necessary to stablish the encryption/decryption flow necessary by Secret Store
* Set the EvmDto necessary to stablish the encryption/decryption flow necessary
* by Secret Store
*
* @param evmDto EvmDto
* @return this
Expand Down Expand Up @@ -426,7 +427,6 @@ public BaseManager setAgreementStoreManagerContract(AgreementStoreManager contra
return this;
}


/**
* It sets the AgreementStoreManager stub instance
*
Expand Down Expand Up @@ -513,7 +513,8 @@ public EscrowComputeExecutionTemplate getEscrowComputeExecutionTemplate() {
* @param escrowComputeExecutionTemplate EscrowComputeExecutionTemplate instance
* @return BaseManager instance
*/
public BaseManager setEscrowComputeExecutionTemplate(EscrowComputeExecutionTemplate escrowComputeExecutionTemplate) {
public BaseManager setEscrowComputeExecutionTemplate(
EscrowComputeExecutionTemplate escrowComputeExecutionTemplate) {
this.escrowComputeExecutionTemplate = escrowComputeExecutionTemplate;
return this;
}
Expand All @@ -538,7 +539,6 @@ public BaseManager setAccessSecretStoreCondition(AccessSecretStoreCondition acce
return this;
}


public Account getMainAccount() {
return mainAccount;
}
Expand All @@ -563,8 +563,27 @@ public byte[] getTemplateIdByName(String contractName) {
}

public String generateSignature(String message) throws IOException, CipherException {
return EncodingHelper.signatureToString(
EthereumHelper.signMessage(message, getKeeperService().getCredentials()));
return EncodingHelper
.signatureToString(EthereumHelper.signMessage(message, getKeeperService().getCredentials()));
}

public String generateAccessGrantToken(String serviceAgreementId, DID did)
throws CryptoException, IOException, CipherException {
return JwtHelper.generateAccessGrantToken(getKeeperService().getCredentials(), serviceAgreementId, did.getDid());
}

public String generateDownloadGrantToken(DID did) throws CryptoException, IOException, CipherException {
return JwtHelper.generateDownloadGrantToken(getKeeperService().getCredentials(), did.getDid());
}

public String generateExecuteGrantToken(String serviceAgreementId, DID workflowDid)
throws CryptoException, IOException, CipherException {
return JwtHelper.generateExecuteGrantToken(getKeeperService().getCredentials(), serviceAgreementId, workflowDid.getDid());
}

public String generateComputeGrantToken(String serviceAgreementId, String executionId)
throws CryptoException, IOException, CipherException {
return JwtHelper.generateComputeGrantToken(getKeeperService().getCredentials(), serviceAgreementId, executionId);
}

@Override
Expand Down

0 comments on commit 8f0b9d3

Please sign in to comment.