
com.visionarts.powerjambda.actions.AbstractSecureAction Maven / Gradle / Ivy
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.visionarts.powerjambda.actions;
import com.amazonaws.services.lambda.runtime.Context;
import com.visionarts.powerjambda.annotations.ExceptionHandler;
import com.visionarts.powerjambda.exceptions.UnauthorizedException;
import com.visionarts.powerjambda.models.ActionRequest;
import com.visionarts.powerjambda.models.DefaultErrorResponseBody;
import com.visionarts.powerjambda.models.ResponseEntity;
/**
* This abstract class provides a method to authenticate client.
* All action classes with the credentials must inherit this abstract class.
*
* @param The type of request body
* @param The type of action result to return the response
* @param The type of client credentials
*/
public abstract class AbstractSecureAction extends AbstractLambdaAction {
/**
* Authenticates and returns a credential when the authentication is successful.
*
*
* @param request The action request
* @param context The Lambda context passed by the AWS Lambda environment
* @return The client credentials object if the authentication is successful
* @throws UnauthorizedException Thrown if an authentication request is rejected
* @throws Exception Thrown if unknown error occurs
*/
protected abstract CredentialsT authenticate(ActionRequest request, Context context)
throws UnauthorizedException, Exception;
@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity exceptionHandler(UnauthorizedException ex, Context context) {
logger.error("Catching Unauthorized Error at default exception handler", ex);
DefaultErrorResponseBody body = new DefaultErrorResponseBody("Unauthorized Error", ex.getMessage());
return new ResponseEntity()
.statusCode(401)
.body(body);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy