diff --git a/opencga-core/src/main/java/org/opencb/opencga/core/cellbase/CellBaseValidator.java b/opencga-core/src/main/java/org/opencb/opencga/core/cellbase/CellBaseValidator.java index a2c5da2dc72..b4383a2ec2a 100644 --- a/opencga-core/src/main/java/org/opencb/opencga/core/cellbase/CellBaseValidator.java +++ b/opencga-core/src/main/java/org/opencb/opencga/core/cellbase/CellBaseValidator.java @@ -1,6 +1,8 @@ package org.opencb.opencga.core.cellbase; +import io.jsonwebtoken.JwtException; import org.apache.commons.lang3.StringUtils; +import org.opencb.biodata.models.variant.avro.VariantAnnotation; import org.opencb.cellbase.client.rest.CellBaseClient; import org.opencb.cellbase.core.config.SpeciesConfiguration; import org.opencb.cellbase.core.config.SpeciesProperties; @@ -8,16 +10,14 @@ import org.opencb.cellbase.core.result.CellBaseDataResponse; import org.opencb.cellbase.core.token.DataAccessTokenManager; import org.opencb.cellbase.core.token.DataAccessTokenSources; +import org.opencb.commons.datastore.core.QueryOptions; import org.opencb.opencga.core.common.VersionUtils; import org.opencb.opencga.core.config.storage.CellBaseConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Optional; +import java.util.*; public class CellBaseValidator { @@ -46,6 +46,7 @@ private CellBaseClient newCellBaseClient(CellBaseConfiguration cellBaseConfigura toCellBaseSpeciesName(species), assembly, cellBaseConfiguration.getDataRelease(), + cellBaseConfiguration.getToken(), cellBaseConfiguration.toClientConfiguration()); } @@ -120,7 +121,7 @@ public void validate() throws IOException { validate(false); } - public CellBaseConfiguration validate(boolean autoComplete) throws IOException { + private CellBaseConfiguration validate(boolean autoComplete) throws IOException { CellBaseConfiguration cellBaseConfiguration = getCellBaseConfiguration(); String inputVersion = getVersion(); CellBaseDataResponse species; @@ -173,6 +174,38 @@ public CellBaseConfiguration validate(boolean autoComplete) throws IOException { } } } + if (getToken() != null) { + // Check it's supported + if (!supportsToken(serverVersion)) { + throw new IllegalArgumentException("Token not supported for cellbase " + + "url: '" + getURL() + "'" + + ", version: '" + inputVersion + "'"); + } + + // Check it's an actual token + DataAccessTokenManager tokenManager = new DataAccessTokenManager(); + try { + tokenManager.decode(getToken()); + } catch (JwtException e) { + throw new IllegalArgumentException("Malformed token for cellbase " + + "url: '" + getURL() + "'" + + ", version: '" + inputVersion + + "', species: '" + getSpecies() + + "', assembly: '" + getAssembly() + "'"); + } + + // Check it's a valid token + CellBaseDataResponse response = cellBaseClient.getVariantClient() + .getAnnotationByVariantIds(Collections.singletonList("1:1:N:C"), new QueryOptions(), true); + if (response.firstResult() == null) { + throw new IllegalArgumentException("Invalid token for cellbase " + + "url: '" + getURL() + "'" + + ", version: '" + inputVersion + + "', species: '" + getSpecies() + + "', assembly: '" + getAssembly() + "'"); + } + } + return cellBaseConfiguration; } @@ -206,6 +239,11 @@ public static boolean supportsDataRelease(String serverVersion) { return VersionUtils.isMinVersion("5.1.0", serverVersion); } + public static boolean supportsToken(String serverVersion) { + // Tokens support starts at version 5.4.0 + return VersionUtils.isMinVersion("5.4.0", serverVersion); + } + public String getVersionFromServerMajor() throws IOException { return major(getVersionFromServer()); } diff --git a/opencga-core/src/test/java/org/opencb/opencga/core/cellbase/CellBaseValidatorTest.java b/opencga-core/src/test/java/org/opencb/opencga/core/cellbase/CellBaseValidatorTest.java index 36db80985f0..38d91dd0fc8 100644 --- a/opencga-core/src/test/java/org/opencb/opencga/core/cellbase/CellBaseValidatorTest.java +++ b/opencga-core/src/test/java/org/opencb/opencga/core/cellbase/CellBaseValidatorTest.java @@ -1,6 +1,8 @@ package org.opencb.opencga.core.cellbase; +import org.apache.commons.lang3.StringUtils; import org.junit.Assert; +import org.junit.Assume; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -70,4 +72,37 @@ public void testNoActiveReleases() throws IOException { thrown.expectMessage("No active data releases found on cellbase"); CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.1", null, null), "mmusculus", "GRCm38", true); } + + @Test + public void testToken() throws IOException { + String token = System.getenv("CELLBASE_HGMD_TOKEN"); + Assume.assumeTrue(StringUtils.isNotEmpty(token)); + CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.4", null, token), "hsapiens", "grch38", true); + Assert.assertNotNull(validated.getToken()); + } + + @Test + public void testTokenNotSupported() throws IOException { + String token = System.getenv("CELLBASE_HGMD_TOKEN"); + Assume.assumeTrue(StringUtils.isNotEmpty(token)); + CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.1", null, token), "hsapiens", "grch38", true); + Assert.assertNotNull(validated.getToken()); + } + + @Test + public void testMalformedToken() throws IOException { + thrown.expectMessage("Malformed token for cellbase"); + String token = "MALFORMED_TOKEN"; + CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.4", null, token), "hsapiens", "grch38", true); + Assert.assertNotNull(validated.getToken()); + } + + @Test + public void testUnsignedToken() throws IOException { + thrown.expectMessage("Invalid token for cellbase"); + String token = "eyJhbGciOiJIUzI1NiJ9.eyJzb3VyY2VzIjp7ImhnbWQiOjkyMjMzNzIwMzY4NTQ3NzU4MDd9LCJ2ZXJzaW9uIjoiMS4wIiwic3ViIjoiWkVUVEEiLCJpYXQiOjE2OTMyMTY5MDd9.invalidsignature"; + CellBaseConfiguration validated = CellBaseValidator.validate(new CellBaseConfiguration(ParamConstants.CELLBASE_URL, "v5.4", null, token), "hsapiens", "grch38", true); + Assert.assertNotNull(validated.getToken()); + } + } \ No newline at end of file