use of com.searchcode.app.dto.api.ApiResponse in project searchcode-server by boyter.
the class ApiRouteService method repoDelete.
public ApiResponse repoDelete(Request request, Response response) {
if (!apiEnabled) {
return new ApiResponse(false, "API not enabled");
}
String publicKey = request.queryParams(Values.PUB);
String signedKey = request.queryParams(Values.SIG);
String reponames = request.queryParams(Values.REPONAME);
String hmacTypeString = request.queryParams(Values.HMAC);
hmacTypeString = hmacTypeString == null ? Values.EMPTYSTRING : hmacTypeString;
if (reponames == null || reponames.trim().equals(Values.EMPTYSTRING)) {
return new ApiResponse(false, "reponame is a required parameter");
}
if (apiAuth) {
if (publicKey == null || publicKey.trim().equals(Values.EMPTYSTRING)) {
return new ApiResponse(false, "pub is a required parameter");
}
if (signedKey == null || signedKey.trim().equals(Values.EMPTYSTRING)) {
return new ApiResponse(false, "sig is a required parameter");
}
String toValidate = null;
try {
toValidate = String.format("pub=%s&reponame=%s", URLEncoder.encode(publicKey, java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(reponames, java.nio.charset.StandardCharsets.UTF_8.toString()));
} catch (UnsupportedEncodingException ex) {
this.logger.severe(String.format("940323e9::error in class %s exception %s", ex.getClass(), ex.getMessage()));
return new ApiResponse(false, "invalid signed url");
}
ApiService.HmacType hmacType = hmacTypeString.toLowerCase().equals("sha512") ? ApiService.HmacType.SHA512 : ApiService.HmacType.SHA1;
boolean validRequest = apiService.validateRequest(publicKey, signedKey, toValidate, hmacType);
if (!validRequest) {
this.logger.apiLog("09353e0e::invalid signed repodelete call using publickey=" + publicKey);
return new ApiResponse(false, "invalid signed url");
}
}
Optional<RepoResult> repoResult = this.repo.getRepoByName(reponames);
if (!repoResult.isPresent()) {
return new ApiResponse(false, "repository already deleted");
}
repoResult.ifPresent(x -> this.dataService.addToPersistentDelete(x.getName()));
this.logger.apiLog("bafbae4f::valid signed repodelete call using publickey=" + publicKey);
return new ApiResponse(true, "repository queued for deletion");
}
Aggregations