// reason, an exception will be thrown and the page will echo back the
// reason.
User user = AuthenticationRequest.getUser(username, password);
// Get the authorization code.
AuthorizationCode authCode =
AuthorizationCodeBin.getInstance().getCode(code);
// If the code is unknown, we cannot redirect back to the third-party
// because we don't know who they are.
if(authCode == null) {
throw new OmhException("The authorization code is unknown.");
}
// Verify that the code has not yet expired.
if(System.currentTimeMillis() > authCode.getExpirationTime()) {
response
.sendRedirect(
OAuthASResponse
.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
.setError(CodeResponse.ACCESS_DENIED)
.setErrorDescription("The code has expired.")
.location(
authCode
.getThirdParty().getRedirectUri().toString())
.setState(authCode.getState())
.buildQueryMessage()
.getLocationUri());
return;
}
// Get the response if it already exists.
AuthorizationCodeResponse codeResponse =
AuthorizationCodeResponseBin.getInstance().getResponse(code);
// If the response does not exist, attempt to create a new one and
// save it.
if(codeResponse == null) {
// Create the new code.
codeResponse =
new AuthorizationCodeResponse(authCode, user, granted);
// Store it.
AuthorizationCodeResponseBin
.getInstance().storeVerification(codeResponse);
}
// Make sure it is being verified by the same user.
else if(
! user
.getUsername().equals(codeResponse.getOwner().getUsername())) {
response
.sendRedirect(
OAuthASResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setError(CodeResponse.ACCESS_DENIED)
.setErrorDescription(
"The code has already been verified by another " +
"user.")
.location(
authCode
.getThirdParty().getRedirectUri().toString())
.setState(authCode.getState())
.buildQueryMessage()
.getLocationUri());
}
// Make sure the same grant response is being made.
else if(granted == codeResponse.getGranted()) {
response
.sendRedirect(
OAuthASResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setError(CodeResponse.ACCESS_DENIED)
.setErrorDescription(
"The user has re-submitted the same " +
"authorization code twice with competing " +
"grant values.")
.location(
authCode
.getThirdParty().getRedirectUri().toString())
.setState(authCode.getState())
.buildQueryMessage()
.getLocationUri());
}
// Otherwise, this is simply a repeat of the same request as before,
// and we can simply ignore it.
// Redirect the user back to the third-party with the authorization
// code and state.
response
.sendRedirect(
OAuthASResponse
.authorizationResponse(
request,
HttpServletResponse.SC_OK)
.location(
authCode.getThirdParty().getRedirectUri().toString())
.setCode(authCode.getCode())
.setParam("state", authCode.getState())
.buildQueryMessage()
.getLocationUri());
}