Search in sources :

Example 6 with ResponseEntity

use of net.petafuel.styx.api.exception.ResponseEntity in project styx by petafuel.

the class STYX09 method generateINGAccessToken.

public void generateINGAccessToken(String url) {
    AccessTokenService service = new AccessTokenService();
    AccessTokenRequest request = new AccessTokenRequest();
    try {
        AccessToken retrievedAccessToken = service.tokenRequest(url + "/oauth2/token", request);
        // give a tolerance of 30 seconds to the expiry date in case of any software
        // related delays
        this.accessTokenValidUntil = Instant.now().plusSeconds((retrievedAccessToken.getExpiresIn() - 30));
        this.accessToken = retrievedAccessToken;
    } catch (BankRequestFailedException e) {
        LOG.error("Error getting ing access token:", e);
        ResponseEntity responseEntity = new ResponseEntity("Generating ING access token failed", ResponseConstant.INTERNAL_SERVER_ERROR, ResponseCategory.ERROR, ResponseOrigin.STYX);
        throw new StyxException(responseEntity);
    }
}
Also used : ResponseEntity(net.petafuel.styx.api.exception.ResponseEntity) AccessTokenService(net.petafuel.styx.core.xs2a.standards.ing.v1_0.services.AccessTokenService) AccessToken(net.petafuel.styx.core.xs2a.standards.ing.v1_0.entities.AccessToken) AccessTokenRequest(net.petafuel.styx.core.xs2a.standards.ing.v1_0.http.AccessTokenRequest) BankRequestFailedException(net.petafuel.styx.core.xs2a.exceptions.BankRequestFailedException) StyxException(net.petafuel.styx.api.exception.StyxException)

Example 7 with ResponseEntity

use of net.petafuel.styx.api.exception.ResponseEntity in project styx by petafuel.

the class SADResourceTest method GetAspspDataWrongBicTest.

@Test
@Category(IntegrationTest.class)
public void GetAspspDataWrongBicTest() {
    Invocation.Builder invocationBuilder = target("/v1/aspsp/" + WRONG_BIC).request();
    invocationBuilder.header("token", pisAccessToken);
    invocationBuilder.header("Content-Type", "application/json");
    Invocation invocation = invocationBuilder.buildGet();
    Response response = invocation.invoke(Response.class);
    Assertions.assertEquals(409, response.getStatus());
    ResponseEntity responseEntity = response.readEntity(ResponseEntity.class);
    Assertions.assertEquals("The requested ASPSP was not found within SAD for BIC " + WRONG_BIC, responseEntity.getMessage());
    Assertions.assertEquals(ResponseConstant.SAD_ASPSP_NOT_FOUND, responseEntity.getCode());
    Assertions.assertEquals(ResponseCategory.ERROR, responseEntity.getCategory());
    Assertions.assertEquals(ResponseOrigin.STYX, responseEntity.getOrigin());
}
Also used : Response(javax.ws.rs.core.Response) ResponseEntity(net.petafuel.styx.api.exception.ResponseEntity) Invocation(javax.ws.rs.client.Invocation) ResponseCategory(net.petafuel.styx.api.exception.ResponseCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(net.petafuel.styx.api.IntegrationTest) Test(org.junit.Test) StyxRESTTest(net.petafuel.styx.api.StyxRESTTest)

Example 8 with ResponseEntity

use of net.petafuel.styx.api.exception.ResponseEntity in project styx by petafuel.

the class AccessTokenFilterUnitTest method testMasterTokenInvalidConfiguration.

@Test
void testMasterTokenInvalidConfiguration() {
    MasterToken masterToken = prepareMasterToken("pis", null);
    AccessTokenFilter accessTokenFilter = new AccessTokenFilter();
    Assertions.assertThrows(StyxException.class, () -> accessTokenFilter.checkRestrictions(masterToken, "pis"));
    try {
        accessTokenFilter.checkRestrictions(masterToken, "pis");
    } catch (StyxException exception) {
        ResponseEntity response = exception.getResponseEntity();
        Assertions.assertEquals(ResponseConstant.STYX_MASTER_TOKEN_RESTRICTED.getReasonPhrase(), response.getMessage());
        Assertions.assertEquals(ResponseConstant.STYX_MASTER_TOKEN_RESTRICTED.getStatusCode(), response.getCode().getStatusCode());
        Assertions.assertEquals(ResponseCategory.ERROR, response.getCategory());
        Assertions.assertEquals(ResponseOrigin.STYX, response.getOrigin());
    }
}
Also used : MasterToken(net.petafuel.styx.core.persistence.models.MasterToken) ResponseEntity(net.petafuel.styx.api.exception.ResponseEntity) StyxException(net.petafuel.styx.api.exception.StyxException) Test(org.junit.jupiter.api.Test)

Example 9 with ResponseEntity

use of net.petafuel.styx.api.exception.ResponseEntity in project styx by petafuel.

the class AccessTokenFilterUnitTest method testCheckMaxUsagesReached.

@Test
void testCheckMaxUsagesReached() {
    AccessToken accessToken = new AccessToken();
    accessToken.setServiceType("ais");
    MasterToken masterToken = prepareMasterToken("ais", 2);
    AccessTokenFilter accessTokenFilter = new AccessTokenFilter();
    accessToken.setUsages(2);
    Assertions.assertThrows(StyxException.class, () -> accessTokenFilter.checkMaxUsages(masterToken, accessToken));
    try {
        accessTokenFilter.checkMaxUsages(masterToken, accessToken);
    } catch (StyxException exception) {
        ResponseEntity response = exception.getResponseEntity();
        Assertions.assertEquals(ResponseConstant.STYX_TOKEN_ACCESS_EXEEDED.getReasonPhrase(), response.getMessage());
        Assertions.assertEquals(ResponseConstant.STYX_TOKEN_ACCESS_EXEEDED.getStatusCode(), response.getCode().getStatusCode());
        Assertions.assertEquals(ResponseCategory.ERROR, response.getCategory());
        Assertions.assertEquals(ResponseOrigin.CLIENT, response.getOrigin());
    }
}
Also used : MasterToken(net.petafuel.styx.core.persistence.models.MasterToken) ResponseEntity(net.petafuel.styx.api.exception.ResponseEntity) AccessToken(net.petafuel.styx.core.persistence.models.AccessToken) StyxException(net.petafuel.styx.api.exception.StyxException) Test(org.junit.jupiter.api.Test)

Example 10 with ResponseEntity

use of net.petafuel.styx.api.exception.ResponseEntity in project styx by petafuel.

the class AbstractTokenFilter method filter.

@Override
public void filter(ContainerRequestContext context) {
    String token = context.getHeaderString("token");
    if (token == null || "".equals(token)) {
        ResponseEntity responseEntity = new ResponseEntity(ResponseConstant.STYX_MISSING_CLIENT_TOKEN, ResponseCategory.ERROR, ResponseOrigin.CLIENT);
        throw new StyxException(responseEntity);
    }
    // token is hashed, constant length of 64 characters
    if (token.length() != 64) {
        ResponseEntity responseEntity = new ResponseEntity(ResponseConstant.STYX_INVALID_TOKEN_FORMAT, ResponseCategory.ERROR, ResponseOrigin.CLIENT);
        throw new StyxException(responseEntity);
    }
    String tokenHash;
    try {
        tokenHash = TokenGenerator.hashSHA256(token);
    } catch (NoSuchAlgorithmException e) {
        LOG.error("plainToken could not be hashed error={}", e.getMessage());
        ResponseEntity responseEntity = new ResponseEntity(ResponseConstant.STYX_INVALID_TOKEN_FORMAT, ResponseCategory.ERROR, ResponseOrigin.CLIENT);
        throw new StyxException(responseEntity);
    }
    boolean tokenValid = checkToken(tokenHash);
    if (!tokenValid) {
        ResponseEntity responseEntity = new ResponseEntity(ResponseConstant.STYX_TOKEN_EXPIRED_OR_REVOKED, ResponseCategory.ERROR, ResponseOrigin.CLIENT);
        throw new StyxException(responseEntity);
    }
    context.setProperty(AbstractTokenFilter.class.getName(), tokenHash);
}
Also used : ResponseEntity(net.petafuel.styx.api.exception.ResponseEntity) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) StyxException(net.petafuel.styx.api.exception.StyxException)

Aggregations

ResponseEntity (net.petafuel.styx.api.exception.ResponseEntity)19 StyxException (net.petafuel.styx.api.exception.StyxException)17 MasterToken (net.petafuel.styx.core.persistence.models.MasterToken)6 Test (org.junit.jupiter.api.Test)5 Path (javax.ws.rs.Path)4 POST (javax.ws.rs.POST)3 AcceptsPreStepAuth (net.petafuel.styx.api.filter.authentication.boundary.AcceptsPreStepAuth)3 AbstractTokenFilter (net.petafuel.styx.api.filter.authentication.control.AbstractTokenFilter)3 RequiresMandatoryHeader (net.petafuel.styx.api.filter.input.boundary.RequiresMandatoryHeader)3 AspspUrlMapper (net.petafuel.styx.api.util.AspspUrlMapper)3 PaymentResponse (net.petafuel.styx.api.v1.payment.entity.PaymentResponse)3 IOProcessor (net.petafuel.styx.core.ioprocessing.IOProcessor)3 PersistenceEmptyResultSetException (net.petafuel.styx.core.persistence.PersistenceEmptyResultSetException)3 AccessToken (net.petafuel.styx.core.persistence.models.AccessToken)3 PISRequest (net.petafuel.styx.core.xs2a.contracts.PISRequest)3 InitiatedPayment (net.petafuel.styx.core.xs2a.entities.InitiatedPayment)3 SinglePayment (net.petafuel.styx.core.xs2a.entities.SinglePayment)3 PISRequestFactory (net.petafuel.styx.core.xs2a.factory.PISRequestFactory)3 XS2AFactoryInput (net.petafuel.styx.core.xs2a.factory.XS2AFactoryInput)3 OAuth2 (net.petafuel.styx.core.xs2a.sca.OAuth2)3