Search in sources :

Example 6 with ApiResult

use of com.searchcode.app.model.ApiResult in project searchcode-server by boyter.

the class ApiServiceTest method testValidateRequestCorrectHmac.

public void testValidateRequestCorrectHmac() {
    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", "3eb4cb7c8a30ac3814bbfae935cbe3c1f4f2acce", "stringtohmac", ApiService.HmacType.SHA1);
    assertTrue(actual);
}
Also used : ApiResult(com.searchcode.app.model.ApiResult) Api(com.searchcode.app.dao.Api)

Example 7 with ApiResult

use of com.searchcode.app.model.ApiResult in project searchcode-server by boyter.

the class ApiServiceTest method testCreateKeys.

public void testCreateKeys() {
    Api apiMock = mock(Api.class);
    when(apiMock.saveApi(anyObject())).thenReturn(true);
    ApiService service = new ApiService(apiMock);
    ApiResult actual = service.createKeys();
    assertNotNull(actual);
    assertEquals(-1, actual.getRowId());
    assertTrue(actual.getPublicKey().startsWith("APIK-"));
    assertEquals(32, actual.getPublicKey().length());
    assertEquals(32, actual.getPrivateKey().length());
    assertEquals("", actual.getData());
    assertEquals("", actual.getLastUsed());
}
Also used : ApiResult(com.searchcode.app.model.ApiResult) Api(com.searchcode.app.dao.Api)

Example 8 with ApiResult

use of com.searchcode.app.model.ApiResult in project searchcode-server by boyter.

the class ApiService method createKeys.

/**
     * Creates a public and private key with access to the API
     * TODO allow creation of permissions for keys
     */
public ApiResult createKeys() {
    String publicKey = "APIK-" + RandomStringUtils.randomAlphanumeric(27);
    String privateKey = RandomStringUtils.randomAlphanumeric(32);
    ApiResult apiResult = new ApiResult(-1, publicKey, privateKey, Values.EMPTYSTRING, Values.EMPTYSTRING);
    this.api.saveApi(apiResult);
    return apiResult;
}
Also used : ApiResult(com.searchcode.app.model.ApiResult)

Example 9 with ApiResult

use of com.searchcode.app.model.ApiResult in project searchcode-server by boyter.

the class Api method getAllApi.

public synchronized List<ApiResult> getAllApi() {
    List<ApiResult> apiResults = new ArrayList<>();
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = this.dbConfig.getConnection();
        preparedStatement = connection.prepareStatement("select rowid,publickey,privatekey,lastused,data from api;");
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int rowId = resultSet.getInt("rowid");
            String d_publicKey = resultSet.getString("publickey");
            String privateKey = resultSet.getString("privatekey");
            String lastUsed = resultSet.getString("lastused");
            String data = resultSet.getString("data");
            apiResults.add(new ApiResult(rowId, d_publicKey, privateKey, lastUsed, data));
        }
    } catch (SQLException ex) {
        Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    } finally {
        this.helpers.closeQuietly(resultSet);
        this.helpers.closeQuietly(preparedStatement);
    }
    return apiResults;
}
Also used : ApiResult(com.searchcode.app.model.ApiResult) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 10 with ApiResult

use of com.searchcode.app.model.ApiResult in project searchcode-server by boyter.

the class Api method getApiByPublicKey.

public synchronized ApiResult getApiByPublicKey(String publicKey) {
    ApiResult result = null;
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = this.dbConfig.getConnection();
        preparedStatement = connection.prepareStatement("select rowid,publickey,privatekey,lastused,data from api where publickey=?;");
        preparedStatement.setString(1, publicKey);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int rowId = resultSet.getInt("rowid");
            String d_publicKey = resultSet.getString("publickey");
            String privateKey = resultSet.getString("privatekey");
            String lastUsed = resultSet.getString("lastused");
            String data = resultSet.getString("data");
            result = new ApiResult(rowId, d_publicKey, privateKey, lastUsed, data);
        }
    } catch (SQLException ex) {
        Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    } finally {
        this.helpers.closeQuietly(resultSet);
        this.helpers.closeQuietly(preparedStatement);
    }
    return result;
}
Also used : ApiResult(com.searchcode.app.model.ApiResult) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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