Search in sources :

Example 51 with WikiPrincipal

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

the class GroupPermissionTest method testImpliesMember.

public final void testImpliesMember() {
    GroupPermission p1;
    Permission p2;
    Subject s;
    // <groupmember> implies TestGroup if Subject has GroupPermission("TestGroup")
    p1 = new GroupPermission("*:<groupmember>", "view");
    p2 = new GroupPermission("*:TestGroup", "view");
    s = new Subject();
    s.getPrincipals().add(new GroupPrincipal("TestGroup"));
    Assert.assertTrue(subjectImplies(s, p1, p2));
    // <groupmember> doesn't imply it if Subject has no GroupPermission("TestGroup")
    s = new Subject();
    s.getPrincipals().add(new WikiPrincipal("TestGroup"));
    Assert.assertFalse(subjectImplies(s, p1, p2));
    // <groupmember> doesn't imply it if Subject's GP doesn't match
    s = new Subject();
    s.getPrincipals().add(new GroupPrincipal("FooGroup"));
    Assert.assertFalse(subjectImplies(s, p1, p2));
    // <groupmember> doesn't imply it if p2 isn't GroupPermission type
    p2 = new PagePermission("*:TestGroup", "view");
    s = new Subject();
    s.getPrincipals().add(new GroupPrincipal("TestGroup"));
    Assert.assertFalse(subjectImplies(s, p1, p2));
    // <groupmember> implies TestGroup if not called with Subject combiner
    p1 = new GroupPermission("*:<groupmember>", "view");
    p2 = new GroupPermission("*:TestGroup", "view");
    Assert.assertFalse(p1.impliesMember(p2));
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) Permission(java.security.Permission) Subject(javax.security.auth.Subject)

Example 52 with WikiPrincipal

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

the class JDBCUserDatabase 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
 */
public Principal[] getWikiNames() throws WikiSecurityException {
    Set<Principal> principals = new HashSet<Principal>();
    Connection conn = null;
    try {
        conn = m_ds.getConnection();
        PreparedStatement ps = conn.prepareStatement(m_findAll);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String wikiName = rs.getString(m_wikiName);
            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);
            }
        }
        ps.close();
    } catch (SQLException e) {
        throw new WikiSecurityException(e.getMessage(), e);
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }
    return principals.toArray(new Principal[principals.size()]);
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal) 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) HashSet(java.util.HashSet)

Example 53 with WikiPrincipal

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

the class XMLGroupDatabase method buildGroup.

/**
 * Constructs a Group based on a DOM group node.
 * @param groupNode the node in the DOM containing the node
 * @param name the name of the group
 * @throws NoSuchPrincipalException
 * @throws WikiSecurityException
 */
private Group buildGroup(Element groupNode, String name) {
    // It's an error if either param is null (very odd)
    if (groupNode == null || name == null) {
        throw new IllegalArgumentException("DOM element or name cannot be null.");
    }
    // Construct a new group
    Group group = new Group(name, m_engine.getApplicationName());
    // Get the users for this group, and add them
    NodeList members = groupNode.getElementsByTagName(MEMBER_TAG);
    for (int i = 0; i < members.getLength(); i++) {
        Element memberNode = (Element) members.item(i);
        String principalName = memberNode.getAttribute(PRINCIPAL);
        Principal member = new WikiPrincipal(principalName);
        group.add(member);
    }
    // Add the created/last-modified info
    String creator = groupNode.getAttribute(CREATOR);
    String created = groupNode.getAttribute(CREATED);
    String modifier = groupNode.getAttribute(MODIFIER);
    String modified = groupNode.getAttribute(LAST_MODIFIED);
    try {
        group.setCreated(m_format.parse(created));
        group.setLastModified(m_format.parse(modified));
    } catch (ParseException e) {
        // If parsing failed, use the platform default
        try {
            group.setCreated(m_defaultFormat.parse(created));
            group.setLastModified(m_defaultFormat.parse(modified));
        } catch (ParseException e2) {
            log.warn("Could not parse 'created' or 'lastModified' " + "attribute for " + " group'" + group.getName() + "'." + " It may have been tampered with.");
        }
    }
    group.setCreator(creator);
    group.setModifier(modifier);
    return group;
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ParseException(java.text.ParseException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 54 with WikiPrincipal

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

the class AnonymousLoginModule method login.

/**
 * Logs in the user by calling back to the registered CallbackHandler with an
 * HttpRequestCallback. The CallbackHandler must supply the current servlet
 * HTTP request as its response.
 * @return the result of the login; this will always be <code>true</code>.
 * @see javax.security.auth.spi.LoginModule#login()
 * @throws {@inheritDoc}
 */
public boolean login() throws LoginException {
    // Let's go and make a Principal based on the IP address
    HttpRequestCallback hcb = new HttpRequestCallback();
    Callback[] callbacks = new Callback[] { hcb };
    try {
        m_handler.handle(callbacks);
        HttpServletRequest request = hcb.getRequest();
        WikiPrincipal ipAddr = new WikiPrincipal(HttpUtil.getRemoteAddress(request));
        if (log.isDebugEnabled()) {
            HttpSession session = request.getSession(false);
            String sid = (session == null) ? NULL : session.getId();
            log.debug("Logged in session ID=" + sid + "; IP=" + ipAddr);
        }
        // If login succeeds, commit these principals/roles
        m_principals.add(ipAddr);
        return true;
    } catch (IOException e) {
        log.error("IOException: " + e.getMessage());
        return false;
    } catch (UnsupportedCallbackException e) {
        String message = "Unable to handle callback, disallowing login.";
        log.error(message, e);
        throw new LoginException(message);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(javax.security.auth.callback.Callback) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) HttpSession(javax.servlet.http.HttpSession) LoginException(javax.security.auth.login.LoginException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 55 with WikiPrincipal

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

the class CookieAssertionLoginModule method login.

/**
 * Logs in the user by calling back to the registered CallbackHandler with
 * an HttpRequestCallback. The CallbackHandler must supply the current
 * servlet HTTP request as its response.
 * @return the result of the login; if a cookie is
 * found, this method returns <code>true</code>. If not found, this
 * method throws a <code>FailedLoginException</code>.
 * @see javax.security.auth.spi.LoginModule#login()
 * @throws {@inheritDoc}
 */
public boolean login() throws LoginException {
    // Otherwise, let's go and look for the cookie!
    HttpRequestCallback hcb = new HttpRequestCallback();
    Callback[] callbacks = new Callback[] { hcb };
    try {
        m_handler.handle(callbacks);
        HttpServletRequest request = hcb.getRequest();
        HttpSession session = (request == null) ? null : request.getSession(false);
        String sid = (session == null) ? NULL : session.getId();
        String name = (request != null) ? getUserCookie(request) : null;
        if (name == null) {
            if (log.isDebugEnabled()) {
                log.debug("No cookie " + PREFS_COOKIE_NAME + " present in session ID=:  " + sid);
            }
            throw new FailedLoginException("The user cookie was not found.");
        }
        if (log.isDebugEnabled()) {
            log.debug("Logged in session ID=" + sid + "; asserted=" + name);
        }
        // If login succeeds, commit these principals/roles
        m_principals.add(new WikiPrincipal(name, WikiPrincipal.FULL_NAME));
        return true;
    } catch (IOException e) {
        log.error("IOException: " + e.getMessage());
        return false;
    } catch (UnsupportedCallbackException e) {
        String message = "Unable to handle callback, disallowing login.";
        log.error(message, e);
        throw new LoginException(message);
    }
}
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)

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