use of io.getlime.security.powerauth.rest.api.base.exception.PowerAuthAuthenticationException in project powerauth-restful-integration by lime-company.
the class TokenController method removeToken.
@RequestMapping(value = "remove", method = RequestMethod.POST)
@PowerAuth(resourceId = "/pa/token/remove", signatureType = { PowerAuthSignatureTypes.POSSESSION, PowerAuthSignatureTypes.POSSESSION_KNOWLEDGE, PowerAuthSignatureTypes.POSSESSION_BIOMETRY, PowerAuthSignatureTypes.POSSESSION_KNOWLEDGE_BIOMETRY })
@ResponseBody
public ObjectResponse<TokenRemoveResponse> removeToken(@RequestBody ObjectRequest<TokenRemoveRequest> request, PowerAuthApiAuthentication authentication) throws PowerAuthAuthenticationException {
try {
if (authentication != null && authentication.getActivationId() != null) {
// Fetch activation ID
final String activationId = authentication.getActivationId();
// Fetch token ID from the request
final TokenRemoveRequest requestObject = request.getRequestObject();
final String tokenId = requestObject.getTokenId();
// Remove a token, ignore response, since the endpoint should quietly return
powerAuthClient.removeToken(tokenId, activationId);
// Prepare a response
final TokenRemoveResponse responseObject = new TokenRemoveResponse();
responseObject.setTokenId(requestObject.getTokenId());
return new ObjectResponse<>(responseObject);
} else {
throw new PowerAuthAuthenticationException();
}
} catch (PowerAuthAuthenticationException ex) {
throw ex;
} catch (Exception ex) {
throw new PowerAuthAuthenticationException(ex.getMessage());
}
}
use of io.getlime.security.powerauth.rest.api.base.exception.PowerAuthAuthenticationException in project powerauth-restful-integration by lime-company.
the class ActivationController method removeActivation.
/**
* Get activation status.
* @param signatureHeader PowerAuth signature HTTP header.
* @return PowerAuth RESTful response with {@link ActivationRemoveResponse} payload.
* @throws PowerAuthActivationException In case activation access fails.
* @throws PowerAuthAuthenticationException In case the signature validation fails.
*/
@RequestMapping(value = "remove", method = RequestMethod.POST)
@ResponseBody
public ObjectResponse<ActivationRemoveResponse> removeActivation(@RequestHeader(value = PowerAuthSignatureHttpHeader.HEADER_NAME) String signatureHeader) throws PowerAuthActivationException, PowerAuthAuthenticationException {
try {
PowerAuthApiAuthentication apiAuthentication = authenticationProvider.validateRequestSignature("POST", null, "/pa/activation/remove", signatureHeader);
if (apiAuthentication != null && apiAuthentication.getActivationId() != null) {
RemoveActivationResponse soapResponse = powerAuthClient.removeActivation(apiAuthentication.getActivationId());
ActivationRemoveResponse response = new ActivationRemoveResponse();
response.setActivationId(soapResponse.getActivationId());
return new ObjectResponse<>(response);
} else {
throw new PowerAuthAuthenticationException("USER_NOT_AUTHENTICATED");
}
} catch (PowerAuthAuthenticationException ex) {
throw ex;
} catch (Exception ex) {
throw new PowerAuthActivationException();
}
}
use of io.getlime.security.powerauth.rest.api.base.exception.PowerAuthAuthenticationException 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.rest.api.base.exception.PowerAuthAuthenticationException in project powerauth-restful-integration by lime-company.
the class TokenController method createToken.
@RequestMapping(value = "create", method = RequestMethod.POST)
@PowerAuth(resourceId = "/pa/token/create", signatureType = { PowerAuthSignatureTypes.POSSESSION, PowerAuthSignatureTypes.POSSESSION_KNOWLEDGE, PowerAuthSignatureTypes.POSSESSION_BIOMETRY, PowerAuthSignatureTypes.POSSESSION_KNOWLEDGE_BIOMETRY })
@ResponseBody
public ObjectResponse<TokenCreateResponse> createToken(@RequestBody ObjectRequest<TokenCreateRequest> request, PowerAuthApiAuthentication authentication) throws PowerAuthAuthenticationException {
try {
if (authentication != null && authentication.getActivationId() != null) {
// Fetch activation ID and signature type
final String activationId = authentication.getActivationId();
final PowerAuthSignatureTypes signatureFactors = authentication.getSignatureFactors();
// Fetch data from the request
final TokenCreateRequest requestObject = request.getRequestObject();
final String ephemeralPublicKey = requestObject.getEphemeralPublicKey();
// Prepare a signature type converter
SignatureTypeConverter converter = new SignatureTypeConverter();
// Create a token
final CreateTokenResponse token = powerAuthClient.createToken(activationId, ephemeralPublicKey, converter.convertFrom(signatureFactors));
// Prepare a response
final TokenCreateResponse responseObject = new TokenCreateResponse();
responseObject.setMac(token.getMac());
responseObject.setEncryptedData(token.getEncryptedData());
return new ObjectResponse<>(responseObject);
} else {
throw new PowerAuthAuthenticationException();
}
} catch (PowerAuthAuthenticationException ex) {
throw ex;
} catch (Exception ex) {
throw new PowerAuthAuthenticationException(ex.getMessage());
}
}
use of io.getlime.security.powerauth.rest.api.base.exception.PowerAuthAuthenticationException in project powerauth-restful-integration by lime-company.
the class PowerAuthExceptionHandler method handleUnauthorizedException.
/**
* Handle PowerAuthAuthenticationException exceptions.
* @param ex Exception instance.
* @return Error response.
*/
@ExceptionHandler(value = PowerAuthAuthenticationException.class)
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
@ResponseBody
public ErrorResponse handleUnauthorizedException(Exception ex) {
PowerAuthAuthenticationException paex = (PowerAuthAuthenticationException) ex;
Logger.getLogger(PowerAuthExceptionHandler.class.getName()).log(Level.SEVERE, paex.getMessage(), paex);
return new ErrorResponse(paex.getDefaultCode(), paex);
}
Aggregations