Search in sources :

Example 56 with WikiPrincipal

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

the class CookieAuthenticationLoginModule method login.

/**
 * @see javax.security.auth.spi.LoginModule#login()
 *
 * {@inheritDoc}
 */
public boolean login() throws LoginException {
    // Otherwise, let's go and look for the cookie!
    HttpRequestCallback hcb = new HttpRequestCallback();
    // UserDatabaseCallback ucb = new UserDatabaseCallback();
    WikiEngineCallback wcb = new WikiEngineCallback();
    Callback[] callbacks = new Callback[] { hcb, wcb };
    try {
        m_handler.handle(callbacks);
        HttpServletRequest request = hcb.getRequest();
        String uid = getLoginCookie(request);
        if (uid != null) {
            WikiEngine engine = wcb.getEngine();
            File cookieFile = getCookieFile(engine, uid);
            if (cookieFile != null && cookieFile.exists() && cookieFile.canRead()) {
                Reader in = null;
                try {
                    in = new BufferedReader(new InputStreamReader(new FileInputStream(cookieFile), "UTF-8"));
                    String username = FileUtil.readContents(in);
                    if (log.isDebugEnabled()) {
                        log.debug("Logged in cookie authenticated name=" + username);
                    }
                    // If login succeeds, commit these principals/roles
                    m_principals.add(new WikiPrincipal(username, WikiPrincipal.LOGIN_NAME));
                    // 
                    // Tag the file so that we know that it has been accessed recently.
                    // 
                    cookieFile.setLastModified(System.currentTimeMillis());
                    return true;
                } catch (IOException e) {
                    return false;
                } finally {
                    if (in != null)
                        in.close();
                }
            }
        }
    } 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);
    }
    return false;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(javax.security.auth.callback.Callback) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) WikiEngine(org.apache.wiki.WikiEngine)

Example 57 with WikiPrincipal

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

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

the class PageManager method actionPerformed.

/**
 * Listens for {@link org.apache.wiki.event.WikiSecurityEvent#PROFILE_NAME_CHANGED}
 * events. If a user profile's name changes, each page ACL is inspected. If an entry contains
 * a name that has changed, it is replaced with the new one. No events are emitted
 * as a consequence of this method, because the page contents are still the same; it is
 * only the representations of the names within the ACL that are changing.
 *
 * @param event The event
 */
public void actionPerformed(WikiEvent event) {
    if (!(event instanceof WikiSecurityEvent)) {
        return;
    }
    WikiSecurityEvent se = (WikiSecurityEvent) event;
    if (se.getType() == WikiSecurityEvent.PROFILE_NAME_CHANGED) {
        UserProfile[] profiles = (UserProfile[]) se.getTarget();
        Principal[] oldPrincipals = new Principal[] { new WikiPrincipal(profiles[0].getLoginName()), new WikiPrincipal(profiles[0].getFullname()), new WikiPrincipal(profiles[0].getWikiName()) };
        Principal newPrincipal = new WikiPrincipal(profiles[1].getFullname());
        // Examine each page ACL
        try {
            int pagesChanged = 0;
            Collection pages = getAllPages();
            for (Iterator it = pages.iterator(); it.hasNext(); ) {
                WikiPage page = (WikiPage) it.next();
                boolean aclChanged = changeAcl(page, oldPrincipals, newPrincipal);
                if (aclChanged) {
                    // If the Acl needed changing, change it now
                    try {
                        m_engine.getAclManager().setPermissions(page, page.getAcl());
                    } catch (WikiSecurityException e) {
                        log.error("Could not change page ACL for page " + page.getName() + ": " + e.getMessage(), e);
                    }
                    pagesChanged++;
                }
            }
            log.info("Profile name change for '" + newPrincipal.toString() + "' caused " + pagesChanged + " page ACLs to change also.");
        } catch (ProviderException e) {
            // Oooo! This is really bad...
            log.error("Could not change user name in Page ACLs because of Provider error:" + e.getMessage(), e);
        }
    }
}
Also used : UserProfile(org.apache.wiki.auth.user.UserProfile) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Iterator(java.util.Iterator) Collection(java.util.Collection) WikiSecurityEvent(org.apache.wiki.event.WikiSecurityEvent) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 59 with WikiPrincipal

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

the class GroupManager method actionPerformed.

/**
 * Listens for {@link org.apache.wiki.event.WikiSecurityEvent#PROFILE_NAME_CHANGED}
 * events. If a user profile's name changes, each group is inspected. If an entry contains
 * a name that has changed, it is replaced with the new one. No group events are emitted
 * as a consequence of this method, because the group memberships are still the same; it is
 * only the representations of the names within that are changing.
 * @param event the incoming event
 */
public void actionPerformed(WikiEvent event) {
    if (!(event instanceof WikiSecurityEvent)) {
        return;
    }
    WikiSecurityEvent se = (WikiSecurityEvent) event;
    if (se.getType() == WikiSecurityEvent.PROFILE_NAME_CHANGED) {
        WikiSession session = se.getSrc();
        UserProfile[] profiles = (UserProfile[]) se.getTarget();
        Principal[] oldPrincipals = new Principal[] { new WikiPrincipal(profiles[0].getLoginName()), new WikiPrincipal(profiles[0].getFullname()), new WikiPrincipal(profiles[0].getWikiName()) };
        Principal newPrincipal = new WikiPrincipal(profiles[1].getFullname());
        // Examine each group
        int groupsChanged = 0;
        try {
            for (Group group : m_groupDatabase.groups()) {
                boolean groupChanged = false;
                for (Principal oldPrincipal : oldPrincipals) {
                    if (group.isMember(oldPrincipal)) {
                        group.remove(oldPrincipal);
                        group.add(newPrincipal);
                        groupChanged = true;
                    }
                }
                if (groupChanged) {
                    setGroup(session, group);
                    groupsChanged++;
                }
            }
        } catch (WikiException e) {
            // Oooo! This is really bad...
            log.error("Could not change user name in Group lists because of GroupDatabase error:" + e.getMessage());
        }
        log.info("Profile name change for '" + newPrincipal.toString() + "' caused " + groupsChanged + " groups to change also.");
    }
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiException(org.apache.wiki.api.exceptions.WikiException) UserProfile(org.apache.wiki.auth.user.UserProfile) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) WikiSecurityEvent(org.apache.wiki.event.WikiSecurityEvent) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 60 with WikiPrincipal

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

the class JDBCGroupDatabase method populateGroup.

/**
 * Fills a Group with members.
 *
 * @param group the group to populate
 * @return the populated Group
 */
private Group populateGroup(Group group) {
    ResultSet rs = null;
    PreparedStatement ps = null;
    Connection conn = null;
    try {
        // Open the database connection
        conn = m_ds.getConnection();
        ps = conn.prepareStatement(m_findMembers);
        ps.setString(1, group.getName());
        rs = ps.executeQuery();
        while (rs.next()) {
            String memberName = rs.getString(m_member);
            if (memberName != null) {
                WikiPrincipal principal = new WikiPrincipal(memberName, WikiPrincipal.UNSPECIFIED);
                group.add(principal);
            }
        }
    } catch (SQLException e) {
    // I guess that means there aren't any principals...
    } finally {
        closeQuietly(conn, ps, rs);
    }
    return group;
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal)

Aggregations

WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)60 Principal (java.security.Principal)41 Test (org.junit.Test)32 LoginException (javax.security.auth.login.LoginException)13 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)13 CallbackHandler (javax.security.auth.callback.CallbackHandler)8 LoginModule (javax.security.auth.spi.LoginModule)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)6 IOException (java.io.IOException)5 Callback (javax.security.auth.callback.Callback)5 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)5 WikiSessionTest (org.apache.wiki.WikiSessionTest)5 NoSuchPrincipalException (org.apache.wiki.auth.NoSuchPrincipalException)5 WikiSecurityException (org.apache.wiki.auth.WikiSecurityException)4 UserProfile (org.apache.wiki.auth.user.UserProfile)4 Before (org.junit.Before)4 Properties (java.util.Properties)3 Subject (javax.security.auth.Subject)3 FailedLoginException (javax.security.auth.login.FailedLoginException)3