Search in sources :

Example 6 with UserDatabase

use of org.apache.wiki.auth.user.UserDatabase in project jspwiki by apache.

the class UserDatabaseLoginModule method login.

/**
 * @see javax.security.auth.spi.LoginModule#login()
 *
 * {@inheritDoc}
 */
public boolean login() throws LoginException {
    UserDatabaseCallback ucb = new UserDatabaseCallback();
    NameCallback ncb = new NameCallback("User name");
    PasswordCallback pcb = new PasswordCallback("Password", false);
    Callback[] callbacks = new Callback[] { ucb, ncb, pcb };
    try {
        m_handler.handle(callbacks);
        UserDatabase db = ucb.getUserDatabase();
        String username = ncb.getName();
        String password = new String(pcb.getPassword());
        // Look up the user and compare the password hash
        if (db == null) {
            throw new FailedLoginException("No user database: check the callback handler code!");
        }
        UserProfile profile = db.findByLoginName(username);
        String storedPassword = profile.getPassword();
        if (storedPassword != null && db.validatePassword(username, password)) {
            if (log.isDebugEnabled()) {
                log.debug("Logged in user database user " + username);
            }
            // If login succeeds, commit these principals/roles
            m_principals.add(new WikiPrincipal(username, WikiPrincipal.LOGIN_NAME));
            return true;
        }
        throw new FailedLoginException("The username or password is incorrect.");
    } catch (IOException e) {
        String message = "IO exception; disallowing login.";
        log.error(message, e);
        throw new LoginException(message);
    } catch (UnsupportedCallbackException e) {
        String message = "Unable to handle callback; disallowing login.";
        log.error(message, e);
        throw new LoginException(message);
    } catch (NoSuchPrincipalException e) {
        throw new FailedLoginException("The username or password is incorrect.");
    }
}
Also used : UserProfile(org.apache.wiki.auth.user.UserProfile) UserDatabase(org.apache.wiki.auth.user.UserDatabase) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) IOException(java.io.IOException) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) FailedLoginException(javax.security.auth.login.FailedLoginException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 7 with UserDatabase

use of org.apache.wiki.auth.user.UserDatabase in project jspwiki by apache.

the class Installer method adminExists.

/**
 * Returns <code>true</code> if the administrative user had
 * been created previously.
 * @return the result
 */
public boolean adminExists() {
    // See if the admin user exists already
    UserManager userMgr = m_engine.getUserManager();
    UserDatabase userDb = userMgr.getUserDatabase();
    try {
        userDb.findByLoginName(ADMIN_ID);
        return true;
    } catch (NoSuchPrincipalException e) {
        return false;
    }
}
Also used : UserManager(org.apache.wiki.auth.UserManager) UserDatabase(org.apache.wiki.auth.user.UserDatabase) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException)

Aggregations

UserDatabase (org.apache.wiki.auth.user.UserDatabase)7 UserProfile (org.apache.wiki.auth.user.UserProfile)5 NoSuchPrincipalException (org.apache.wiki.auth.NoSuchPrincipalException)4 Principal (java.security.Principal)3 UserManager (org.apache.wiki.auth.UserManager)3 WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)2 IOException (java.io.IOException)1 Callback (javax.security.auth.callback.Callback)1 NameCallback (javax.security.auth.callback.NameCallback)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 LoginException (javax.security.auth.login.LoginException)1 WikiSecurityException (org.apache.wiki.auth.WikiSecurityException)1 UnresolvedPrincipal (org.apache.wiki.auth.acl.UnresolvedPrincipal)1 Group (org.apache.wiki.auth.authorize.Group)1 GroupManager (org.apache.wiki.auth.authorize.GroupManager)1 Role (org.apache.wiki.auth.authorize.Role)1 AllPermission (org.apache.wiki.auth.permissions.AllPermission)1