AbstractLoginModule implements LoginModule { @Nullable protected Credentials getCredentials() { Set<Class> supported = getSupportedCredentials(); if (callbackHandler != null) { log.debug("Login: retrieving Credentials using callback."); try { CredentialsCallback callback = new CredentialsCallback(); callbackHandler.handle(new Callback[]{callback}); Credentials creds = callback.getCredentials(); if (creds != null && supported.contains(creds.getClass())) { log.debug("Login: Credentials '{}' obtained from callback", creds); return creds; } else { log.debug("Login: No supported credentials obtained from callback; trying shared state."); } } catch (UnsupportedCallbackException | IOException e) { onError(); log.error(e.getMessage(), e); } } Credentials creds = getSharedCredentials(); if (creds != null && supported.contains(creds.getClass())) { log.debug("Login: Credentials obtained from shared state."); return creds; } else { log.debug("Login: No supported credentials found in shared state; looking for credentials in subject."); for (Class clz : getSupportedCredentials()) { Set<Credentials> cds = subject.getPublicCredentials(clz); if (!cds.isEmpty()) { log.debug("Login: Credentials found in subject."); return cds.iterator().next(); } } } log.debug("No credentials found."); return null; }  @Override void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options); @Override boolean logout(); @Override boolean abort(); static final String SHARED_KEY_CREDENTIALS; static final String SHARED_KEY_LOGIN_NAME; static final String SHARED_KEY_ATTRIBUTES; static final String SHARED_KEY_PRE_AUTH_LOGIN; }
@Test public void testGetCredentialsFromSharedState() { Map<String, Credentials> sharedState = new HashMap<>(); sharedState.put(SHARED_KEY_CREDENTIALS, new TestCredentials()); AbstractLoginModule lm = initLoginModule(TestCredentials.class, sharedState); assertTrue(lm.getCredentials() instanceof TestCredentials); SimpleCredentials sc = new SimpleCredentials("test", "test".toCharArray()); sharedState.put(SHARED_KEY_CREDENTIALS, sc); lm = initLoginModule(TestCredentials.class, sharedState); assertNull(lm.getCredentials()); sharedState.put(SHARED_KEY_CREDENTIALS, sc); lm = initLoginModule(SimpleCredentials.class, sharedState); assertTrue(lm.getCredentials() instanceof SimpleCredentials); sharedState.clear(); lm = initLoginModule(TestCredentials.class, sharedState); assertNull(lm.getCredentials()); }
@Test public void testGetCredentialsIOException() { LoginModuleMonitor monitor = mock(LoginModuleMonitor.class); AbstractLoginModule lm = initLoginModule(new ThrowingCallbackHandler(true), monitor); assertNull(lm.getCredentials()); verify(monitor, times(1)).loginError(); }
@Test public void testGetCredentialsUnsupportedCallbackException() { LoginModuleMonitor monitor = mock(LoginModuleMonitor.class); AbstractLoginModule lm = initLoginModule(new ThrowingCallbackHandler(false), monitor); assertNull(lm.getCredentials()); verify(monitor, times(1)).loginError(); }
@Test public void testGetCredentialsCallbackReturnsNull() { CallbackHandler cbh = callbacks -> { for (Callback cb : callbacks) { if (cb instanceof CredentialsCallback) { ((CredentialsCallback) cb).setCredentials(null); } } }; AbstractLoginModule lm = initLoginModule(cbh); assertNull(lm.getCredentials()); }