use of io.getlime.security.powerauth.http.validator.InvalidPowerAuthHttpHeaderException in project powerauth-restful-integration by lime-company.
the class SecureVaultService method vaultUnlock.
/**
* Unlock secure vault.
* @param signatureHeader PowerAuth signature HTTP header.
* @param request Vault unlock request.
* @param httpServletRequest HTTP servlet request.
* @return Vault unlock response.
* @throws PowerAuthSecureVaultException In case vault unlock fails.
* @throws PowerAuthAuthenticationException In case authentication fails.
*/
public VaultUnlockResponse vaultUnlock(String signatureHeader, VaultUnlockRequest request, HttpServletRequest httpServletRequest) throws PowerAuthSecureVaultException, PowerAuthAuthenticationException {
try {
// Parse the header
final PowerAuthSignatureHttpHeader header = new PowerAuthSignatureHttpHeader().fromValue(signatureHeader);
// Validate the header
try {
PowerAuthSignatureHttpHeaderValidator.validate(header);
} catch (InvalidPowerAuthHttpHeaderException ex) {
logger.warn("Signature HTTP header validation failed, error: {}", ex.getMessage());
logger.debug(ex.getMessage(), ex);
throw new PowerAuthSignatureTypeInvalidException();
}
final SignatureTypeConverter converter = new SignatureTypeConverter();
final String activationId = header.getActivationId();
final String applicationId = header.getApplicationKey();
final String signature = header.getSignature();
final SignatureType signatureType = converter.convertFrom(header.getSignatureType());
if (signatureType == null) {
logger.warn("Invalid signature type: {}", header.getSignatureType());
throw new PowerAuthSignatureTypeInvalidException();
}
final String nonce = header.getNonce();
String reason = null;
byte[] requestBodyBytes;
if ("2.0".equals(header.getVersion())) {
// Version 2.0 requires null data in signature for vault unlock.
requestBodyBytes = null;
} else if ("2.1".equals(header.getVersion())) {
// Version 2.1 or higher requires request data in signature (POST request body) for vault unlock.
if (request != null) {
// Send vault unlock reason, in case it is available.
if (request.getReason() != null) {
reason = request.getReason();
}
}
// Use POST request body as data for signature.
requestBodyBytes = authenticationProvider.extractRequestBodyBytes(httpServletRequest);
} else {
logger.warn("Invalid protocol version in secure vault: {}", header.getVersion());
throw new PowerAuthSecureVaultException();
}
final String data = PowerAuthHttpBody.getSignatureBaseString("POST", "/pa/vault/unlock", BaseEncoding.base64().decode(nonce), requestBodyBytes);
final com.wultra.security.powerauth.client.v2.VaultUnlockResponse paResponse = powerAuthClient.v2().unlockVault(activationId, applicationId, data, signature, signatureType, reason);
if (!paResponse.isSignatureValid()) {
logger.debug("Signature validation failed");
throw new PowerAuthSignatureInvalidException();
}
final VaultUnlockResponse response = new VaultUnlockResponse();
response.setActivationId(paResponse.getActivationId());
response.setEncryptedVaultEncryptionKey(paResponse.getEncryptedVaultEncryptionKey());
return response;
} catch (PowerAuthAuthenticationException ex) {
throw ex;
} catch (Exception ex) {
logger.warn("PowerAuth vault unlock failed, error: {}", ex.getMessage());
logger.debug(ex.getMessage(), ex);
throw new PowerAuthSecureVaultException();
}
}
use of io.getlime.security.powerauth.http.validator.InvalidPowerAuthHttpHeaderException in project powerauth-restful-integration by lime-company.
the class SecureVaultController method unlockVault.
/**
* Request the vault unlock key.
* @param signatureHeader PowerAuth signature HTTP header.
* @param request Vault unlock request data.
* @param httpServletRequest HTTP servlet request.
* @return PowerAuth RESTful response with {@link VaultUnlockResponse} payload.
* @throws PowerAuthAuthenticationException In case authentication fails.
* @throws PowerAuthSecureVaultException In case unlocking the vault fails.
*/
@RequestMapping(value = "unlock", method = RequestMethod.POST)
public ObjectResponse<VaultUnlockResponse> unlockVault(@RequestHeader(value = PowerAuthSignatureHttpHeader.HEADER_NAME, defaultValue = "unknown") String signatureHeader, @RequestBody(required = false) ObjectRequest<VaultUnlockRequest> request, HttpServletRequest httpServletRequest) throws PowerAuthAuthenticationException, PowerAuthSecureVaultException {
// Request object is not validated - it is optional for version 2
// Parse the header
PowerAuthSignatureHttpHeader header = new PowerAuthSignatureHttpHeader().fromValue(signatureHeader);
// Validate the header
try {
PowerAuthSignatureHttpHeaderValidator.validate(header);
} catch (InvalidPowerAuthHttpHeaderException ex) {
logger.warn("Signature HTTP header validation failed, error: {}", ex.getMessage());
logger.debug(ex.getMessage(), ex);
throw new PowerAuthSignatureInvalidException();
}
if (!"2.0".equals(header.getVersion()) && !"2.1".equals(header.getVersion())) {
logger.warn("Endpoint does not support PowerAuth protocol version {}", header.getVersion());
throw new PowerAuthInvalidRequestException();
}
VaultUnlockResponse response = secureVaultServiceV2.vaultUnlock(signatureHeader, request.getRequestObject(), httpServletRequest);
return new ObjectResponse<>(response);
}
use of io.getlime.security.powerauth.http.validator.InvalidPowerAuthHttpHeaderException in project powerauth-restful-integration by lime-company.
the class SecureVaultController method unlockVault.
/**
* Request the vault unlock key.
* @param signatureHeader PowerAuth signature HTTP header.
* @return PowerAuth RESTful response with {@link VaultUnlockResponse} payload.
* @throws PowerAuthAuthenticationException In case authentication fails.
*/
@RequestMapping(value = "unlock", method = RequestMethod.POST)
@ResponseBody
public ObjectResponse<VaultUnlockResponse> unlockVault(@RequestHeader(value = PowerAuthSignatureHttpHeader.HEADER_NAME, defaultValue = "unknown") String signatureHeader, @RequestBody(required = false) ObjectRequest<VaultUnlockRequest> request, HttpServletRequest httpServletRequest) throws PowerAuthAuthenticationException, PowerAuthSecureVaultException {
try {
// Parse the header
PowerAuthSignatureHttpHeader header = new PowerAuthSignatureHttpHeader().fromValue(signatureHeader);
// Validate the header
try {
PowerAuthSignatureHttpHeaderValidator.validate(header);
} catch (InvalidPowerAuthHttpHeaderException e) {
throw new PowerAuthAuthenticationException(e.getMessage());
}
SignatureTypeConverter converter = new SignatureTypeConverter();
String activationId = header.getActivationId();
String applicationId = header.getApplicationKey();
String signature = header.getSignature();
SignatureType signatureType = converter.convertFrom(header.getSignatureType());
String nonce = header.getNonce();
String reason = null;
byte[] requestBodyBytes;
if ("2.0".equals(header.getVersion())) {
// Version 2.0 requires null data in signature for vault unlock.
requestBodyBytes = null;
} else {
// Version 2.1 or higher requires request data in signature (POST request body) for vault unlock.
if (request != null) {
// Send vault unlock reason, in case it is available.
VaultUnlockRequest vaultUnlockRequest = request.getRequestObject();
if (vaultUnlockRequest != null && vaultUnlockRequest.getReason() != null) {
reason = vaultUnlockRequest.getReason();
}
}
// Use POST request body as data for signature.
String requestBodyString = ((String) httpServletRequest.getAttribute(PowerAuthRequestFilterBase.POWERAUTH_SIGNATURE_BASE_STRING));
requestBodyBytes = requestBodyString == null ? null : BaseEncoding.base64().decode(requestBodyString);
}
String data = PowerAuthHttpBody.getSignatureBaseString("POST", "/pa/vault/unlock", BaseEncoding.base64().decode(nonce), requestBodyBytes);
io.getlime.powerauth.soap.VaultUnlockResponse soapResponse = powerAuthClient.unlockVault(activationId, applicationId, data, signature, signatureType, reason);
if (!soapResponse.isSignatureValid()) {
throw new PowerAuthAuthenticationException();
}
VaultUnlockResponse response = new VaultUnlockResponse();
response.setActivationId(soapResponse.getActivationId());
response.setEncryptedVaultEncryptionKey(soapResponse.getEncryptedVaultEncryptionKey());
return new ObjectResponse<>(response);
} catch (Exception ex) {
if (PowerAuthAuthenticationException.class.equals(ex.getClass())) {
throw ex;
} else {
throw new PowerAuthSecureVaultException();
}
}
}
use of io.getlime.security.powerauth.http.validator.InvalidPowerAuthHttpHeaderException in project powerauth-restful-integration by lime-company.
the class PowerAuthAuthenticationProvider method validateToken.
public PowerAuthApiAuthentication validateToken(String tokenHeader, List<PowerAuthSignatureTypes> allowedSignatureTypes) throws PowerAuthAuthenticationException {
// Check for HTTP PowerAuth signature header
if (tokenHeader == null || tokenHeader.equals("undefined")) {
throw new PowerAuthAuthenticationException("POWER_AUTH_TOKEN_INVALID_EMPTY");
}
// Parse HTTP header
PowerAuthTokenHttpHeader header = new PowerAuthTokenHttpHeader().fromValue(tokenHeader);
// Validate the header
try {
PowerAuthTokenHttpHeaderValidator.validate(header);
} catch (InvalidPowerAuthHttpHeaderException e) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, e.getMessage(), e);
throw new PowerAuthAuthenticationException(e.getMessage());
}
// Prepare authentication object
PowerAuthTokenAuthenticationImpl powerAuthTokenAuthentication = new PowerAuthTokenAuthenticationImpl();
powerAuthTokenAuthentication.setTokenId(header.getTokenId());
powerAuthTokenAuthentication.setTokenDigest(header.getTokenDigest());
powerAuthTokenAuthentication.setNonce(header.getNonce());
powerAuthTokenAuthentication.setTimestamp(header.getTimestamp());
// Call the authentication based on token authentication object
final PowerAuthApiAuthentication auth = (PowerAuthApiAuthentication) this.authenticate(powerAuthTokenAuthentication);
// In case authentication is null, throw PowerAuth exception
if (auth == null) {
throw new PowerAuthAuthenticationException("POWER_AUTH_TOKEN_INVALID_VALUE");
}
// Check if the signature type is allowed
PowerAuthSignatureTypes expectedSignatureType = auth.getSignatureFactors();
if (!allowedSignatureTypes.contains(expectedSignatureType)) {
throw new PowerAuthAuthenticationException("POWER_AUTH_TOKEN_SIGNATURE_TYPE_INVALID");
}
return auth;
}
use of io.getlime.security.powerauth.http.validator.InvalidPowerAuthHttpHeaderException in project powerauth-restful-integration by lime-company.
the class PowerAuthAuthenticationProvider method validateRequestSignature.
/**
* Validate the signature from the PowerAuth 2.0 HTTP header against the provided HTTP method, request body and URI identifier.
* Make sure to accept only allowed signatures.
* @param httpMethod HTTP method (GET, POST, ...)
* @param httpBody Body of the HTTP request.
* @param requestUriIdentifier Request URI identifier.
* @param httpAuthorizationHeader PowerAuth 2.0 HTTP authorization header.
* @param allowedSignatureTypes Allowed types of the signature.
* @return Instance of a PowerAuthApiAuthenticationImpl on successful authorization.
* @throws PowerAuthAuthenticationException In case authorization fails, exception is raised.
*/
public PowerAuthApiAuthentication validateRequestSignature(String httpMethod, byte[] httpBody, String requestUriIdentifier, String httpAuthorizationHeader, List<PowerAuthSignatureTypes> allowedSignatureTypes) throws PowerAuthAuthenticationException {
// Check for HTTP PowerAuth signature header
if (httpAuthorizationHeader == null || httpAuthorizationHeader.equals("undefined")) {
throw new PowerAuthAuthenticationException("POWER_AUTH_SIGNATURE_INVALID_EMPTY");
}
// Parse HTTP header
PowerAuthSignatureHttpHeader header = new PowerAuthSignatureHttpHeader().fromValue(httpAuthorizationHeader);
// Validate the header
try {
PowerAuthSignatureHttpHeaderValidator.validate(header);
} catch (InvalidPowerAuthHttpHeaderException e) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, e.getMessage(), e);
throw new PowerAuthAuthenticationException(e.getMessage());
}
// Check if the application is allowed, "true" is the default behavior
if (applicationConfiguration != null) {
boolean isApplicationAllowed = applicationConfiguration.isAllowedApplicationKey(header.getApplicationKey());
if (!isApplicationAllowed) {
throw new PowerAuthAuthenticationException("POWER_AUTH_SIGNATURE_INVALID_APPLICATION_ID");
}
}
// Check if the signature type is allowed
PowerAuthSignatureTypes expectedSignatureType = PowerAuthSignatureTypes.getEnumFromString(header.getSignatureType());
if (!allowedSignatureTypes.contains(expectedSignatureType)) {
throw new PowerAuthAuthenticationException("POWER_AUTH_SIGNATURE_TYPE_INVALID");
}
// Configure PowerAuth authentication object
PowerAuthSignatureAuthenticationImpl powerAuthAuthentication = new PowerAuthSignatureAuthenticationImpl();
powerAuthAuthentication.setActivationId(header.getActivationId());
powerAuthAuthentication.setApplicationKey(header.getApplicationKey());
powerAuthAuthentication.setNonce(BaseEncoding.base64().decode(header.getNonce()));
powerAuthAuthentication.setSignatureType(header.getSignatureType());
powerAuthAuthentication.setSignature(header.getSignature());
powerAuthAuthentication.setHttpMethod(httpMethod);
powerAuthAuthentication.setRequestUri(requestUriIdentifier);
powerAuthAuthentication.setData(httpBody);
// Call the authentication based on signature authentication object
PowerAuthApiAuthentication auth = (PowerAuthApiAuthentication) this.authenticate(powerAuthAuthentication);
// In case authentication is null, throw PowerAuth exception
if (auth == null) {
throw new PowerAuthAuthenticationException("POWER_AUTH_SIGNATURE_INVALID_VALUE");
}
return auth;
}
Aggregations