use of io.getlime.security.powerauth.rest.api.spring.exception.PowerAuthAuthenticationException 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();
}
}
use of io.getlime.security.powerauth.rest.api.spring.exception.PowerAuthAuthenticationException in project powerauth-restful-integration by lime-company.
the class TokenService method removeToken.
/**
* Remove token.
*
* @param request Remove token request.
* @param authentication PowerAuth API authentication object.
* @return Remove token response.
* @throws PowerAuthAuthenticationException In case authentication fails.
*/
public TokenRemoveResponse removeToken(TokenRemoveRequest request, PowerAuthApiAuthentication authentication) throws PowerAuthAuthenticationException {
try {
// Fetch activation ID
final String activationId = authentication.getActivationContext().getActivationId();
// Fetch token ID from the request
final String tokenId = request.getTokenId();
// Remove a token, ignore response, since the endpoint should quietly return
powerAuthClient.removeToken(tokenId, activationId);
// Prepare a response
final TokenRemoveResponse response = new TokenRemoveResponse();
response.setTokenId(tokenId);
return response;
} catch (Exception ex) {
logger.warn("Removing PowerAuth token failed, error: {}", ex.getMessage());
logger.debug(ex.getMessage(), ex);
throw new PowerAuthTokenErrorException();
}
}
use of io.getlime.security.powerauth.rest.api.spring.exception.PowerAuthAuthenticationException 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();
}
}
Aggregations