LoginModuleImpl extends AbstractLoginModule { @Override public boolean login() throws LoginException { credentials = getCredentials(); PreAuthenticatedLogin preAuthLogin = getSharedPreAuthLogin(); String loginName = getLoginId(preAuthLogin); Authentication authentication = getUserAuthentication(loginName); if (authentication != null) { if (preAuthLogin != null) { success = authentication.authenticate(PreAuthenticatedLogin.PRE_AUTHENTICATED); } else { success = authentication.authenticate(credentials); } if (success) { log.debug("Adding Credentials to shared state."); sharedState.put(SHARED_KEY_CREDENTIALS, credentials); log.debug("Adding login name to shared state."); sharedState.put(SHARED_KEY_LOGIN_NAME, loginName); userId = authentication.getUserId(); if (userId == null) { userId = loginName; } principal = authentication.getUserPrincipal(); } } else { credentials = null; userId = null; } return success; }  @Override boolean login(); @Override boolean commit(); @Override boolean logout();  }
@Test(expected = LoginException.class) public void testNullLogin() throws Exception { try (ContentSession cs = login(null)) { fail("Null login should fail"); } }
@Test public void testUserLogin() throws Exception { createTestUser(); try (ContentSession cs = login(new SimpleCredentials(USER_ID, USER_PW.toCharArray()))) { AuthInfo authInfo = cs.getAuthInfo(); assertEquals(USER_ID, authInfo.getUserID()); } }
@Test public void testAuthInfoContainsUserId() throws Exception { createTestUser(); try (ContentSession cs = login(new SimpleCredentials(USER_ID_CASED, USER_PW.toCharArray()))) { AuthInfo authInfo = cs.getAuthInfo(); assertEquals(user.getID(), authInfo.getUserID()); } }
@Test public void testUserLoginIsCaseInsensitive() throws Exception { createTestUser(); try (ContentSession cs = login(new SimpleCredentials(USER_ID_CASED, USER_PW.toCharArray()))) { AuthInfo authInfo = cs.getAuthInfo(); UserManager userMgr = getUserManager(root); Authorizable auth = userMgr.getAuthorizable(authInfo.getUserID()); assertNotNull(auth); assertTrue(auth.getID().equalsIgnoreCase(USER_ID_CASED)); } }
@Test public void testUserLoginIsCaseInsensitive2() throws Exception { createTestUser(); try (ContentSession cs = login(new SimpleCredentials(USER_ID_CASED, USER_PW.toCharArray()))) { AuthInfo authInfo = cs.getAuthInfo(); assertEquals(user.getID(), authInfo.getUserID()); assertTrue(USER_ID_CASED.equalsIgnoreCase(authInfo.getUserID())); } }
@Test(expected = LoginException.class) public void testUnknownUserLogin() throws Exception { try (ContentSession cs = login(new SimpleCredentials("unknown", "".toCharArray()))) { fail("Unknown user must not be able to login"); } }
@Test public void testSelfImpersonation() throws Exception { createTestUser(); AuthInfo authInfo; try (ContentSession cs = login(new SimpleCredentials(USER_ID, USER_PW.toCharArray()))) { authInfo = cs.getAuthInfo(); assertEquals(USER_ID, authInfo.getUserID()); } SimpleCredentials sc = new SimpleCredentials(USER_ID, new char[0]); ImpersonationCredentials ic = new ImpersonationCredentials(sc, authInfo); try (ContentSession cs = login(ic)) { authInfo = cs.getAuthInfo(); assertEquals(USER_ID, authInfo.getUserID()); } }
@Test(expected = LoginException.class) public void testInvalidImpersonation() throws Exception { createTestUser(); AuthInfo authInfo; try (ContentSession cs = login(new SimpleCredentials(USER_ID, USER_PW.toCharArray()))) { authInfo = cs.getAuthInfo(); assertEquals(USER_ID, authInfo.getUserID()); } ConfigurationParameters config = securityProvider.getConfiguration(UserConfiguration.class).getParameters(); String adminId = UserUtil.getAdminId(config); SimpleCredentials sc = new SimpleCredentials(adminId, new char[0]); ImpersonationCredentials ic = new ImpersonationCredentials(sc, authInfo); try (ContentSession cs = login(ic)) { fail("User 'test' should not be allowed to impersonate " + adminId); } }
@Test public void testLoginWithAttributes( ) throws Exception { createTestUser(); SimpleCredentials sc = new SimpleCredentials(USER_ID, USER_PW.toCharArray()); sc.setAttribute("attr", "value"); try (ContentSession cs = login(sc)){ AuthInfo authInfo = cs.getAuthInfo(); assertTrue(Arrays.asList(authInfo.getAttributeNames()).contains("attr")); assertEquals("value", authInfo.getAttribute("attr")); } }
@Test public void testImpersonationWithAttributes() throws Exception { createTestUser(); AuthInfo authInfo; try (ContentSession cs = login(new SimpleCredentials(USER_ID, USER_PW.toCharArray()))) { authInfo = cs.getAuthInfo(); } SimpleCredentials sc = new SimpleCredentials(USER_ID, new char[0]); sc.setAttribute("attr", "value"); ImpersonationCredentials ic = new ImpersonationCredentials(sc, authInfo); try (ContentSession cs = login(ic)) { authInfo = cs.getAuthInfo(); assertTrue(Arrays.asList(authInfo.getAttributeNames()).contains("attr")); assertEquals("value", authInfo.getAttribute("attr")); } }
@Test(expected = LoginException.class) public void testImpersonationWithUnsupportedBaseCredentials() throws Exception { Credentials baseCredentials = mock(Credentials.class); ImpersonationCredentials ic = new ImpersonationCredentials(baseCredentials, new AuthInfoImpl(USER_ID, null, null)); try (ContentSession cs = login(ic)) { fail("Base credentials of ImpersonationCredentials can only be SimpleCredentials."); } }
@Test public void LoginUnsupportedCredentials() throws Exception { Credentials unsupportedCredentials = mock(Credentials.class); CallbackHandler cbh = callbacks -> { for (Callback callback : callbacks) { if (callback instanceof RepositoryCallback) { ((RepositoryCallback) callback).setSecurityProvider(getSecurityProvider()); ((RepositoryCallback) callback).setContentRepository(getContentRepository()); } else if (callback instanceof CredentialsCallback) { ((CredentialsCallback) callback).setCredentials(unsupportedCredentials); } else { throw new UnsupportedCallbackException(callback); } } }; Subject subject = new Subject(false, ImmutableSet.of(), ImmutableSet.of(unsupportedCredentials), ImmutableSet.of()); LoginModuleImpl lm = new LoginModuleImpl(); lm.initialize(subject, cbh, Maps.newHashMap(), Maps.newHashMap()); assertFalse(lm.login()); }
@Test public void testLoginLogoutPreexistingReadonlySubject() throws Exception { createTestUser(); Subject subject = new Subject(true, Collections.singleton(() -> "JMXPrincipal: foo"), Collections.EMPTY_SET, Collections.EMPTY_SET); Subject.doAs(subject, (PrivilegedExceptionAction<Void>) () -> { LogCustomizer logCustomizer = LogCustomizer .forLogger("org.apache.jackrabbit.oak.core.ContentSessionImpl") .enable(Level.ERROR) .create(); ContentSession cs = login(new SimpleCredentials(USER_ID, USER_PW.toCharArray())); try { logCustomizer.starting(); cs.close(); } finally { assertEquals(0, logCustomizer.getLogs().size()); logCustomizer.finished(); } return null; }); }