private Subject getAuthenticatedSubject(final String user, final String password, final RemotingConnection remotingConnection) throws LoginException { LoginContext lc; ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); ClassLoader thisLoader = this.getClass().getClassLoader(); try { if (thisLoader != currentLoader) { Thread.currentThread().setContextClassLoader(thisLoader); } if (certificateConfigurationName != null && certificateConfigurationName.length() > 0 && getCertsFromConnection(remotingConnection) != null) { lc = new LoginContext(certificateConfigurationName, null, new JaasCallbackHandler(user, password, remotingConnection), certificateConfiguration); } else { lc = new LoginContext(configurationName, null, new JaasCallbackHandler(user, password, remotingConnection), configuration); } lc.login(); return lc.getSubject(); } finally { if (thisLoader != currentLoader) { Thread.currentThread().setContextClassLoader(currentLoader); } } }
@Test public void testPropertyConfigMap() throws Exception { LDAPLoginModule loginModule = new LDAPLoginModule(); JaasCallbackHandler callbackHandler = new JaasCallbackHandler(null, null, null); Field configMap = null; HashMap<String, Object> options = new HashMap<>(); for (Field field: loginModule.getClass().getDeclaredFields()) { if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && field.getType().isAssignableFrom(String.class)) { field.setAccessible(true); options.put((String)field.get(loginModule), "SET"); } if (field.getName().equals("config")) { field.setAccessible(true); configMap = field; } } loginModule.initialize(new Subject(), callbackHandler, null, options); LDAPLoginProperty[] ldapProps = (LDAPLoginProperty[]) configMap.get(loginModule); for (String key: options.keySet()) { assertTrue("val set: " + key, presentInArray(ldapProps, key)); } }
private void loginWithCredentials(String userName, Set<String> rolesNames) throws LoginException { loginModule = new StubCertificateLoginModule(userName, new HashSet<>(rolesNames)); JaasCallbackHandler callbackHandler = new JaasCallbackHandler(null, null, null); loginModule.initialize(subject, callbackHandler, null, new HashMap<String, Object>()); loginModule.login(); loginModule.commit(); }
@Before public void before() throws FileNotFoundException, IOException { createUsers(); createGroups(); options = new HashMap<>(); options.put("reload", "true"); // Used to simplify reproduction of the // race condition options.put("org.apache.activemq.jaas.properties.user", USERS_FILE); options.put("org.apache.activemq.jaas.properties.role", ROLES_FILE); options.put("baseDir", temp.getRoot().getAbsolutePath()); errors = new ArrayBlockingQueue<>(processorCount()); pool = Executors.newFixedThreadPool(processorCount(), ActiveMQThreadFactory.defaultThreadFactory()); callback = new JaasCallbackHandler(USERNAME, PASSWORD, null); }
@Test public void testCommitOnFailedLogin() throws LoginException { LoginModule loginModule = new LDAPLoginModule(); JaasCallbackHandler callbackHandler = new JaasCallbackHandler(null, null, null); loginModule.initialize(new Subject(), callbackHandler, null, new HashMap<String, Object>()); // login should return false due to null username assertFalse(loginModule.login()); // since login failed commit should return false as well assertFalse(loginModule.commit()); }