Search in sources :

Example 11 with WikiPrincipal

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

the class XMLUserDatabase method getWikiNames.

/**
 * Returns all WikiNames that are stored in the UserDatabase
 * as an array of WikiPrincipal objects. If the database does not
 * contain any profiles, this method will return a zero-length
 * array.
 * @return the WikiNames
 * @throws WikiSecurityException In case things fail.
 */
public Principal[] getWikiNames() throws WikiSecurityException {
    if (c_dom == null) {
        throw new IllegalStateException("FATAL: database does not exist");
    }
    SortedSet<Principal> principals = new TreeSet<Principal>();
    NodeList users = c_dom.getElementsByTagName(USER_TAG);
    for (int i = 0; i < users.getLength(); i++) {
        Element user = (Element) users.item(i);
        String wikiName = user.getAttribute(WIKI_NAME);
        if (wikiName == null) {
            log.warn("Detected null wiki name in XMLUserDataBase. Check your user database.");
        } else {
            Principal principal = new WikiPrincipal(wikiName, WikiPrincipal.WIKI_NAME);
            principals.add(principal);
        }
    }
    return principals.toArray(new Principal[principals.size()]);
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) TreeSet(java.util.TreeSet) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 12 with WikiPrincipal

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

the class WebContainerLoginModule method login.

/**
 * Logs in the user.
 * @see javax.security.auth.spi.LoginModule#login()
 *
 * @return {@inheritDoc}
 * @throws {@inheritDoc}
 */
public boolean login() throws LoginException {
    HttpRequestCallback rcb = new HttpRequestCallback();
    Callback[] callbacks = new Callback[] { rcb };
    String userId = null;
    try {
        // First, try to extract a Principal object out of the request
        // directly. If we find one, we're done.
        m_handler.handle(callbacks);
        HttpServletRequest request = rcb.getRequest();
        if (request == null) {
            throw new LoginException("No Http request supplied.");
        }
        HttpSession session = request.getSession(false);
        String sid = (session == null) ? NULL : session.getId();
        Principal principal = request.getUserPrincipal();
        if (principal == null) {
            // If no Principal in request, try the remoteUser
            if (log.isDebugEnabled()) {
                log.debug("No userPrincipal found for session ID=" + sid);
            }
            userId = request.getRemoteUser();
            if (userId == null) {
                if (log.isDebugEnabled()) {
                    log.debug("No remoteUser found for session ID=" + sid);
                }
                throw new FailedLoginException("No remote user found");
            }
            principal = new WikiPrincipal(userId, WikiPrincipal.LOGIN_NAME);
        }
        if (log.isDebugEnabled()) {
            log.debug("Logged in container principal " + principal.getName() + ".");
        }
        m_principals.add(principal);
        return true;
    } catch (IOException e) {
        log.error("IOException: " + e.getMessage());
        return false;
    } catch (UnsupportedCallbackException e) {
        log.error("UnsupportedCallbackException: " + e.getMessage());
        return false;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(javax.security.auth.callback.Callback) FailedLoginException(javax.security.auth.login.FailedLoginException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) HttpSession(javax.servlet.http.HttpSession) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 13 with WikiPrincipal

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

the class AclImplTest method testAlice.

@Test
public void testAlice() {
    // Alice should be able to view but not edit or comment
    Principal wup = new WikiPrincipal("Alice");
    Assert.assertTrue("view", inArray(m_acl.findPrincipals(PagePermission.VIEW), wup));
    Assert.assertFalse("edit", inArray(m_acl.findPrincipals(PagePermission.EDIT), wup));
    Assert.assertFalse("comment", inArray(m_acl.findPrincipals(PagePermission.COMMENT), wup));
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 14 with WikiPrincipal

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

the class AclImplTest method testDave.

@Test
public void testDave() {
    // Dave should be able to view and comment but not edit or delete
    Principal wup = new WikiPrincipal("Dave");
    Assert.assertTrue("view", inArray(m_acl.findPrincipals(PagePermission.VIEW), wup));
    Assert.assertFalse("edit", inArray(m_acl.findPrincipals(PagePermission.EDIT), wup));
    Assert.assertTrue("comment", inArray(m_acl.findPrincipals(PagePermission.COMMENT), wup));
    Assert.assertFalse("delete", inArray(m_acl.findPrincipals(PagePermission.DELETE), wup));
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 15 with WikiPrincipal

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

the class AclImplTest method testGroups.

@Test
public void testGroups() {
    Principal wup = new WikiPrincipal("Alice");
    Assert.assertTrue("Alice view", inGroup(m_aclGroup.findPrincipals(PagePermission.VIEW), wup));
    Assert.assertTrue("Alice edit", inGroup(m_aclGroup.findPrincipals(PagePermission.EDIT), wup));
    Assert.assertTrue("Alice comment", inGroup(m_aclGroup.findPrincipals(PagePermission.COMMENT), wup));
    Assert.assertFalse("Alice delete", inGroup(m_aclGroup.findPrincipals(PagePermission.DELETE), wup));
    wup = new WikiPrincipal("Bob");
    Assert.assertTrue("Bob view", inGroup(m_aclGroup.findPrincipals(PagePermission.VIEW), wup));
    Assert.assertTrue("Bob edit", inGroup(m_aclGroup.findPrincipals(PagePermission.EDIT), wup));
    Assert.assertTrue("Bob comment", inGroup(m_aclGroup.findPrincipals(PagePermission.COMMENT), wup));
    Assert.assertFalse("Bob delete", inGroup(m_aclGroup.findPrincipals(PagePermission.DELETE), wup));
    wup = new WikiPrincipal("Charlie");
    Assert.assertTrue("Charlie view", inGroup(m_aclGroup.findPrincipals(PagePermission.VIEW), wup));
    Assert.assertFalse("Charlie edit", inGroup(m_aclGroup.findPrincipals(PagePermission.EDIT), wup));
    Assert.assertFalse("Charlie comment", inGroup(m_aclGroup.findPrincipals(PagePermission.COMMENT), wup));
    Assert.assertFalse("Charlie delete", inGroup(m_aclGroup.findPrincipals(PagePermission.DELETE), wup));
    wup = new WikiPrincipal("Dave");
    Assert.assertFalse("Dave view", inGroup(m_aclGroup.findPrincipals(PagePermission.VIEW), wup));
    Assert.assertFalse("Dave edit", inGroup(m_aclGroup.findPrincipals(PagePermission.EDIT), wup));
    Assert.assertFalse("Dave comment", inGroup(m_aclGroup.findPrincipals(PagePermission.COMMENT), wup));
    Assert.assertFalse("Dave delete", inGroup(m_aclGroup.findPrincipals(PagePermission.DELETE), wup));
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

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