Skip to content

Commit

Permalink
core: rename token to apiKey, #TASK-4791, #TASK-4641
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Aug 23, 2023
1 parent 11a0934 commit 39d17e7
Show file tree
Hide file tree
Showing 45 changed files with 416 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.beust.jcommander.*;
import org.opencb.cellbase.app.cli.CliOptionsParser;
import org.opencb.cellbase.core.token.TokenQuota;
import org.opencb.cellbase.core.api.key.ApiKeyQuota;

import java.util.HashMap;
import java.util.List;
Expand All @@ -35,7 +35,7 @@ public class AdminCliOptionsParser extends CliOptionsParser {
private DownloadCommandOptions downloadCommandOptions;
private BuildCommandOptions buildCommandOptions;
private DataReleaseCommandOptions dataReleaseCommandOptions;
private DataTokenCommandOptions dataTokenCommandOptions;
private ApiKeyCommandOptions apiKeyCommandOptions;
private LoadCommandOptions loadCommandOptions;
private ExportCommandOptions exportCommandOptions;
private CustomiseCommandOptions customiseCommandOptions;
Expand All @@ -52,7 +52,7 @@ public AdminCliOptionsParser() {
downloadCommandOptions = new DownloadCommandOptions();
buildCommandOptions = new BuildCommandOptions();
dataReleaseCommandOptions = new DataReleaseCommandOptions();
dataTokenCommandOptions = new DataTokenCommandOptions();
apiKeyCommandOptions = new ApiKeyCommandOptions();
loadCommandOptions = new LoadCommandOptions();
exportCommandOptions = new ExportCommandOptions();
customiseCommandOptions = new CustomiseCommandOptions();
Expand All @@ -64,7 +64,7 @@ public AdminCliOptionsParser() {
jCommander.addCommand("download", downloadCommandOptions);
jCommander.addCommand("build", buildCommandOptions);
jCommander.addCommand("data-release", dataReleaseCommandOptions);
jCommander.addCommand("data-token", dataTokenCommandOptions);
jCommander.addCommand("api-key", apiKeyCommandOptions);
jCommander.addCommand("load", loadCommandOptions);
jCommander.addCommand("export", exportCommandOptions);
jCommander.addCommand("customise", customiseCommandOptions);
Expand Down Expand Up @@ -153,31 +153,31 @@ public class DataReleaseCommandOptions {
public String versions;
}

@Parameters(commandNames = {"data-token"}, commandDescription = "Manage data access tokens in order to access to restricted/licensed data sources")
public class DataTokenCommandOptions {
@Parameters(commandNames = {"api-key"}, commandDescription = "Manage API keys in order to access to restricted/licensed data sources and set quota")
public class ApiKeyCommandOptions {

@ParametersDelegate
public CommonCommandOptions commonOptions = commonCommandOptions;

@Parameter(names = {"--create-token"}, description = "Create a data access token with the data sources indicated separated by"
@Parameter(names = {"--create-api-key"}, description = "Create an API key with the licensed data sources indicated separated by"
+ " commas and optionally the expiration date: source[:dd/mm/yyyy]. e.g.: cosmic:31/01/2025,hgmd. In addition the"
+ " 'organization' has to be specified", arity = 1)
public String createWithDataSources;

@Parameter(names = {"--expiration"}, description = "Use this parameter in conjunction with --create-token to specify the"
@Parameter(names = {"--expiration"}, description = "Use this parameter in conjunction with --create-api-key to specify the"
+ " expiration date in format dd/mm/yyyy, e.g.: 03/09/2030", arity = 1)
public String expiration;

@Parameter(names = {"--organization"}, description = "Use this parameter in conjunction with --create-token to specify the"
@Parameter(names = {"--organization"}, description = "Use this parameter in conjunction with --create-api-key to specify the"
+ " organization", arity = 1)
public String organization;

@Parameter(names = {"--max-num-queries"}, description = "Use this parameter in conjunction with --create-token to specify the"
@Parameter(names = {"--max-num-queries"}, description = "Use this parameter in conjunction with --create-api-key to specify the"
+ " maximum number of queries per month", arity = 1)
public long maxNumQueries = TokenQuota.DEFAULT_MAX_NUM_QUERIES;
public long maxNumQueries = ApiKeyQuota.DEFAULT_MAX_NUM_QUERIES;

@Parameter(names = {"--view-token"}, description = "Token to view", arity = 1)
public String tokenToView;
@Parameter(names = {"--view-api-key"}, description = "API key to view", arity = 1)
public String apiKeyToView;
}

@Parameters(commandNames = {"load"}, commandDescription = "Load the built data models into the database")
Expand Down Expand Up @@ -245,8 +245,8 @@ public class ExportCommandOptions {
@Parameter(names = {"--data-release"}, description = "Data release for exporting data.", required = true, arity = 1)
public int dataRelease;

@Parameter(names = {"--token"}, description = "Data token to export licensed data.", arity = 1)
public String token;
@Parameter(names = {"--api-key"}, description = "API key to export licensed data.", arity = 1)
public String apiKey;

@Parameter(names = {"--gene"}, description = "List of genes (separated by commas). Exported data will be related to these genes"
+ " (gene coordinates will be taken into account).", required = true, arity = 1)
Expand Down Expand Up @@ -353,8 +353,8 @@ public class ValidationCommandOptions {
@Parameter(names = {"--data-release"}, description = "Data release. To use the default data release, please, set this parameter to 0", required = false, arity = 1)
public int dataRelease = 0;

@Parameter(names = {"--token"}, description = "Data token to get access to licensed/restricted data sources such as COSMIC or HGMD", required = false, arity = 1)
public String token;
@Parameter(names = {"--api-key"}, description = "API key to get access to licensed/restricted data sources such as COSMIC or HGMD", required = false, arity = 1)
public String apiKey;

@Parameter(names = {"-i", "--input-file"}, description = "Full path to VCF", required = true, arity = 1)
public String inputFile;
Expand Down Expand Up @@ -405,9 +405,7 @@ public DataReleaseCommandOptions getDataReleaseCommandOptions() {
return dataReleaseCommandOptions;
}

public DataTokenCommandOptions getDataTokenCommandOptions() {
return dataTokenCommandOptions;
}
public ApiKeyCommandOptions getApiKeyCommandOptions() {return apiKeyCommandOptions; }

public LoadCommandOptions getLoadCommandOptions() { return loadCommandOptions; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public static void main(String[] args) {
case "data-release":
commandExecutor = new DataReleaseCommandExecutor(cliOptionsParser.getDataReleaseCommandOptions());
break;
case "data-token":
commandExecutor = new DataTokenCommandExecutor(cliOptionsParser.getDataTokenCommandOptions());
case "api-key":
commandExecutor = new ApiKeyCommandExecutor(cliOptionsParser.getApiKeyCommandOptions());
break;
case "load":
commandExecutor = new LoadCommandExecutor(cliOptionsParser.getLoadCommandOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import org.apache.commons.lang3.StringUtils;
import org.opencb.cellbase.app.cli.CommandExecutor;
import org.opencb.cellbase.app.cli.admin.AdminCliOptionsParser;
import org.opencb.cellbase.core.token.DataAccessTokenManager;
import org.opencb.cellbase.core.token.TokenJwtPayload;
import org.opencb.cellbase.core.token.TokenQuota;
import org.opencb.cellbase.core.api.key.ApiKeyJwtPayload;
import org.opencb.cellbase.core.api.key.ApiKeyManager;
import org.opencb.cellbase.core.api.key.ApiKeyQuota;

import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
Expand All @@ -35,17 +35,17 @@
import java.util.HashMap;
import java.util.Map;

public class DataTokenCommandExecutor extends CommandExecutor {
public class ApiKeyCommandExecutor extends CommandExecutor {

private AdminCliOptionsParser.DataTokenCommandOptions dataTokenCommandOptions;
private AdminCliOptionsParser.ApiKeyCommandOptions apiKeyCommandOptions;

private DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");


public DataTokenCommandExecutor(AdminCliOptionsParser.DataTokenCommandOptions dataTokenCommandOptions) {
super(dataTokenCommandOptions.commonOptions.logLevel, dataTokenCommandOptions.commonOptions.conf);
public ApiKeyCommandExecutor(AdminCliOptionsParser.ApiKeyCommandOptions apiKeyCommandOptions) {
super(apiKeyCommandOptions.commonOptions.logLevel, apiKeyCommandOptions.commonOptions.conf);

this.dataTokenCommandOptions = dataTokenCommandOptions;
this.apiKeyCommandOptions = apiKeyCommandOptions;
}


Expand All @@ -57,27 +57,27 @@ public void execute() {

Key key = new SecretKeySpec(Base64.getEncoder().encode(configuration.getSecretKey().getBytes(StandardCharsets.UTF_8)),
SignatureAlgorithm.HS256.getJcaName());
DataAccessTokenManager datManager = new DataAccessTokenManager(SignatureAlgorithm.HS256.getValue(), key);
ApiKeyManager apiKeyManager = new ApiKeyManager(SignatureAlgorithm.HS256.getValue(), key);

try {
if (StringUtils.isNotEmpty(dataTokenCommandOptions.createWithDataSources)) {
// Create the token payload
TokenJwtPayload payload = new TokenJwtPayload();
payload.setSubject(dataTokenCommandOptions.organization);
payload.setVersion(TokenJwtPayload.CURRENT_VERSION);
if (StringUtils.isNotEmpty(apiKeyCommandOptions.createWithDataSources)) {
// Create the API key JWT payload
ApiKeyJwtPayload payload = new ApiKeyJwtPayload();
payload.setSubject(apiKeyCommandOptions.organization);
payload.setVersion(ApiKeyJwtPayload.CURRENT_VERSION);
payload.setIssuedAt(new Date());
if (dataTokenCommandOptions.expiration != null) {
payload.setExpiration(parseDate(dataTokenCommandOptions.expiration));
if (apiKeyCommandOptions.expiration != null) {
payload.setExpiration(parseDate(apiKeyCommandOptions.expiration));
}
payload.setSources(parseSources(dataTokenCommandOptions.createWithDataSources));
payload.setQuota(new TokenQuota(dataTokenCommandOptions.maxNumQueries));

// Create token
String token = datManager.encode(payload);
System.out.println("Data access token generated:\n" + token);
} else if (StringUtils.isNotEmpty(dataTokenCommandOptions.tokenToView)) {
// View data token
datManager.display(dataTokenCommandOptions.tokenToView);
payload.setSources(parseSources(apiKeyCommandOptions.createWithDataSources));
payload.setQuota(new ApiKeyQuota(apiKeyCommandOptions.maxNumQueries));

// Create API key
String apiKey = apiKeyManager.encode(payload);
System.out.println("API key generated:\n" + apiKey);
} else if (StringUtils.isNotEmpty(apiKeyCommandOptions.apiKeyToView)) {
// View API key
apiKeyManager.display(apiKeyCommandOptions.apiKeyToView);
}
} catch (ParseException e) {
e.printStackTrace();
Expand All @@ -86,18 +86,18 @@ public void execute() {
}

private void checkParameters() {
if (StringUtils.isNotEmpty(dataTokenCommandOptions.createWithDataSources)
&& StringUtils.isNotEmpty(dataTokenCommandOptions.tokenToView)) {
if (StringUtils.isNotEmpty(apiKeyCommandOptions.createWithDataSources)
&& StringUtils.isNotEmpty(apiKeyCommandOptions.apiKeyToView)) {
throw new IllegalArgumentException("Please, select only one of these input parameters: create or view");
}
if (StringUtils.isEmpty(dataTokenCommandOptions.createWithDataSources)
&& StringUtils.isEmpty(dataTokenCommandOptions.tokenToView)) {
if (StringUtils.isEmpty(apiKeyCommandOptions.createWithDataSources)
&& StringUtils.isEmpty(apiKeyCommandOptions.apiKeyToView)) {
throw new IllegalArgumentException("Please, it is mandatory to select one of these input parameters: create or view");
}

// Check create parameters
if (StringUtils.isNotEmpty(dataTokenCommandOptions.createWithDataSources)) {
if (StringUtils.isEmpty(dataTokenCommandOptions.organization)) {
if (StringUtils.isNotEmpty(apiKeyCommandOptions.createWithDataSources)) {
if (StringUtils.isEmpty(apiKeyCommandOptions.organization)) {
throw new IllegalArgumentException("Missing organization");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class ExportCommandExecutor extends CommandExecutor {
private Path output;
private String[] dataToExport;
private int dataRelease;
private String token;
private String apiKey;

private String database;
private CellBaseManagerFactory managerFactory;
Expand All @@ -71,7 +71,7 @@ public ExportCommandExecutor(AdminCliOptionsParser.ExportCommandOptions exportCo
this.exportCommandOptions = exportCommandOptions;

this.dataRelease = exportCommandOptions.dataRelease;
this.token = exportCommandOptions.token;
this.apiKey = exportCommandOptions.apiKey;

this.output = Paths.get(exportCommandOptions.output);

Expand Down Expand Up @@ -324,7 +324,7 @@ private int exportClinicalVariantData(List<Region> regions) throws CellBaseExcep
ClinicalManager clinicalManager = managerFactory.getClinicalManager(species, assembly);
ClinicalVariantQuery query = new ClinicalVariantQuery();
query.setDataRelease(dataRelease);
query.setToken(token);
query.setApiKey(apiKey);
int counter = 0;
for (Region region : regions) {
query.setRegions(Collections.singletonList(region));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void execute() {
VariantAnnotationCalculator variantAnnotationCalculator = null;
try {
variantAnnotationCalculator = new VariantAnnotationCalculator(validationCommandOptions.species,
validationCommandOptions.assembly, validationCommandOptions.dataRelease, validationCommandOptions.token,
validationCommandOptions.assembly, validationCommandOptions.dataRelease, validationCommandOptions.apiKey,
cellBaseManagerFactory);
} catch (CellBaseException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class CellBaseClient {
private final String species;
private final String assembly;
private final String dataRelease;
private final String apiKey;
@Deprecated
private final String token;
private ClientConfiguration clientConfiguration;

Expand All @@ -53,14 +55,15 @@ public CellBaseClient(String species, String assembly, String dataRelease, Clien
this(species, assembly, dataRelease, null, clientConfiguration);
}

public CellBaseClient(String species, String assembly, String dataRelease, String token, ClientConfiguration clientConfiguration) {
public CellBaseClient(String species, String assembly, String dataRelease, String apiKey, ClientConfiguration clientConfiguration) {
if (StringUtils.isBlank(species)) {
throw new IllegalArgumentException("Species parameter cannot be empty when building a CellBaseClient");
}
this.species = species;
this.assembly = StringUtils.isEmpty(assembly) ? null : assembly;
this.dataRelease = StringUtils.isEmpty(dataRelease) ? null : dataRelease;
this.token = token;
this.token = apiKey;
this.apiKey = apiKey;
if (clientConfiguration != null && clientConfiguration.getRest() != null
&& clientConfiguration.getRest().getHosts() != null
&& !clientConfiguration.getRest().getHosts().isEmpty()
Expand All @@ -75,39 +78,39 @@ public CellBaseClient(String species, String assembly, String dataRelease, Strin
}

public GeneClient getGeneClient() {
return getClient("GENE", () -> new GeneClient(species, assembly, dataRelease, token, clientConfiguration));
return getClient("GENE", () -> new GeneClient(species, assembly, dataRelease, apiKey, clientConfiguration));
}

public TranscriptClient getTranscriptClient() {
return getClient("TRANSCRIPT", () -> new TranscriptClient(species, assembly, dataRelease, token, clientConfiguration));
return getClient("TRANSCRIPT", () -> new TranscriptClient(species, assembly, dataRelease, apiKey, clientConfiguration));
}

// public VariationClient getVariationClient() {
// return getClient("VARIATION", () -> new VariationClient(species, assembly, clientConfiguration));
// }

public VariantClient getVariantClient() {
return getClient("VARIANT", () -> new VariantClient(species, assembly, dataRelease, token, clientConfiguration));
return getClient("VARIANT", () -> new VariantClient(species, assembly, dataRelease, apiKey, clientConfiguration));
}

public ProteinClient getProteinClient() {
return getClient("PROTEIN", () -> new ProteinClient(species, assembly, dataRelease, token, clientConfiguration));
return getClient("PROTEIN", () -> new ProteinClient(species, assembly, dataRelease, apiKey, clientConfiguration));
}

public GenomicRegionClient getGenomicRegionClient() {
return getClient("GENOME_REGION", () -> new GenomicRegionClient(species, assembly, dataRelease, token, clientConfiguration));
return getClient("GENOME_REGION", () -> new GenomicRegionClient(species, assembly, dataRelease, apiKey, clientConfiguration));
}

public MetaClient getMetaClient() {
return getClient("META", () -> new MetaClient(species, assembly, dataRelease, token, clientConfiguration));
return getClient("META", () -> new MetaClient(species, assembly, dataRelease, apiKey, clientConfiguration));
}

public ClinicalVariantClient getClinicalClient() {
return getClient("CLINICAL", () -> new ClinicalVariantClient(species, assembly, dataRelease, token, clientConfiguration));
return getClient("CLINICAL", () -> new ClinicalVariantClient(species, assembly, dataRelease, apiKey, clientConfiguration));
}

public GenericClient getGenericClient() {
return getClient("GENERIC", () -> new GenericClient(species, assembly, dataRelease, token, clientConfiguration));
return getClient("GENERIC", () -> new GenericClient(species, assembly, dataRelease, apiKey, clientConfiguration));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -144,8 +147,13 @@ public String getDataRelease() {
return dataRelease;
}

public String getApiKey() {
return apiKey;
}

@Deprecated
public String getToken() {
return token;
return apiKey;
}

public ClientConfiguration getClientConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
*/
public class ClinicalVariantClient extends ParentRestClient<Variant> {

ClinicalVariantClient(String species, String assembly, String dataRelease, String token, ClientConfiguration configuration) {
super(species, assembly, dataRelease, token, configuration);
ClinicalVariantClient(String species, String assembly, String dataRelease, String apiKey, ClientConfiguration configuration) {
super(species, assembly, dataRelease, apiKey, configuration);

this.clazz = Variant.class;

Expand Down
Loading

0 comments on commit 39d17e7

Please sign in to comment.