RequestAuthenticator { public AuthContext authenticate(Request request) { final boolean hasAuthHeader = request.header(HttpHeaders.AUTHORIZATION).isPresent(); if (!hasAuthHeader) { return Optional::empty; } final String authHeader = request.header(HttpHeaders.AUTHORIZATION).get(); if (!authHeader.startsWith(BEARER_PREFIX)) { throw new ResponseException(Response.forStatus(Status.BAD_REQUEST .withReasonPhrase("Authorization token must be of type Bearer"))); } final GoogleIdToken googleIdToken; try { googleIdToken = authenticator.authenticate(authHeader.substring(BEARER_PREFIX.length())); } catch (IllegalArgumentException e) { throw new ResponseException(Response.forStatus(Status.BAD_REQUEST .withReasonPhrase("Failed to parse Authorization token")), e); } if (googleIdToken == null) { throw new ResponseException(Response.forStatus(Status.UNAUTHORIZED .withReasonPhrase("Authorization token is invalid"))); } return () -> Optional.of(googleIdToken); } RequestAuthenticator(Authenticator authenticator); AuthContext authenticate(Request request);  }
@Test public void testAuthorizationHeaderMissing() { final Request request = Request.forUri("/", "PUT") .withPayload(ByteString.encodeUtf8("hello")); final AuthContext authContext = sut.authenticate(request); assertThat(authContext.user(), is(Optional.empty())); }
@Test public void testAuthorizationMissingBearer() { final Request request = Request.forUri("/", "PUT") .withHeader(HttpHeaders.AUTHORIZATION, "broken") .withPayload(ByteString.encodeUtf8("hello")); try { sut.authenticate(request); fail(); } catch (ResponseException e) { assertThat(e.getResponse().status(), is( Status.BAD_REQUEST.withReasonPhrase("Authorization token must be of type Bearer"))); } }
@Test public void testInvalidToken() { when(authenticator.authenticate(any())).thenThrow(IllegalArgumentException.class); final Request request = Request.forUri("/", "PUT") .withHeader(HttpHeaders.AUTHORIZATION, "Bearer broken") .withPayload(ByteString.encodeUtf8("hello")); try { sut.authenticate(request); fail(); } catch (ResponseException e) { assertThat(e.getResponse().status(), is( Status.BAD_REQUEST.withReasonPhrase("Failed to parse Authorization token"))); } }
@Test public void testAuthenticationFailure() { when(authenticator.authenticate(any())).thenReturn(null); final Request request = Request.forUri("/", "PUT") .withHeader(HttpHeaders.AUTHORIZATION, "Bearer token") .withPayload(ByteString.encodeUtf8("hello")); try { sut.authenticate(request); fail(); } catch (ResponseException e) { assertThat(e.getResponse().status(), is( Status.UNAUTHORIZED .withReasonPhrase("Authorization token is invalid"))); } }
@Test public void testSuccessfulAuthorization() { when(authenticator.authenticate(any())).thenReturn(idToken); final Request request = Request.forUri("/", "PUT") .withHeader(HttpHeaders.AUTHORIZATION, "Bearer token") .withPayload(ByteString.encodeUtf8("hello")); final AuthContext authContext = sut.authenticate(request); assertThat(authContext.user(), is(Optional.of(idToken))); }