Search in sources :

Example 6 with EciesEncryptedResponse

use of io.getlime.security.powerauth.rest.api.model.response.v3.EciesEncryptedResponse in project powerauth-restful-integration by lime-company.

the class EncryptionResponseBodyAdvice method beforeBodyWrite.

/**
 * Encrypt response before writing body.
 *
 * @param response Response object.
 * @param methodParameter Method parameter.
 * @param mediaType Selected HTTP response media type.
 * @param converterClass Selected HTTP message converter class.
 * @param serverHttpRequest HTTP request.
 * @param serverHttpResponse HTTP response.
 * @return ECIES cryptogram.
 */
@Override
public Object beforeBodyWrite(Object response, @NonNull MethodParameter methodParameter, @NonNull MediaType mediaType, @NonNull Class<? extends HttpMessageConverter<?>> converterClass, @NonNull ServerHttpRequest serverHttpRequest, @NonNull ServerHttpResponse serverHttpResponse) {
    if (response == null) {
        return null;
    }
    // Extract ECIES encryption object from HTTP request
    final HttpServletRequest httpServletRequest = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest();
    final PowerAuthEciesEncryption eciesEncryption = (PowerAuthEciesEncryption) httpServletRequest.getAttribute(PowerAuthRequestObjects.ENCRYPTION_OBJECT);
    if (eciesEncryption == null) {
        return null;
    }
    // Convert response to JSON
    try {
        byte[] responseBytes = serializeResponseObject(response);
        // Encrypt response using decryptor and return ECIES cryptogram
        final EciesDecryptor eciesDecryptor = eciesEncryption.getEciesDecryptor();
        final EciesCryptogram cryptogram = eciesDecryptor.encryptResponse(responseBytes);
        final String encryptedDataBase64 = BaseEncoding.base64().encode(cryptogram.getEncryptedData());
        final String macBase64 = BaseEncoding.base64().encode(cryptogram.getMac());
        // Return encrypted response with type given by converter class
        final EciesEncryptedResponse encryptedResponse = new EciesEncryptedResponse(encryptedDataBase64, macBase64);
        if (converterClass.isAssignableFrom(MappingJackson2HttpMessageConverter.class)) {
            // Object conversion is done automatically using MappingJackson2HttpMessageConverter
            return encryptedResponse;
        } else if (converterClass.isAssignableFrom(StringHttpMessageConverter.class)) {
            // Conversion to byte[] is done using first applicable configured HTTP message converter, corresponding String is returned
            return new String(convertEncryptedResponse(encryptedResponse, mediaType), StandardCharsets.UTF_8);
        } else {
            // Conversion to byte[] is done using first applicable configured HTTP message converter
            return convertEncryptedResponse(encryptedResponse, mediaType);
        }
    } catch (Exception ex) {
        logger.warn("Encryption failed, error: {}", ex.getMessage());
        logger.debug("Error details", ex);
        return null;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EciesCryptogram(io.getlime.security.powerauth.crypto.lib.encryptor.ecies.model.EciesCryptogram) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) PowerAuthEciesEncryption(io.getlime.security.powerauth.rest.api.spring.encryption.PowerAuthEciesEncryption) EciesEncryptedResponse(io.getlime.security.powerauth.rest.api.model.response.v3.EciesEncryptedResponse) EciesDecryptor(io.getlime.security.powerauth.crypto.lib.encryptor.ecies.EciesDecryptor) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) IOException(java.io.IOException)

Example 7 with EciesEncryptedResponse

use of io.getlime.security.powerauth.rest.api.model.response.v3.EciesEncryptedResponse in project powerauth-restful-integration by lime-company.

the class RecoveryService method confirmRecoveryCode.

/**
 * Confirm recovery code.
 * @param request ECIES encrypted request.
 * @param authentication PowerAuth API authentication object.
 * @return ECIES encrypted response.
 * @throws PowerAuthAuthenticationException In case confirm recovery fails.
 */
public EciesEncryptedResponse confirmRecoveryCode(EciesEncryptedRequest request, PowerAuthApiAuthentication authentication) throws PowerAuthAuthenticationException {
    try {
        final String activationId = authentication.getActivationContext().getActivationId();
        final PowerAuthSignatureHttpHeader httpHeader = (PowerAuthSignatureHttpHeader) authentication.getHttpHeader();
        final String applicationKey = httpHeader.getApplicationKey();
        if (activationId == null || applicationKey == null || request.getEphemeralPublicKey() == null || request.getEncryptedData() == null || request.getMac() == null) {
            logger.warn("PowerAuth confirm recovery failed because of invalid request");
            throw new PowerAuthInvalidRequestException();
        }
        final ConfirmRecoveryCodeResponse paResponse = powerAuthClient.confirmRecoveryCode(activationId, applicationKey, request.getEphemeralPublicKey(), request.getEncryptedData(), request.getMac(), request.getNonce());
        if (!paResponse.getActivationId().equals(activationId)) {
            logger.warn("PowerAuth confirm recovery failed because of invalid activation ID in response");
            throw new PowerAuthInvalidRequestException();
        }
        return new EciesEncryptedResponse(paResponse.getEncryptedData(), paResponse.getMac());
    } catch (Exception ex) {
        logger.warn("PowerAuth confirm recovery failed, error: {}", ex.getMessage());
        logger.debug(ex.getMessage(), ex);
        throw new PowerAuthRecoveryConfirmationException();
    }
}
Also used : PowerAuthInvalidRequestException(io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthInvalidRequestException) ConfirmRecoveryCodeResponse(com.wultra.security.powerauth.client.v3.ConfirmRecoveryCodeResponse) PowerAuthRecoveryConfirmationException(io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthRecoveryConfirmationException) EciesEncryptedResponse(io.getlime.security.powerauth.rest.api.model.response.v3.EciesEncryptedResponse) PowerAuthSignatureHttpHeader(io.getlime.security.powerauth.http.PowerAuthSignatureHttpHeader) PowerAuthAuthenticationException(io.getlime.security.powerauth.rest.api.spring.exception.PowerAuthAuthenticationException) PowerAuthInvalidRequestException(io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthInvalidRequestException) PowerAuthRecoveryConfirmationException(io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthRecoveryConfirmationException)

Example 8 with EciesEncryptedResponse

use of io.getlime.security.powerauth.rest.api.model.response.v3.EciesEncryptedResponse in project powerauth-restful-integration by lime-company.

the class TokenService method createToken.

/**
 * Create token.
 *
 * @param request        ECIES encrypted create token request.
 * @param authentication PowerAuth API authentication object.
 * @return ECIES encrypted create token response.
 * @throws PowerAuthAuthenticationException In case token could not be created.
 */
public EciesEncryptedResponse createToken(EciesEncryptedRequest request, PowerAuthApiAuthentication authentication) throws PowerAuthAuthenticationException {
    try {
        // Fetch activation ID and signature type
        final PowerAuthSignatureTypes signatureFactors = authentication.getAuthenticationContext().getSignatureType();
        // Fetch data from the request
        final String ephemeralPublicKey = request.getEphemeralPublicKey();
        final String encryptedData = request.getEncryptedData();
        final String mac = request.getMac();
        final String nonce = request.getNonce();
        // Prepare a signature type converter
        final SignatureTypeConverter converter = new SignatureTypeConverter();
        final SignatureType signatureType = converter.convertFrom(signatureFactors);
        if (signatureType == null) {
            logger.warn("Invalid signature type: {}", signatureFactors);
            throw new PowerAuthSignatureTypeInvalidException();
        }
        // Get ECIES headers
        final String activationId = authentication.getActivationContext().getActivationId();
        final PowerAuthSignatureHttpHeader httpHeader = (PowerAuthSignatureHttpHeader) authentication.getHttpHeader();
        final String applicationKey = httpHeader.getApplicationKey();
        // Create a token
        final CreateTokenResponse token = powerAuthClient.createToken(activationId, applicationKey, ephemeralPublicKey, encryptedData, mac, nonce, signatureType);
        // Prepare a response
        final EciesEncryptedResponse response = new EciesEncryptedResponse();
        response.setMac(token.getMac());
        response.setEncryptedData(token.getEncryptedData());
        return response;
    } catch (Exception ex) {
        logger.warn("Creating PowerAuth token failed, error: {}", ex.getMessage());
        logger.debug(ex.getMessage(), ex);
        throw new PowerAuthTokenErrorException();
    }
}
Also used : PowerAuthTokenErrorException(io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthTokenErrorException) PowerAuthSignatureTypeInvalidException(io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthSignatureTypeInvalidException) EciesEncryptedResponse(io.getlime.security.powerauth.rest.api.model.response.v3.EciesEncryptedResponse) SignatureType(com.wultra.security.powerauth.client.v3.SignatureType) PowerAuthSignatureHttpHeader(io.getlime.security.powerauth.http.PowerAuthSignatureHttpHeader) CreateTokenResponse(com.wultra.security.powerauth.client.v3.CreateTokenResponse) PowerAuthSignatureTypes(io.getlime.security.powerauth.crypto.lib.enums.PowerAuthSignatureTypes) PowerAuthTokenErrorException(io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthTokenErrorException) PowerAuthAuthenticationException(io.getlime.security.powerauth.rest.api.spring.exception.PowerAuthAuthenticationException) PowerAuthSignatureTypeInvalidException(io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthSignatureTypeInvalidException) SignatureTypeConverter(io.getlime.security.powerauth.rest.api.spring.converter.v3.SignatureTypeConverter)

Aggregations

EciesEncryptedResponse (io.getlime.security.powerauth.rest.api.model.response.v3.EciesEncryptedResponse)8 PowerAuthAuthenticationException (io.getlime.security.powerauth.rest.api.spring.exception.PowerAuthAuthenticationException)4 PowerAuthInvalidRequestException (io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthInvalidRequestException)3 SignatureType (com.wultra.security.powerauth.client.v3.SignatureType)2 EciesCryptogram (io.getlime.security.powerauth.crypto.lib.encryptor.ecies.model.EciesCryptogram)2 PowerAuthSignatureHttpHeader (io.getlime.security.powerauth.http.PowerAuthSignatureHttpHeader)2 ActivationLayer1Response (io.getlime.security.powerauth.rest.api.model.response.v3.ActivationLayer1Response)2 SignatureTypeConverter (io.getlime.security.powerauth.rest.api.spring.converter.v3.SignatureTypeConverter)2 PowerAuthSignatureInvalidException (io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthSignatureInvalidException)2 PowerAuthSignatureTypeInvalidException (io.getlime.security.powerauth.rest.api.spring.exception.authentication.PowerAuthSignatureTypeInvalidException)2 IOException (java.io.IOException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 PowerAuthClientException (com.wultra.security.powerauth.client.model.error.PowerAuthClientException)1 PowerAuthErrorRecovery (com.wultra.security.powerauth.client.model.error.PowerAuthErrorRecovery)1 ConfirmRecoveryCodeResponse (com.wultra.security.powerauth.client.v3.ConfirmRecoveryCodeResponse)1 CreateTokenResponse (com.wultra.security.powerauth.client.v3.CreateTokenResponse)1 StartUpgradeResponse (com.wultra.security.powerauth.client.v3.StartUpgradeResponse)1 VaultUnlockResponse (com.wultra.security.powerauth.client.v3.VaultUnlockResponse)1 EciesDecryptor (io.getlime.security.powerauth.crypto.lib.encryptor.ecies.EciesDecryptor)1 PowerAuthSignatureTypes (io.getlime.security.powerauth.crypto.lib.enums.PowerAuthSignatureTypes)1