List of usage examples for javax.security.auth.spi LoginModule commit
boolean commit() throws LoginException;
From source file:com.fiveamsolutions.nci.commons.authentication.LoginModuleTest.java
@Test public void testIt() throws Exception { String un = "user"; String pw = "Password1"; LoginModule module = new CommonLoginModule(); Map<String, ?> options = new HashMap<String, Object>(); Map<String, ?> sharedState = new HashMap<String, Object>(); Subject subject = new Subject(); CallbackHandler cbh = new MockCallbackHandler(true); module.initialize(subject, cbh, sharedState, options); try {/*from www . j a v a2 s .c o m*/ module.login(); fail(); } catch (LoginException e) { // expected } assertTrue(sharedState.isEmpty()); assertTrue(module.abort()); cbh = new MockCallbackHandler(false); module.initialize(subject, cbh, sharedState, options); try { module.login(); fail(); } catch (LoginException e) { // expected } assertTrue(sharedState.isEmpty()); assertTrue(module.abort()); cbh = new MockCallbackHandler(un, "pass".toCharArray()); module.initialize(subject, cbh, sharedState, options); try { module.login(); fail(); } catch (FailedLoginException e) { // expected } assertTrue(sharedState.isEmpty()); assertTrue(module.abort()); createUser(un, pw); try { module.login(); fail(); } catch (FailedLoginException e) { // expected } assertTrue(sharedState.isEmpty()); assertTrue(module.abort()); cbh = new MockCallbackHandler(un.toUpperCase(), pw.toCharArray()); module.initialize(subject, cbh, sharedState, options); assertTrue(module.login()); assertTrue(!sharedState.isEmpty()); assertEquals(un, sharedState.get("javax.security.auth.login.name")); assertEquals(pw, new String((char[]) sharedState.get("javax.security.auth.login.password"))); assertTrue(subject.getPrincipals().isEmpty()); assertTrue(module.commit()); assertTrue(!subject.getPrincipals().isEmpty()); assertEquals(un, subject.getPrincipals().iterator().next().getName()); assertTrue(module.logout()); assertTrue(subject.getPrincipals().isEmpty()); }
From source file:org.apache.wiki.auth.AuthenticationManager.java
/** * Instantiates and executes a single JAAS * {@link javax.security.auth.spi.LoginModule}, and returns a Set of * Principals that results from a successful login. The LoginModule is instantiated, * then its {@link javax.security.auth.spi.LoginModule#initialize(Subject, CallbackHandler, Map, Map)} * method is called. The parameters passed to <code>initialize</code> is a * dummy Subject, an empty shared-state Map, and an options Map the caller supplies. * /*from w w w . j a v a2s .c o m*/ * @param clazz * the LoginModule class to instantiate * @param handler * the callback handler to supply to the LoginModule * @param options * a Map of key/value strings for initializing the LoginModule * @return the set of Principals returned by the JAAS method {@link Subject#getPrincipals()} * @throws WikiSecurityException * if the LoginModule could not be instantiated for any reason */ protected Set<Principal> doJAASLogin(Class<? extends LoginModule> clazz, CallbackHandler handler, Map<String, String> options) throws WikiSecurityException { // Instantiate the login module LoginModule loginModule = null; try { loginModule = clazz.newInstance(); } catch (InstantiationException e) { throw new WikiSecurityException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new WikiSecurityException(e.getMessage(), e); } // Initialize the LoginModule Subject subject = new Subject(); loginModule.initialize(subject, handler, EMPTY_MAP, options); // Try to log in: boolean loginSucceeded = false; boolean commitSucceeded = false; try { loginSucceeded = loginModule.login(); if (loginSucceeded) { commitSucceeded = loginModule.commit(); } } catch (LoginException e) { // Login or commit failed! No principal for you! } // If we successfully logged in & committed, return all the principals if (loginSucceeded && commitSucceeded) { return subject.getPrincipals(); } return NO_PRINCIPALS; }