use of net.petafuel.styx.core.persistence.models.AccessToken in project styx by petafuel.
the class PersistentPayment method getById.
public static PaymentEntry getById(String id) {
Connection connection = Persistence.getInstance().getConnection();
PaymentEntry paymentEntry = null;
try (PreparedStatement query = connection.prepareStatement("SELECT * FROM get_payment_by_id(?)")) {
query.setString(1, id);
try (ResultSet resultSet = query.executeQuery()) {
if (resultSet.next()) {
paymentEntry = StyxifySQL.fetchModel(PaymentEntry.class, resultSet);
if (resultSet.getString(COLUMN_CLIENT_TOKEN) != null) {
AccessToken accessToken = PersistentAccessToken.get(resultSet.getString(COLUMN_CLIENT_TOKEN));
paymentEntry.setClientToken(accessToken);
}
if (resultSet.getString(COLUMN_STATUS) != null) {
paymentEntry.setStatus(TransactionStatus.valueOf(resultSet.getString(COLUMN_STATUS)));
}
paymentEntry.setPaymentService(PaymentService.valueOf(resultSet.getString(COLUMN_SERVICE)));
paymentEntry.setPaymentProduct(PaymentProduct.valueOf(resultSet.getString(COLUMN_PRODUCT)));
}
}
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IntrospectionException e) {
LOG.error(ERROR_MODEL_MAPPING, e.getMessage());
throw new PersistenceException(e.getMessage(), e);
} catch (SQLException e) {
LOG.error("Error executing get_payment_by_id() for id={} state={} message={}", id, e.getSQLState(), e.getMessage());
throw new PersistenceException(e.getMessage(), e);
}
return paymentEntry;
}
use of net.petafuel.styx.core.persistence.models.AccessToken in project styx by petafuel.
the class PersistentPayment method updateStatusById.
public static PaymentEntry updateStatusById(String id, TransactionStatus transactionStatus) {
Connection connection = Persistence.getInstance().getConnection();
PaymentEntry paymentEntry = null;
try (PreparedStatement query = connection.prepareStatement("SELECT * FROM update_payment_status_by_id(?, ?)")) {
query.setString(1, id);
query.setString(2, transactionStatus.name());
try (ResultSet resultSet = query.executeQuery()) {
if (resultSet.next()) {
paymentEntry = StyxifySQL.fetchModel(PaymentEntry.class, resultSet);
AccessToken accessToken = PersistentAccessToken.get(resultSet.getString(COLUMN_CLIENT_TOKEN));
paymentEntry.setClientToken(accessToken);
paymentEntry.setStatus(TransactionStatus.valueOf(resultSet.getString(COLUMN_STATUS)));
paymentEntry.setPaymentService(PaymentService.valueOf(resultSet.getString(COLUMN_SERVICE)));
paymentEntry.setPaymentProduct(PaymentProduct.valueOf(resultSet.getString(COLUMN_PRODUCT)));
}
}
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IntrospectionException e) {
LOG.error(ERROR_MODEL_MAPPING, e.getMessage());
throw new PersistenceException(e.getMessage(), e);
} catch (SQLException e) {
LOG.error("Error executing update_payment_status_by_id() for id={} state={} message={}", id, e.getSQLState(), e.getMessage());
throw new PersistenceException(e.getMessage(), e);
}
return paymentEntry;
}
use of net.petafuel.styx.core.persistence.models.AccessToken in project styx by petafuel.
the class PersistentAccessToken method get.
/**
* @param accessToken this should be the hashed styx access token
* @return accesstoken with metadata
* @throws PersistenceException if any sql error happened
* @throws PersistenceEmptyResultSetException if there was no matching token found in the database
*/
public static AccessToken get(String accessToken) {
Connection connection = Persistence.getInstance().getConnection();
AccessToken model = new AccessToken();
try (PreparedStatement query = connection.prepareStatement("SELECT * FROM get_token(?);")) {
query.setString(1, accessToken);
try (ResultSet resultSet = query.executeQuery()) {
if (resultSet.next()) {
model = dbToModel(resultSet);
} else {
throw new PersistenceEmptyResultSetException("No entry found matching the specified token");
}
}
} catch (SQLException e) {
logSQLError(e);
}
return model;
}
use of net.petafuel.styx.core.persistence.models.AccessToken in project styx by petafuel.
the class TokenHashTest method testTokenHashCheck.
@Test
@Tag("integration")
public void testTokenHashCheck() throws NoSuchAlgorithmException {
// handle the request
JsonObject element = AuthenticationHandler.createAccessToken(TokenGenerator.hashSHA256(masterToken), XS2ATokenType.AISPIS.name().toLowerCase(), 300, TokenGenerator.hashSHA256("testRef"));
// expect that there is a "token" in the response and it's a valid UUID
String accessTokenString = element.getString("token");
AccessToken accessToken = PersistentAccessToken.get(TokenGenerator.hashSHA256(accessTokenString));
Assertions.assertEquals(TokenGenerator.hashSHA256(accessTokenString), accessToken.getId());
Assertions.assertEquals(TokenGenerator.hashSHA256("testRef"), accessToken.getClientReference());
Assertions.assertEquals(0, accessToken.getUsages());
}
use of net.petafuel.styx.core.persistence.models.AccessToken in project styx by petafuel.
the class AccessTokenFilterUnitTest method testCheckMaxUsagesNotReached.
@Test
void testCheckMaxUsagesNotReached() {
AccessToken accessToken = new AccessToken();
accessToken.setServiceType("pis");
MasterToken masterToken = prepareMasterToken("pis", 5);
AccessTokenFilter accessTokenFilter = new AccessTokenFilter();
accessToken.setUsages(1);
Assertions.assertDoesNotThrow(() -> accessTokenFilter.checkMaxUsages(masterToken, accessToken));
}
Aggregations