Search in sources :

Example 16 with ApiResult

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);
}
Also used : ApiResult(com.searchcode.app.model.ApiResult) Api(com.searchcode.app.dao.Api)

Example 17 with ApiResult

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);
}
Also used : ApiResult(com.searchcode.app.model.ApiResult)

Example 18 with ApiResult

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);
}
Also used : ApiResult(com.searchcode.app.model.ApiResult)

Aggregations

ApiResult (com.searchcode.app.model.ApiResult)18 Api (com.searchcode.app.dao.Api)10 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)1