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.
*/
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Path("unlock")
public ObjectResponse<VaultUnlockResponse> unlockVault(@HeaderParam(PowerAuthSignatureHttpHeader.HEADER_NAME) String signatureHeader, ObjectRequest<VaultUnlockRequest> request, @Context HttpServletRequest httpServletRequest) throws PowerAuthAuthenticationException, PowerAuthSecureVaultException {
try {
PowerAuthSignatureHttpHeader header = new PowerAuthSignatureHttpHeader().fromValue(signatureHeader);
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();
PowerAuthPortServiceStub.SignatureType signatureType = converter.convertFrom(header.getSignatureType());
String nonce = header.getNonce();
String reason = null;
if (request != null) {
VaultUnlockRequest vaultUnlockRequest = request.getRequestObject();
if (vaultUnlockRequest != null && vaultUnlockRequest.getReason() != null) {
reason = vaultUnlockRequest.getReason();
}
}
String requestBodyString = ((String) httpServletRequest.getAttribute(PowerAuthRequestFilterBase.POWERAUTH_SIGNATURE_BASE_STRING));
byte[] requestBodyBytes = requestBodyString == null ? null : BaseEncoding.base64().decode(requestBodyString);
String data = PowerAuthHttpBody.getSignatureBaseString("POST", "/pa/vault/unlock", BaseEncoding.base64().decode(nonce), requestBodyBytes);
PowerAuthPortServiceStub.VaultUnlockResponse soapResponse = powerAuthClient.unlockVault(activationId, applicationId, data, signature, signatureType, reason);
if (!soapResponse.getSignatureValid()) {
throw new PowerAuthAuthenticationException();
}
VaultUnlockResponse response = new VaultUnlockResponse();
response.setActivationId(soapResponse.getActivationId());
response.setEncryptedVaultEncryptionKey(soapResponse.getEncryptedVaultEncryptionKey());
return new ObjectResponse<>(response);
} catch (PowerAuthAuthenticationException ex) {
throw ex;
} catch (Exception ex) {
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.
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Path("create")
public ObjectResponse<TokenCreateResponse> createToken(ObjectRequest<TokenCreateRequest> request, @HeaderParam(PowerAuthTokenHttpHeader.HEADER_NAME) String tokenHeader) throws RemoteException, PowerAuthAuthenticationException {
try {
PowerAuthApiAuthentication authentication = authenticationProvider.validateToken(tokenHeader, Arrays.asList(PowerAuthSignatureTypes.POSSESSION, PowerAuthSignatureTypes.POSSESSION_KNOWLEDGE, PowerAuthSignatureTypes.POSSESSION_BIOMETRY, PowerAuthSignatureTypes.POSSESSION_KNOWLEDGE_BIOMETRY));
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 PowerAuthPortServiceStub.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 TokenController method removeToken.
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Path("remove")
public ObjectResponse<TokenRemoveResponse> removeToken(ObjectRequest<TokenRemoveRequest> request, @HeaderParam(PowerAuthTokenHttpHeader.HEADER_NAME) String tokenHeader) throws RemoteException, PowerAuthAuthenticationException {
try {
PowerAuthApiAuthentication authentication = authenticationProvider.validateToken(tokenHeader, Arrays.asList(PowerAuthSignatureTypes.POSSESSION, PowerAuthSignatureTypes.POSSESSION_KNOWLEDGE, PowerAuthSignatureTypes.POSSESSION_BIOMETRY, PowerAuthSignatureTypes.POSSESSION_KNOWLEDGE_BIOMETRY));
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 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) {
throw new PowerAuthAuthenticationException(e.getMessage());
}
// 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
PowerAuthSignatureAuthentication 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
PowerAuthApiAuthentication auth;
try {
auth = this.authenticate(powerAuthAuthentication);
} catch (RemoteException e) {
throw new PowerAuthAuthenticationException("POWER_AUTH_SIGNATURE_SOAP_ERROR");
}
// In case authentication is null, throw PowerAuth exception
if (auth == null) {
throw new PowerAuthAuthenticationException("POWER_AUTH_SIGNATURE_INVALID_VALUE");
}
return auth;
}
use of io.getlime.security.powerauth.rest.api.base.exception.PowerAuthAuthenticationException in project powerauth-restful-integration by lime-company.
the class PowerAuthAnnotationInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// requests before the actual requests.
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
// Obtain annotations
PowerAuth powerAuthSignatureAnnotation = handlerMethod.getMethodAnnotation(PowerAuth.class);
PowerAuthToken powerAuthTokenAnnotation = handlerMethod.getMethodAnnotation(PowerAuthToken.class);
// Check that only one annotation is active
if (powerAuthSignatureAnnotation != null && powerAuthTokenAnnotation != null) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "You cannot use both @PowerAuth and @PowerAuthToken on same handler method. We are removing both.");
powerAuthSignatureAnnotation = null;
powerAuthTokenAnnotation = null;
}
// Resolve @PowerAuth annotation
if (powerAuthSignatureAnnotation != null) {
try {
PowerAuthApiAuthentication authentication = this.authenticationProvider.validateRequestSignature(request, powerAuthSignatureAnnotation.resourceId(), request.getHeader(PowerAuthSignatureHttpHeader.HEADER_NAME), new ArrayList<>(Arrays.asList(powerAuthSignatureAnnotation.signatureType())));
request.setAttribute(PowerAuth.AUTHENTICATION_OBJECT, authentication);
} catch (PowerAuthAuthenticationException ex) {
// silently ignore here and make sure authentication object is null
request.setAttribute(PowerAuth.AUTHENTICATION_OBJECT, null);
}
}
// Resolve @PowerAuthToken annotation
if (powerAuthTokenAnnotation != null) {
try {
PowerAuthApiAuthentication authentication = this.authenticationProvider.validateToken(request.getHeader(PowerAuthTokenHttpHeader.HEADER_NAME), new ArrayList<>(Arrays.asList(powerAuthTokenAnnotation.signatureType())));
request.setAttribute(PowerAuth.AUTHENTICATION_OBJECT, authentication);
} catch (PowerAuthAuthenticationException ex) {
// silently ignore here and make sure authentication object is null
request.setAttribute(PowerAuth.AUTHENTICATION_OBJECT, null);
}
}
}
return super.preHandle(request, response, handler);
}
Aggregations