JaasAuthenticator extends BaseAuthenticator { @Override protected boolean doAuthenticate(HttpServletRequest pRequest, AuthorizationHeaderParser.Result pAuthInfo) { try { String user = pAuthInfo.getUser(); String password = pAuthInfo.getPassword(); final CallbackHandler handler = new UserPasswordCallbackHandler(user, password); LoginContext loginContext = createLoginContext(realm, handler); loginContext.login(); pRequest.setAttribute(HttpContext.AUTHENTICATION_TYPE,HttpServletRequest.BASIC_AUTH); pRequest.setAttribute(HttpContext.REMOTE_USER, user); pRequest.setAttribute(ConfigKey.JAAS_SUBJECT_REQUEST_ATTRIBUTE,loginContext.getSubject()); return true; } catch (LoginException e) { return false; } } JaasAuthenticator(String pRealm);   }
@Test public void testAuthenticateNoLoginModule() throws Exception { HttpServletRequest req = createMock(HttpServletRequest.class); JaasAuthenticator auth = new JaasAuthenticator("jolokia"); replay(req); assertFalse(auth.doAuthenticate(req, info)); }
@Test public void testAuthenticationPositive() throws Exception { HttpServletRequest req = prepareRequest(); JaasAuthenticator auth = new JaasAuthenticator("jolokia") { @Override protected LoginContext createLoginContext(String realm, CallbackHandler handler) throws LoginException { LoginContext mockLogin = mock(LoginContext.class); mockLogin.login(); expect(mockLogin.getSubject()).andReturn(SUBJECT); replay(mockLogin); return mockLogin; } }; assertTrue(auth.doAuthenticate(req, info)); }
@Test public void testAuthenticationNegative() throws Exception { HttpServletRequest req = prepareRequest(); JaasAuthenticator auth = new JaasAuthenticator("jolokia") { @Override protected LoginContext createLoginContext(String realm, CallbackHandler handler) throws LoginException { LoginContext mockLogin = mock(LoginContext.class); mockLogin.login(); EasyMock.expectLastCall().andThrow(new LoginException("Failed")); replay(mockLogin); return mockLogin; } }; assertFalse(auth.doAuthenticate(req, info)); }