Search in sources :

Example 6 with AccessToken

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;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) IntrospectionException(java.beans.IntrospectionException) PreparedStatement(java.sql.PreparedStatement) InvocationTargetException(java.lang.reflect.InvocationTargetException) AccessToken(net.petafuel.styx.core.persistence.models.AccessToken) PaymentEntry(net.petafuel.styx.core.persistence.models.PaymentEntry) ResultSet(java.sql.ResultSet) PersistenceException(net.petafuel.styx.core.persistence.PersistenceException)

Example 7 with AccessToken

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;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) IntrospectionException(java.beans.IntrospectionException) PreparedStatement(java.sql.PreparedStatement) InvocationTargetException(java.lang.reflect.InvocationTargetException) AccessToken(net.petafuel.styx.core.persistence.models.AccessToken) PaymentEntry(net.petafuel.styx.core.persistence.models.PaymentEntry) ResultSet(java.sql.ResultSet) PersistenceException(net.petafuel.styx.core.persistence.PersistenceException)

Example 8 with AccessToken

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;
}
Also used : SQLException(java.sql.SQLException) AccessToken(net.petafuel.styx.core.persistence.models.AccessToken) PersistenceEmptyResultSetException(net.petafuel.styx.core.persistence.PersistenceEmptyResultSetException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 9 with AccessToken

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());
}
Also used : PersistentAccessToken(net.petafuel.styx.core.persistence.layers.PersistentAccessToken) AccessToken(net.petafuel.styx.core.persistence.models.AccessToken) JsonObject(javax.json.JsonObject) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 10 with AccessToken

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));
}
Also used : MasterToken(net.petafuel.styx.core.persistence.models.MasterToken) AccessToken(net.petafuel.styx.core.persistence.models.AccessToken) Test(org.junit.jupiter.api.Test)

Aggregations

AccessToken (net.petafuel.styx.core.persistence.models.AccessToken)18 Connection (java.sql.Connection)12 PreparedStatement (java.sql.PreparedStatement)12 ResultSet (java.sql.ResultSet)12 SQLException (java.sql.SQLException)12 IntrospectionException (java.beans.IntrospectionException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 PersistenceException (net.petafuel.styx.core.persistence.PersistenceException)9 PaymentEntry (net.petafuel.styx.core.persistence.models.PaymentEntry)9 MasterToken (net.petafuel.styx.core.persistence.models.MasterToken)4 Test (org.junit.jupiter.api.Test)4 ResponseEntity (net.petafuel.styx.api.exception.ResponseEntity)3 StyxException (net.petafuel.styx.api.exception.StyxException)3 PersistenceEmptyResultSetException (net.petafuel.styx.core.persistence.PersistenceEmptyResultSetException)2 PersistentAccessToken (net.petafuel.styx.core.persistence.layers.PersistentAccessToken)2 Date (java.util.Date)1 JsonObject (javax.json.JsonObject)1 CheckAccessToken (net.petafuel.styx.api.filter.authentication.boundary.CheckAccessToken)1 TokenTypeMapperService (net.petafuel.styx.spi.tokentypemapper.TokenTypeMapperService)1 XS2ATokenType (net.petafuel.styx.spi.tokentypemapper.api.XS2ATokenType)1