-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #660 from opencb/TASK-4641
TASK-4641- Implement an API key to control the number of queries
- Loading branch information
Showing
81 changed files
with
1,207 additions
and
1,203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...-app/src/main/java/org/opencb/cellbase/app/cli/admin/executors/ApiKeyCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2015-2020 OpenCB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.opencb.cellbase.app.cli.admin.executors; | ||
|
||
import io.jsonwebtoken.SignatureAlgorithm; | ||
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.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; | ||
import java.security.Key; | ||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Base64; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ApiKeyCommandExecutor extends CommandExecutor { | ||
|
||
private AdminCliOptionsParser.ApiKeyCommandOptions apiKeyCommandOptions; | ||
|
||
private DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy"); | ||
|
||
|
||
public ApiKeyCommandExecutor(AdminCliOptionsParser.ApiKeyCommandOptions apiKeyCommandOptions) { | ||
super(apiKeyCommandOptions.commonOptions.logLevel, apiKeyCommandOptions.commonOptions.conf); | ||
|
||
this.apiKeyCommandOptions = apiKeyCommandOptions; | ||
} | ||
|
||
|
||
/** | ||
* Execute one of the selected actions according to the input parameters. | ||
*/ | ||
public void execute() { | ||
checkParameters(); | ||
|
||
Key key = new SecretKeySpec(Base64.getEncoder().encode(configuration.getSecretKey().getBytes(StandardCharsets.UTF_8)), | ||
SignatureAlgorithm.HS256.getJcaName()); | ||
ApiKeyManager apiKeyManager = new ApiKeyManager(SignatureAlgorithm.HS256.getValue(), key); | ||
|
||
try { | ||
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 (apiKeyCommandOptions.expiration != null) { | ||
payload.setExpiration(parseDate(apiKeyCommandOptions.expiration)); | ||
} | ||
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(); | ||
} | ||
|
||
} | ||
|
||
private void checkParameters() { | ||
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(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(apiKeyCommandOptions.createWithDataSources)) { | ||
if (StringUtils.isEmpty(apiKeyCommandOptions.organization)) { | ||
throw new IllegalArgumentException("Missing organization"); | ||
} | ||
} | ||
|
||
if (StringUtils.isEmpty(configuration.getSecretKey())) { | ||
throw new IllegalArgumentException("Missing secret key in the CellBase configuration file."); | ||
} | ||
} | ||
|
||
private Map<String, Date> parseSources(String sources) throws ParseException { | ||
Map<String, Date> sourcesMap = new HashMap<>(); | ||
if (StringUtils.isNotEmpty(sources)) { | ||
String[] split = sources.split(","); | ||
for (String source : split) { | ||
String[] splits = source.split(":"); | ||
if (splits.length == 1) { | ||
sourcesMap.put(splits[0], parseDate("31/12/999999")); | ||
} else { | ||
sourcesMap.put(splits[0], parseDate(splits[1])); | ||
} | ||
} | ||
} | ||
return sourcesMap; | ||
} | ||
|
||
private Date parseDate(String date) throws ParseException { | ||
return dateFormatter.parse(date); | ||
} | ||
} |
89 changes: 0 additions & 89 deletions
89
...p/src/main/java/org/opencb/cellbase/app/cli/admin/executors/DataTokenCommandExecutor.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.