Search in sources :

Example 6 with WikiPrincipal

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

the class WorkflowManagerTest method setUp.

@Before
public void setUp() throws Exception {
    Properties props = TestEngine.getTestProperties();
    m_engine = new TestEngine(props);
    wm = m_engine.getWorkflowManager();
    // Create a workflow with 3 steps, with a Decision in the middle
    w = new Workflow("workflow.key", new WikiPrincipal("Owner1"));
    w.setWorkflowManager(m_engine.getWorkflowManager());
    Step startTask = new TaskTest.NormalTask(w);
    Step endTask = new TaskTest.NormalTask(w);
    Decision decision = new SimpleDecision(w, "decision.editWikiApproval", new WikiPrincipal("Actor1"));
    startTask.addSuccessor(Outcome.STEP_COMPLETE, decision);
    decision.addSuccessor(Outcome.DECISION_APPROVE, endTask);
    w.setFirstStep(startTask);
    // Add a message argument to the workflow with the page name
    w.addMessageArgument("MyPage");
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) TestEngine(org.apache.wiki.TestEngine) Properties(java.util.Properties) Before(org.junit.Before)

Example 7 with WikiPrincipal

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

the class WikiSessionTest method testUserPrincipal.

@Test
public void testUserPrincipal() throws ServletException, IOException {
    MockHttpServletRequest request;
    WikiSession wikiSession;
    // Changing the UserPrincipal value should cause the user to be authenticated...
    request = m_engine.newHttpRequest();
    request.setUserPrincipal(new WikiPrincipal("Fred Flintstone"));
    runSecurityFilter(m_engine, request);
    wikiSession = WikiSession.getWikiSession(m_engine, request);
    Assert.assertTrue(wikiSession.isAuthenticated());
    Assert.assertEquals("Fred Flintstone", wikiSession.getUserPrincipal().getName());
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Test(org.junit.Test)

Example 8 with WikiPrincipal

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

the class Installer method createAdministrator.

/**
 * Creates an administrative user and returns the new password.
 * If the admin user exists, the password will be <code>null</code>.
 * @return the password
 * @throws WikiSecurityException
 */
public String createAdministrator() throws WikiSecurityException {
    if (!m_validated) {
        throw new WikiSecurityException("Cannot create administrator because one or more of the installation settings are invalid.");
    }
    if (adminExists()) {
        return null;
    }
    // See if the admin user exists already
    UserManager userMgr = m_engine.getUserManager();
    UserDatabase userDb = userMgr.getUserDatabase();
    String password = null;
    try {
        userDb.findByLoginName(ADMIN_ID);
    } catch (NoSuchPrincipalException e) {
        // Create a random 12-character password
        password = TextUtil.generateRandomPassword();
        UserProfile profile = userDb.newProfile();
        profile.setLoginName(ADMIN_ID);
        profile.setFullname(ADMIN_NAME);
        profile.setPassword(password);
        userDb.save(profile);
    }
    // Create a new admin group
    GroupManager groupMgr = m_engine.getGroupManager();
    Group group = null;
    try {
        group = groupMgr.getGroup(ADMIN_GROUP);
        group.add(new WikiPrincipal(ADMIN_NAME));
    } catch (NoSuchPrincipalException e) {
        group = groupMgr.parseGroup(ADMIN_GROUP, ADMIN_NAME, true);
    }
    groupMgr.setGroup(m_session, group);
    return password;
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) Group(org.apache.wiki.auth.authorize.Group) UserProfile(org.apache.wiki.auth.user.UserProfile) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) UserManager(org.apache.wiki.auth.UserManager) UserDatabase(org.apache.wiki.auth.user.UserDatabase) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) GroupManager(org.apache.wiki.auth.authorize.GroupManager)

Example 9 with WikiPrincipal

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

the class GroupManager method parseGroup.

/**
 * <p>
 * Extracts group name and members from passed parameters and populates an
 * existing Group with them. The Group will either be a copy of an existing
 * Group (if one can be found), or a new, unregistered Group (if not).
 * Optionally, this method can throw a WikiSecurityException if the Group
 * does not yet exist in the GroupManager cache.
 * </p>
 * <p>
 * The <code>group</code> parameter in the HTTP request contains the Group
 * name to look up and populate. The <code>members</code> parameter
 * contains the member list. If these differ from those in the existing
 * group, the passed values override the old values.
 * </p>
 * <p>
 * This method does not commit the new Group to the GroupManager cache. To
 * do that, use {@link #setGroup(WikiSession, Group)}.
 * </p>
 * @param name the name of the group to construct
 * @param memberLine the line of text containing the group membership list
 * @param create whether this method should create a new, empty Group if one
 *            with the requested name is not found. If <code>false</code>,
 *            groups that do not exist will cause a
 *            <code>NoSuchPrincipalException</code> to be thrown
 * @return a new, populated group
 * @see org.apache.wiki.auth.authorize.Group#RESTRICTED_GROUPNAMES
 * @throws WikiSecurityException if the group name isn't allowed, or if
 * <code>create</code> is <code>false</code>
 * and the Group named <code>name</code> does not exist
 */
public Group parseGroup(String name, String memberLine, boolean create) throws WikiSecurityException {
    // If null name parameter, it's because someone's creating a new group
    if (name == null) {
        if (create) {
            name = "MyGroup";
        } else {
            throw new WikiSecurityException("Group name cannot be blank.");
        }
    } else if (ArrayUtils.contains(Group.RESTRICTED_GROUPNAMES, name)) {
        // Certain names are forbidden
        throw new WikiSecurityException("Illegal group name: " + name);
    }
    name = name.trim();
    // Normalize the member line
    if (InputValidator.isBlank(memberLine)) {
        memberLine = "";
    }
    memberLine = memberLine.trim();
    // Create or retrieve the group (may have been previously cached)
    Group group = new Group(name, m_engine.getApplicationName());
    try {
        Group existingGroup = getGroup(name);
        // If existing, clone it
        group.setCreator(existingGroup.getCreator());
        group.setCreated(existingGroup.getCreated());
        group.setModifier(existingGroup.getModifier());
        group.setLastModified(existingGroup.getLastModified());
        for (Principal existingMember : existingGroup.members()) {
            group.add(existingMember);
        }
    } catch (NoSuchPrincipalException e) {
        // It's a new group.... throw error if we don't create new ones
        if (!create) {
            throw new NoSuchPrincipalException("Group '" + name + "' does not exist.");
        }
    }
    // If passed members not empty, overwrite
    String[] members = extractMembers(memberLine);
    if (members.length > 0) {
        group.clear();
        for (String member : members) {
            group.add(new WikiPrincipal(member));
        }
    }
    return group;
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

Example 10 with WikiPrincipal

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

the class AbstractUserDatabase method getPrincipals.

/**
 * <p>Looks up the Principals representing a user from the user database. These
 * are defined as a set of WikiPrincipals manufactured from the login name,
 * full name, and wiki name. If the user database does not contain a user
 * with the supplied identifier, throws a {@link NoSuchPrincipalException}.</p>
 * <p>When this method creates WikiPrincipals, the Principal containing
 * the user's full name is marked as containing the common name (see
 * {@link org.apache.wiki.auth.WikiPrincipal#WikiPrincipal(String, String)}).
 * @param identifier the name of the principal to retrieve; this corresponds to
 *            value returned by the user profile's
 *            {@link UserProfile#getLoginName()}method.
 * @return the array of Principals representing the user
 * @see org.apache.wiki.auth.user.UserDatabase#getPrincipals(java.lang.String)
 * @throws NoSuchPrincipalException {@inheritDoc}
 */
public Principal[] getPrincipals(String identifier) throws NoSuchPrincipalException {
    try {
        UserProfile profile = findByLoginName(identifier);
        ArrayList<Principal> principals = new ArrayList<Principal>();
        if (profile.getLoginName() != null && profile.getLoginName().length() > 0) {
            principals.add(new WikiPrincipal(profile.getLoginName(), WikiPrincipal.LOGIN_NAME));
        }
        if (profile.getFullname() != null && profile.getFullname().length() > 0) {
            principals.add(new WikiPrincipal(profile.getFullname(), WikiPrincipal.FULL_NAME));
        }
        if (profile.getWikiName() != null && profile.getWikiName().length() > 0) {
            principals.add(new WikiPrincipal(profile.getWikiName(), WikiPrincipal.WIKI_NAME));
        }
        return principals.toArray(new Principal[principals.size()]);
    } catch (NoSuchPrincipalException e) {
        throw e;
    }
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) ArrayList(java.util.ArrayList) NoSuchPrincipalException(org.apache.wiki.auth.NoSuchPrincipalException) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal)

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