PreAuthenticatedAuthenticationProvider extends TimeMeasuringAuthenticationProvider<AuthenticationResult> { @Override protected AuthenticationResult tryToAuthenticate(final RequestContext requestContext, final DittoHeaders dittoHeaders) { final Optional<String> preAuthOpt = getPreAuthenticated(requestContext); if (preAuthOpt.isEmpty()) { return DefaultAuthenticationResult.failed(dittoHeaders, getAuthenticationFailedException(dittoHeaders)); } final String preAuthenticatedSubject = preAuthOpt.get(); final List<AuthorizationSubject> authorizationSubjects = getAuthorizationSubjects(preAuthenticatedSubject); if (authorizationSubjects.isEmpty()) { return toFailedAuthenticationResult( buildFailedToExtractAuthorizationSubjectsException(preAuthenticatedSubject, dittoHeaders), dittoHeaders); } final AuthorizationContext authContext = AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.PRE_AUTHENTICATED_HTTP, authorizationSubjects); LOGGER.withCorrelationId(dittoHeaders) .info("Pre-authentication has been applied resulting in AuthorizationContext <{}>.", authContext); return DefaultAuthenticationResult.successful(dittoHeaders, authContext); } private  PreAuthenticatedAuthenticationProvider(); static PreAuthenticatedAuthenticationProvider getInstance(); @Override boolean isApplicable(final RequestContext requestContext); @Override AuthorizationContextType getType(final RequestContext requestContext);  }
@Test public void doExtractAuthenticationFails() { final RequestContext requestContext = mockRequestContext(null, null); final AuthenticationResult authenticationResult = underTest.tryToAuthenticate(requestContext, knownDittoHeaders); assertThat(authenticationResult.isSuccess()).isFalse(); }
@Test public void doExtractAuthenticationFailsWithEmptyDummyAuthHeader() { final RequestContext requestContext = mockRequestContext(HttpHeader.parse(DUMMY_AUTH_HEADER_NAME, ""), null); final AuthenticationResult authenticationResult = underTest.tryToAuthenticate(requestContext, knownDittoHeaders); assertThat(authenticationResult.isSuccess()).isFalse(); }
@Test public void doExtractAuthenticationFailsWithEmptyDummyAuthQueryParam() { final RequestContext requestContext = mockRequestContext(null, Query.create(Pair.create(DUMMY_AUTH_HEADER_NAME, ""))); final AuthenticationResult authenticationResult = underTest.tryToAuthenticate(requestContext, knownDittoHeaders); assertThat(authenticationResult.isSuccess()).isFalse(); }
@Test public void doExtractAuthenticationByHeader() { final RequestContext requestContext = mockRequestContext(DUMMY_AUTH_HEADER, null); final AuthenticationResult authenticationResult = underTest.tryToAuthenticate(requestContext, knownDittoHeaders); assertThat(authenticationResult.isSuccess()).isTrue(); }
@Test public void doExtractAuthenticationByQueryParam() { final RequestContext requestContext = mockRequestContext(null, DUMMY_AUTH_QUERY); final AuthenticationResult authenticationResult = underTest.tryToAuthenticate(requestContext, knownDittoHeaders); assertThat(authenticationResult.isSuccess()).isTrue(); }
@Test public void doExtractAuthenticationByQueryParamAndHeader() { final RequestContext requestContext = mockRequestContext(DUMMY_AUTH_HEADER, DUMMY_AUTH_QUERY); final AuthenticationResult authenticationResult = underTest.tryToAuthenticate(requestContext, knownDittoHeaders); assertThat(authenticationResult.isSuccess()).isTrue(); }