use of com.searchcode.app.model.ApiResult in project searchcode-server by boyter.
the class ApiServiceTest method testCase1.
public void testCase1() {
Api apiMock = mock(Api.class);
when(apiMock.getApiByPublicKey("publicKey")).thenReturn(new ApiResult(1, "publicKey", "privateKey", "", ""));
ApiService service = new ApiService(apiMock);
boolean actual = service.validateRequest("publicKey", "e15db69d711f0f25ce07a9c11ebebe821e6fc312", "", ApiService.HmacType.SHA1);
assertTrue(actual);
}
use of com.searchcode.app.model.ApiResult in project searchcode-server by boyter.
the class ApiService method validateRequest.
/**
* Validates a request made to the API against the public key supplied, the hmac supplied and the
* query string itself.
* http://stackoverflow.com/questions/11830338/web-api-creating-api-keys
* http://stackoverflow.com/questions/6312544/hmac-sha1-how-to-do-it-properly-in-java
* http://stackoverflow.com/questions/3208160/how-to-generate-an-hmac-in-java-equivalent-to-a-python-example?rq=1
*
*/
public boolean validateRequest(String publicKey, String hmac, String query, HmacType hmacType) {
ApiResult apiResult = this.api.getApiByPublicKey(publicKey);
if (apiResult == null) {
return false;
}
String myHmac;
switch(hmacType) {
case SHA512:
myHmac = HmacUtils.hmacSha512Hex(apiResult.getPrivateKey(), query);
break;
default:
myHmac = HmacUtils.hmacSha1Hex(apiResult.getPrivateKey(), query);
break;
}
return myHmac.equals(hmac);
}
use of com.searchcode.app.model.ApiResult in project searchcode-server by boyter.
the class ApiTest method testSaveDelete.
public void testSaveDelete() {
String randomApiString = this.getRandomString();
this.api.saveApi(new ApiResult(0, randomApiString, "privateKey", "", ""));
this.api.deleteApiByPublicKey(randomApiString);
}
Aggregations