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);
}
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());
}
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;
}
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;
}
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;
}
Aggregations