Search in sources :

Example 1 with OAuthSession

use of net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession in project styx by petafuel.

the class OAuthService method buildLink.

public static String buildLink(String state, String bic) {
    OAuthSession stored = PersistentOAuthSession.getByState(state);
    HashMap<String, String> queryParams = getQueryParameters(stored);
    queryParams.put("bic", bic);
    Properties properties = Config.getInstance().getProperties();
    queryParams.put("client_id", properties.getProperty("keystore.client_id"));
    queryParams.put("redirect_uri", CallbackProvider.generateCallbackUrl(ServiceRealm.OAUTH, RealmParameter.PREAUTH, stored.getState()));
    return stored.getAuthorizationEndpoint() + BasicService.httpBuildQuery(queryParams);
}
Also used : PersistentOAuthSession(net.petafuel.styx.core.persistence.layers.PersistentOAuthSession) OAuthSession(net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession) Properties(java.util.Properties)

Example 2 with OAuthSession

use of net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession in project styx by petafuel.

the class OAuthService method startSession.

public static OAuthSession startSession(StrongAuthenticatableResource strongAuthenticatableResource, String scope) {
    OAuthService service = new OAuthService();
    Map<String, String> endpoints = service.getEndpoints(strongAuthenticatableResource.getLinks().getScaOAuth().getUrl());
    OAuthSession session = OAuthSession.start(strongAuthenticatableResource.getxRequestId());
    session.setScope(scope);
    session.setAuthorizationEndpoint(endpoints.get("authorization_endpoint"));
    session.setTokenEndpoint(endpoints.get("token_endpoint"));
    return PersistentOAuthSession.create(session);
}
Also used : PersistentOAuthSession(net.petafuel.styx.core.persistence.layers.PersistentOAuthSession) OAuthSession(net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession)

Example 3 with OAuthSession

use of net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession in project styx by petafuel.

the class OAuthService method buildLink.

public static String buildLink(String state, UUID xRequestId, ServiceRealm serviceRealm) {
    OAuthSession stored = PersistentOAuthSession.getByState(state);
    HashMap<String, String> queryParams = getQueryParameters(stored);
    Properties properties = Config.getInstance().getProperties();
    queryParams.put("client_id", properties.getProperty("keystore.client_id"));
    queryParams.put("redirect_uri", CallbackProvider.generateCallbackUrl(serviceRealm, RealmParameter.OK, xRequestId.toString()));
    return stored.getAuthorizationEndpoint() + BasicService.httpBuildQuery(queryParams);
}
Also used : PersistentOAuthSession(net.petafuel.styx.core.persistence.layers.PersistentOAuthSession) OAuthSession(net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession) Properties(java.util.Properties)

Example 4 with OAuthSession

use of net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession in project styx by petafuel.

the class OAuthService method startPreAuthSession.

public static OAuthSession startPreAuthSession(Url url, String scope) {
    // The X-Request-Id is not relevant for the prestep, since the OAuthSession will not directly relate to a consent or payment
    OAuthSession session = OAuthSession.start(null);
    session.setScope(scope);
    session.setAuthorizationEndpoint(url.getPreauthAuthorizationEndpoint());
    session.setTokenEndpoint(url.getPreauthTokenEndpoint());
    session.setxRequestId(session.getId());
    return PersistentOAuthSession.create(session);
}
Also used : PersistentOAuthSession(net.petafuel.styx.core.persistence.layers.PersistentOAuthSession) OAuthSession(net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession)

Example 5 with OAuthSession

use of net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession in project styx by petafuel.

the class PreAuthAccessFilter method filter.

/**
 * Supressing java:S3776 -> need to rework logic to reduce code complexity
 *
 * @param containerRequestContext
 */
@Override
@SuppressWarnings("java:S3776")
public void filter(ContainerRequestContext containerRequestContext) {
    XS2AStandard xs2AStandard = (XS2AStandard) containerRequestContext.getProperty(XS2AStandard.class.getName());
    IOParser ioParser = new IOParser(xs2AStandard.getAspsp());
    ImplementerOption ioPreAuthRequired = ioParser.get("IO6");
    if (ioPreAuthRequired != null && ioPreAuthRequired.getOptions().get(IOParser.Option.REQUIRED)) {
        LOG.info("ASPSP bic={} requires pre-auth", xs2AStandard.getAspsp().getBic());
        // preauth is available and required for this bank -> check if preauth id is present
        String preAuthIdString = containerRequestContext.getHeaderString(PRE_AUTH_ID);
        if (preAuthIdString == null || "".equals(preAuthIdString)) {
            throw new StyxException(new ResponseEntity("The requested aspsps requires a pre-step authorisation, preAuthId Header is missing", ResponseConstant.STYX_PREAUTH_HEADER_REQUIRED, ResponseCategory.ERROR, ResponseOrigin.CLIENT));
        }
        try {
            UUID preAuthId = UUID.fromString(preAuthIdString);
            OAuthSession oAuthSession = PersistentOAuthSession.getById(preAuthId);
            LOG.info("Loaded state={} oauth_session", oAuthSession.getState());
            STYX03.setPreauthId(preAuthId);
            if (oAuthSession.getAccessToken() == null || oAuthSession.getAccessTokenExpiresAt() == null) {
                throw new PersistenceEmptyResultSetException("The access_token data should be set");
            }
            if (oAuthSession.getAccessTokenExpiresAt().before(new Date())) {
                if (oAuthSession.getRefreshTokenExpiresAt().after(new Date())) {
                    oAuthSession = refreshToken(oAuthSession);
                } else {
                    throw new OAuthTokenExpiredException(OAuthTokenExpiredException.MESSAGE);
                }
            }
            // Add the Authorization: <type> <credentials> header to the request context so we can use it later on demand
            Map<String, String> additionalHeaders = new HashMap<>();
            additionalHeaders.put(XS2AHeader.AUTHORIZATION, oAuthSession.getTokenType() + " " + oAuthSession.getAccessToken());
            containerRequestContext.setProperty(PreAuthAccessFilter.class.getName(), additionalHeaders);
            LOG.info("Successfully attached pre-auth from oAuthSessionState={}", oAuthSession.getState());
        } catch (PersistenceEmptyResultSetException noOauthSessionFound) {
            throw new StyxException(new ResponseEntity("There was no valid pre-step authorisation found for the specified preAuthId", ResponseConstant.STYX_PREAUTH_NOT_AVAILABLE, ResponseCategory.ERROR, ResponseOrigin.CLIENT));
        } catch (OAuthTokenExpiredException tokenExpired) {
            throw new StyxException(new ResponseEntity(tokenExpired.getMessage(), ResponseConstant.STYX_PREAUTH_EXPIRED, ResponseCategory.ERROR, ResponseOrigin.CLIENT));
        }
    }
}
Also used : XS2AStandard(net.petafuel.styx.core.banklookup.XS2AStandard) OAuthTokenExpiredException(net.petafuel.styx.core.xs2a.exceptions.OAuthTokenExpiredException) HashMap(java.util.HashMap) PersistentOAuthSession(net.petafuel.styx.core.persistence.layers.PersistentOAuthSession) OAuthSession(net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession) PersistenceEmptyResultSetException(net.petafuel.styx.core.persistence.PersistenceEmptyResultSetException) ImplementerOption(net.petafuel.styx.core.banklookup.sad.entities.ImplementerOption) StyxException(net.petafuel.styx.api.exception.StyxException) Date(java.util.Date) ResponseEntity(net.petafuel.styx.api.exception.ResponseEntity) IOParser(net.petafuel.styx.core.ioprocessing.IOParser) UUID(java.util.UUID)

Aggregations

OAuthSession (net.petafuel.styx.core.xs2a.oauth.entities.OAuthSession)17 PersistentOAuthSession (net.petafuel.styx.core.persistence.layers.PersistentOAuthSession)15 PersistenceEmptyResultSetException (net.petafuel.styx.core.persistence.PersistenceEmptyResultSetException)4 Test (org.junit.jupiter.api.Test)4 Links (net.petafuel.styx.core.xs2a.entities.Links)3 Order (org.junit.jupiter.api.Order)3 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)3 Date (java.util.Date)2 Properties (java.util.Properties)2 ApplicationPath (javax.ws.rs.ApplicationPath)2 Path (javax.ws.rs.Path)2 ResponseEntity (net.petafuel.styx.api.exception.ResponseEntity)2 StyxException (net.petafuel.styx.api.exception.StyxException)2 CheckAccessToken (net.petafuel.styx.api.filter.authentication.boundary.CheckAccessToken)2 StrongAuthenticatableResource (net.petafuel.styx.core.xs2a.entities.StrongAuthenticatableResource)2 OAuthService (net.petafuel.styx.core.xs2a.oauth.OAuthService)2 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1