@Override
	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {

		if (!Strings.isNullOrEmpty(request.getParameter("error"))) {

			// there's an error coming back from the server, need to handle this
			handleError(request, response);
			return null; // no auth, response is sent to display page or something

		} else if (!Strings.isNullOrEmpty(request.getParameter("code"))) {

			// we got back the code, need to process this to get our tokens
			Authentication auth = handleAuthorizationCodeResponse(request, response);
			return auth;

		} else {

			// not an error, not a code, must be an initial login of some type
			handleAuthorizationRequest(request, response);

			return null; // no auth, response redirected to the server's Auth Endpoint (or possibly to the account chooser)
		}

	}
@Test
	public void attemptAuthentication_error() throws Exception {

		HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
		Mockito.when(request.getParameter("error")).thenReturn("Error");
		Mockito.when(request.getParameter("error_description")).thenReturn("Description");
		Mockito.when(request.getParameter("error_uri")).thenReturn("http://example.com");

		try {
			filter.attemptAuthentication(request, mock(HttpServletResponse.class));

			fail("AuthorizationEndpointException expected.");
		}
		catch (AuthorizationEndpointException exception) {
			assertThat(exception.getMessage(),
					is("Error from Authorization Endpoint: Error Description http://example.com"));

			assertThat(exception.getError(), is("Error"));
			assertThat(exception.getErrorDescription(), is("Description"));
			assertThat(exception.getErrorURI(), is("http://example.com"));

			assertThat(exception, is(instanceOf(AuthenticationServiceException.class)));
		}
	}