MachineLoginFilter extends MultiUserEnvironmentInitializationFilter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { try { super.doFilter(request, response, filterChain); } catch (NotMachineTokenJwtException mte) { filterChain.doFilter(request, response); } catch (JwtException e) { sendError( response, SC_UNAUTHORIZED, format("Machine token authentication failed: %s", e.getMessage())); return; } } @Inject  MachineLoginFilter(
      SessionStore sessionStore,
      RequestTokenExtractor tokenExtractor,
      UserManager userManager,
      MachineSigningKeyResolver machineKeyResolver,
      PermissionChecker permissionChecker); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain); @Override Subject extractSubject(String token); @Override void destroy();  }
@Test public void testProcessRequestWithValidToken() throws Exception { machineLoginFilter.doFilter(getRequestMock(), responseMock, chainMock); verify(keyManagerMock, atLeastOnce()).getOrCreateKeyPair(eq(WORKSPACE_ID)); verify(userManagerMock).getById(anyString()); verifyZeroInteractions(responseMock); }
@Test public void testNotProceedRequestWhenSignatureCheckIsFailed() throws Exception { final HttpServletRequest requestMock = getRequestMock(); final KeyPairGenerator kpg = KeyPairGenerator.getInstance(SIGNATURE_ALGORITHM); kpg.initialize(KEY_SIZE); final KeyPair pair = kpg.generateKeyPair(); when(keyManagerMock.getOrCreateKeyPair(eq(WORKSPACE_ID))).thenReturn(pair); machineLoginFilter.doFilter(requestMock, responseMock, chainMock); verify(tokenExtractorMock, atLeastOnce()).getToken(any(HttpServletRequest.class)); verify(responseMock) .sendError( 401, "Machine token authentication failed: JWT signature does not match locally computed signature." + " JWT validity cannot be asserted and should not be trusted."); }
@Test public void testProceedRequestWhenEmptyTokenProvided() throws Exception { final HttpServletRequest requestMock = getRequestMock(); when(tokenExtractorMock.getToken(any(HttpServletRequest.class))).thenReturn(null); machineLoginFilter.doFilter(requestMock, responseMock, chainMock); verify(tokenExtractorMock, atLeastOnce()).getToken(any(HttpServletRequest.class)); verify(chainMock).doFilter(requestMock, responseMock); verifyZeroInteractions(keyManagerMock); verifyZeroInteractions(userManagerMock); verifyZeroInteractions(responseMock); }
@Test public void testSetErrorInResponseWhenNoUserFoundForProvidedToken() throws Exception { when(userManagerMock.getById(anyString())).thenThrow(new NotFoundException("User not found")); machineLoginFilter.doFilter(getRequestMock(), responseMock, chainMock); verify(keyManagerMock, atLeastOnce()).getOrCreateKeyPair(eq(WORKSPACE_ID)); verify(userManagerMock).getById(anyString()); verify(responseMock) .sendError(401, "Machine token authentication failed: Corresponding user doesn't exist."); }
@Test public void testSetErrorInResponseWhenUnableToGetUserForProvidedToken() throws Exception { when(userManagerMock.getById(anyString())).thenThrow(new ServerException("err")); machineLoginFilter.doFilter(getRequestMock(), responseMock, chainMock); verify(keyManagerMock, atLeastOnce()).getOrCreateKeyPair(eq(WORKSPACE_ID)); verify(userManagerMock).getById(anyString()); verify(responseMock) .sendError(eq(401), argThat(s -> s.startsWith("Machine token authentication failed:"))); }