Search in sources :

Example 6 with StyxException

use of net.petafuel.styx.api.exception.StyxException 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 StyxException

use of net.petafuel.styx.api.exception.StyxException 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 8 with StyxException

use of net.petafuel.styx.api.exception.StyxException 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 9 with StyxException

use of net.petafuel.styx.api.exception.StyxException 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)

Example 10 with StyxException

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

the class SADInitialisationFilter method filter.

@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    if (containerRequestContext.getProperty(BICFilter.class.getName()) == null) {
        LOG.info("XS2AStandard was not initialized as there was no BICFilter in place for the requested Resource");
        return;
    }
    String bic = (String) containerRequestContext.getProperty(BICFilter.class.getName());
    XS2AStandard xs2AStandard;
    try {
        xs2AStandard = new SAD().getBankByBIC(bic, WebServer.isSandbox());
        if (Boolean.FALSE.equals(xs2AStandard.getAspsp().isActive())) {
            throw new StyxException(new ResponseEntity("ASPSP with bic=" + xs2AStandard.getAspsp().getBic() + " is inactive", ResponseConstant.SAD_ASPSP_INACTIVE, ResponseCategory.ERROR, ResponseOrigin.STYX));
        }
        LOG.info("XS2AStandard successfully initialized. bic={}, aspspName={}, aspspId={}, aspspGroup={}, aspspGroupId={}, standard={}, standardVersion={}, ais={}, cs={}, pis={}, piis={}, availableOptions={}", xs2AStandard.getAspsp().getBic(), xs2AStandard.getAspsp().getName(), xs2AStandard.getAspsp().getId(), xs2AStandard.getAspsp().getAspspGroup().getName(), xs2AStandard.getAspsp().getAspspGroup().getId(), xs2AStandard.getAspsp().getConfig().getStandard().getName(), xs2AStandard.getAspsp().getConfig().getStandard().getVersion(), xs2AStandard.getAis(), xs2AStandard.getCs(), xs2AStandard.getPis(), xs2AStandard.getPiis(), xs2AStandard.getAspsp().getConfig().getImplementerOptions() != null ? xs2AStandard.getAspsp().getConfig().getImplementerOptions().size() : 0);
    } catch (BankNotFoundException bicNotFound) {
        throw new StyxException(new ResponseEntity(bicNotFound.getMessage(), ResponseConstant.SAD_ASPSP_NOT_FOUND, ResponseCategory.ERROR, ResponseOrigin.STYX));
    } catch (BankLookupFailedException internalSADError) {
        throw new StyxException(new ResponseEntity("SAD was unable to initialize required Services", ResponseConstant.INTERNAL_SERVER_ERROR, ResponseCategory.ERROR, ResponseOrigin.STYX), internalSADError);
    }
    containerRequestContext.setProperty(XS2AStandard.class.getName(), xs2AStandard);
}
Also used : XS2AStandard(net.petafuel.styx.core.banklookup.XS2AStandard) ResponseEntity(net.petafuel.styx.api.exception.ResponseEntity) SAD(net.petafuel.styx.core.banklookup.sad.SAD) BankLookupFailedException(net.petafuel.styx.core.banklookup.exceptions.BankLookupFailedException) StyxException(net.petafuel.styx.api.exception.StyxException) BankNotFoundException(net.petafuel.styx.core.banklookup.exceptions.BankNotFoundException)

Aggregations

ResponseEntity (net.petafuel.styx.api.exception.ResponseEntity)17 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