Search in sources :

Example 11 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.

the class JDBCUserDatabase method rename.

/**
 * @see org.apache.wiki.auth.user.UserDatabase#rename(String, String)
 */
public void rename(String loginName, String newName) throws NoSuchPrincipalException, DuplicateUserException, WikiSecurityException {
    // Get the existing user; if not found, throws NoSuchPrincipalException
    UserProfile profile = findByLoginName(loginName);
    // Get user with the proposed name; if found, it's a collision
    try {
        UserProfile otherProfile = findByLoginName(newName);
        if (otherProfile != null) {
            throw new DuplicateUserException("security.error.cannot.rename", newName);
        }
    } catch (NoSuchPrincipalException e) {
    // Good! That means it's safe to save using the new name
    }
    Connection conn = null;
    try {
        // Open the database connection
        conn = m_ds.getConnection();
        if (m_supportsCommits) {
            conn.setAutoCommit(false);
        }
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        Date modDate = new Date(ts.getTime());
        // Change the login ID for the user record
        PreparedStatement ps = conn.prepareStatement(m_renameProfile);
        ps.setString(1, newName);
        ps.setTimestamp(2, ts);
        ps.setString(3, loginName);
        ps.execute();
        ps.close();
        // Change the login ID for the role records
        ps = conn.prepareStatement(m_renameRoles);
        ps.setString(1, newName);
        ps.setString(2, loginName);
        ps.execute();
        ps.close();
        // Set the profile name and mod time
        profile.setLoginName(newName);
        profile.setLastModified(modDate);
        // Commit and close connection
        if (m_supportsCommits) {
            conn.commit();
        }
    } catch (SQLException e) {
        throw new WikiSecurityException(e.getMessage(), e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) IOException(java.io.IOException)

Example 12 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.

the class JDBCUserDatabase method findByPreparedStatement.

/**
 * Private method that returns the first {@link UserProfile} matching a
 * named column's value. This method will also set the UID if it has not yet been set.
 * @param sql the SQL statement that should be prepared; it must have one parameter
 * to set (either a String or a Long)
 * @param index the value to match
 * @return the resolved UserProfile
 * @throws SQLException
 */
private UserProfile findByPreparedStatement(String sql, Object index) throws NoSuchPrincipalException {
    UserProfile profile = null;
    boolean found = false;
    boolean unique = true;
    Connection conn = null;
    try {
        // Open the database connection
        conn = m_ds.getConnection();
        if (m_supportsCommits) {
            conn.setAutoCommit(false);
        }
        PreparedStatement ps = conn.prepareStatement(sql);
        // Set the parameter to search by
        if (index instanceof String) {
            ps.setString(1, (String) index);
        } else if (index instanceof Long) {
            ps.setLong(1, ((Long) index).longValue());
        } else {
            throw new IllegalArgumentException("Index type not recognized!");
        }
        // Go and get the record!
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            if (profile != null) {
                unique = false;
                break;
            }
            profile = newProfile();
            // Fetch the basic user attributes
            profile.setUid(rs.getString(m_uid));
            if (profile.getUid() == null) {
                profile.setUid(generateUid(this));
            }
            profile.setCreated(rs.getTimestamp(m_created));
            profile.setEmail(rs.getString(m_email));
            profile.setFullname(rs.getString(m_fullName));
            profile.setLastModified(rs.getTimestamp(m_modified));
            Date lockExpiry = rs.getDate(m_lockExpiry);
            profile.setLockExpiry(rs.wasNull() ? null : lockExpiry);
            profile.setLoginName(rs.getString(m_loginName));
            profile.setPassword(rs.getString(m_password));
            // Fetch the user attributes
            String rawAttributes = rs.getString(m_attributes);
            if (rawAttributes != null) {
                try {
                    Map<String, ? extends Serializable> attributes = Serializer.deserializeFromBase64(rawAttributes);
                    profile.getAttributes().putAll(attributes);
                } catch (IOException e) {
                    log.error("Could not parse user profile attributes!", e);
                }
            }
            found = true;
        }
        ps.close();
    } catch (SQLException e) {
        throw new NoSuchPrincipalException(e.getMessage());
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
    if (!found) {
        throw new NoSuchPrincipalException("Could not find profile in database!");
    }
    if (!unique) {
        throw new NoSuchPrincipalException("More than one profile in database!");
    }
    return profile;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) IOException(java.io.IOException) Date(java.util.Date) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) IOException(java.io.IOException) ResultSet(java.sql.ResultSet)

Example 13 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.

the class XMLUserDatabase method rename.

/**
 * @see org.apache.wiki.auth.user.UserDatabase#rename(String, String)
 */
public synchronized void rename(String loginName, String newName) throws NoSuchPrincipalException, DuplicateUserException, WikiSecurityException {
    if (c_dom == null) {
        log.fatal("Could not rename profile '" + loginName + "'; database does not exist");
        throw new IllegalStateException("FATAL: database does not exist");
    }
    checkForRefresh();
    // Get the existing user; if not found, throws NoSuchPrincipalException
    UserProfile profile = findByLoginName(loginName);
    // Get user with the proposed name; if found, it's a collision
    try {
        UserProfile otherProfile = findByLoginName(newName);
        if (otherProfile != null) {
            throw new DuplicateUserException("security.error.cannot.rename", newName);
        }
    } catch (NoSuchPrincipalException e) {
    // Good! That means it's safe to save using the new name
    }
    // Find the user with the old login id attribute, and change it
    NodeList users = c_dom.getElementsByTagName(USER_TAG);
    for (int i = 0; i < users.getLength(); i++) {
        Element user = (Element) users.item(i);
        if (user.getAttribute(LOGIN_NAME).equals(loginName)) {
            DateFormat c_format = new SimpleDateFormat(DATE_FORMAT);
            Date modDate = new Date(System.currentTimeMillis());
            setAttribute(user, LOGIN_NAME, newName);
            setAttribute(user, LAST_MODIFIED, c_format.format(modDate));
            profile.setLoginName(newName);
            profile.setLastModified(modDate);
            break;
        }
    }
    // Commit to disk
    saveDOM();
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 14 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException 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 15 with NoSuchPrincipalException

use of org.apache.wiki.auth.NoSuchPrincipalException 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

NoSuchPrincipalException (org.apache.wiki.auth.NoSuchPrincipalException)16 WikiSecurityException (org.apache.wiki.auth.WikiSecurityException)9 IOException (java.io.IOException)5 Date (java.util.Date)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 NamingException (javax.naming.NamingException)4 NoRequiredPropertyException (org.apache.wiki.api.exceptions.NoRequiredPropertyException)4 UserManager (org.apache.wiki.auth.UserManager)4 WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)4 UserDatabase (org.apache.wiki.auth.user.UserDatabase)4 UserProfile (org.apache.wiki.auth.user.UserProfile)3 Principal (java.security.Principal)2 ResultSet (java.sql.ResultSet)2 Timestamp (java.sql.Timestamp)2 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)2 Element (org.w3c.dom.Element)2 NodeList (org.w3c.dom.NodeList)2 DateFormat (java.text.DateFormat)1