Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BAU] - Add error handling for session and token expiry #137

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common-lambdas
2 changes: 1 addition & 1 deletion common-lib
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.nimbusds.oauth2.sdk.OAuth2Error;
import org.apache.logging.log4j.Level;
import software.amazon.awssdk.http.HttpStatusCode;
import software.amazon.awssdk.services.sqs.SqsClient;
Expand Down Expand Up @@ -105,11 +106,16 @@ public APIGatewayProxyResponseEvent handleRequest(
} catch (SessionNotFoundException | SessionExpiredException e) {
eventProbe.log(Level.ERROR, e).counterMetric(LAMBDA_NAME, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
HttpStatusCode.BAD_REQUEST, e.getMessage());
OAuth2Error.INVALID_REQUEST.getHTTPStatusCode(),
OAuth2Error.INVALID_REQUEST
.appendDescription(" - " + e.getMessage())
.toJSONObject());
} catch (AddressProcessingException | SqsException e) {
eventProbe.log(Level.ERROR, e).counterMetric(LAMBDA_NAME, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
HttpStatusCode.INTERNAL_SERVER_ERROR, e.getMessage());
}
OAuth2Error.SERVER_ERROR.getHTTPStatusCode(),
OAuth2Error.SERVER_ERROR
.appendDescription(" - " + e.getMessage())
.toJSONObject()); }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jwt.SignedJWT;
import com.nimbusds.oauth2.sdk.OAuth2Error;
import com.nimbusds.oauth2.sdk.ParseException;
import com.nimbusds.oauth2.sdk.token.AccessToken;
import com.nimbusds.oauth2.sdk.token.AccessTokenType;
Expand All @@ -23,6 +24,9 @@
import uk.gov.di.ipv.cri.address.library.service.AddressService;
import uk.gov.di.ipv.cri.common.library.domain.AuditEventType;
import uk.gov.di.ipv.cri.common.library.error.ErrorResponse;
import uk.gov.di.ipv.cri.common.library.exception.AccessTokenExpiredException;
import uk.gov.di.ipv.cri.common.library.exception.SessionExpiredException;
import uk.gov.di.ipv.cri.common.library.exception.SessionNotFoundException;
import uk.gov.di.ipv.cri.common.library.exception.SqsException;
import uk.gov.di.ipv.cri.common.library.service.AuditService;
import uk.gov.di.ipv.cri.common.library.service.ConfigurationService;
Expand All @@ -44,7 +48,7 @@ public class IssueCredentialHandler
private final VerifiableCredentialService verifiableCredentialService;
private final AddressService addressService;
private final SessionService sessionService;
private EventProbe eventProbe;
private final EventProbe eventProbe;
private final AuditService auditService;

public IssueCredentialHandler(
Expand Down Expand Up @@ -86,7 +90,7 @@ public APIGatewayProxyResponseEvent handleRequest(

try {
var accessToken = validateInputHeaderBearerToken(input.getHeaders());
var sessionItem = this.sessionService.getSessionByAccessToken(accessToken);
var sessionItem = sessionService.getSessionByAccessToken(accessToken);
var addressItem = addressService.getAddressItem(sessionItem.getSessionId());

SignedJWT signedJWT =
Expand All @@ -101,15 +105,43 @@ public APIGatewayProxyResponseEvent handleRequest(
eventProbe.log(ERROR, ex).counterMetric(ADDRESS_CREDENTIAL_ISSUER, 0d);

return ApiGatewayResponseGenerator.proxyJsonResponse(
HttpStatusCode.INTERNAL_SERVER_ERROR, ex.awsErrorDetails().errorMessage());
OAuth2Error.SERVER_ERROR.getHTTPStatusCode(),
OAuth2Error.SERVER_ERROR.appendDescription(" - " + ex.awsErrorDetails().errorMessage()));
} catch (CredentialRequestException | ParseException | JOSEException e) {
eventProbe.log(ERROR, e).counterMetric(ADDRESS_CREDENTIAL_ISSUER, 0d);

return ApiGatewayResponseGenerator.proxyJsonResponse(
HttpStatusCode.BAD_REQUEST, ErrorResponse.VERIFIABLE_CREDENTIAL_ERROR);
OAuth2Error.INVALID_REQUEST.getHTTPStatusCode(),
OAuth2Error.INVALID_REQUEST
.appendDescription(" - " + ErrorResponse.VERIFIABLE_CREDENTIAL_ERROR)
.toJSONObject());
} catch (SqsException sqsException) {
return ApiGatewayResponseGenerator.proxyJsonResponse(
HttpStatusCode.INTERNAL_SERVER_ERROR, sqsException.getMessage());
OAuth2Error.SERVER_ERROR.getHTTPStatusCode(),
OAuth2Error.SERVER_ERROR
.appendDescription(" - " + sqsException.getMessage())
.toJSONObject());
} catch (AccessTokenExpiredException e) {
eventProbe.log(ERROR, e).counterMetric(ADDRESS_CREDENTIAL_ISSUER, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
OAuth2Error.ACCESS_DENIED.getHTTPStatusCode(),
OAuth2Error.ACCESS_DENIED
.appendDescription(" - " + ErrorResponse.ACCESS_TOKEN_EXPIRED)
.toJSONObject());
} catch (SessionExpiredException e) {
eventProbe.log(ERROR, e).counterMetric(ADDRESS_CREDENTIAL_ISSUER, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
OAuth2Error.ACCESS_DENIED.getHTTPStatusCode(),
OAuth2Error.ACCESS_DENIED
.appendDescription(" - " + ErrorResponse.SESSION_EXPIRED)
.toJSONObject());
} catch (SessionNotFoundException e) {
eventProbe.log(ERROR, e).counterMetric(ADDRESS_CREDENTIAL_ISSUER, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
OAuth2Error.ACCESS_DENIED.getHTTPStatusCode(),
OAuth2Error.ACCESS_DENIED
.appendDescription(" - " + ErrorResponse.SESSION_NOT_FOUND)
.toJSONObject());
}
}

Expand Down
3 changes: 2 additions & 1 deletion lambdas/postcode-lookup/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ dependencies {
implementation project(":common-lib"),
configurations.aws,
configurations.lambda,
configurations.gson
configurations.gson,
configurations.nimbus

aspect configurations.powertools

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.nimbusds.oauth2.sdk.OAuth2Error;
import org.apache.logging.log4j.Level;
import software.amazon.awssdk.http.HttpStatusCode;
import software.amazon.lambda.powertools.logging.CorrelationIdPathConstants;
Expand Down Expand Up @@ -67,15 +68,31 @@ public APIGatewayProxyResponseEvent handleRequest(
} catch (PostcodeLookupValidationException e) {
eventProbe.log(Level.ERROR, e).counterMetric(LAMBDA_NAME, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
HttpStatusCode.BAD_REQUEST, ErrorResponse.INVALID_POSTCODE);
} catch (SessionExpiredException | SessionNotFoundException e) {
OAuth2Error.INVALID_REQUEST.getHTTPStatusCode(),
OAuth2Error.INVALID_REQUEST
.appendDescription(" - " + ErrorResponse.INVALID_POSTCODE)
.toJSONObject());
} catch (SessionExpiredException e) {
eventProbe.log(Level.ERROR, e).counterMetric(LAMBDA_NAME, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
HttpStatusCode.BAD_REQUEST, e.getMessage());
OAuth2Error.ACCESS_DENIED.getHTTPStatusCode(),
OAuth2Error.ACCESS_DENIED
.appendDescription(" - " + ErrorResponse.SESSION_EXPIRED)
.toJSONObject());
} catch (SessionNotFoundException e) {
eventProbe.log(Level.ERROR, e).counterMetric(LAMBDA_NAME, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
OAuth2Error.ACCESS_DENIED.getHTTPStatusCode(),
OAuth2Error.ACCESS_DENIED
.appendDescription(" - " + ErrorResponse.SESSION_NOT_FOUND)
.toJSONObject());
} catch (Exception e) {
eventProbe.log(Level.ERROR, e).counterMetric(LAMBDA_NAME, 0d);
return ApiGatewayResponseGenerator.proxyJsonResponse(
HttpStatusCode.INTERNAL_SERVER_ERROR, ErrorResponse.SERVER_ERROR);
OAuth2Error.SERVER_ERROR.getHTTPStatusCode(),
OAuth2Error.SERVER_ERROR
.appendDescription(" - " + e.getMessage())
.toJSONObject());
}
}
}