Search in sources :

Example 46 with Session

use of org.apache.wiki.api.core.Session in project jspwiki by apache.

the class WikiSessionTest method testAssertionCookie.

@Test
public void testAssertionCookie() throws ServletException, IOException {
    final MockHttpServletRequest request;
    final Session wikiSession;
    // Adding the magic "assertion cookie" should  set asserted status.
    request = m_engine.newHttpRequest();
    request.setUserPrincipal(null);
    final String cookieName = CookieAssertionLoginModule.PREFS_COOKIE_NAME;
    request.setCookies(new Cookie[] { new Cookie(cookieName, "FredFlintstone") });
    runSecurityFilter(m_engine, request);
    wikiSession = Wiki.session().find(m_engine, request);
    Assertions.assertTrue(wikiSession.isAsserted());
    Assertions.assertEquals("FredFlintstone", wikiSession.getUserPrincipal().getName());
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Session(org.apache.wiki.api.core.Session) Test(org.junit.jupiter.api.Test)

Example 47 with Session

use of org.apache.wiki.api.core.Session in project jspwiki by apache.

the class WikiSessionTest method containerAuthenticatedSession.

public static Session containerAuthenticatedSession(final TestEngine engine, final String id, final Principal[] roles) throws Exception {
    // Build container session
    final MockHttpServletRequest request = engine.newHttpRequest();
    final Set<String> r = new HashSet<>();
    for (final Principal role : roles) {
        r.add(role.getName());
    }
    request.setRoles(r);
    request.setUserPrincipal(new WikiPrincipal(id));
    // Log in
    runSecurityFilter(engine, request);
    // Make sure the user is actually authenticated
    final Session session = Wiki.session().find(engine, request);
    if (!session.isAuthenticated()) {
        throw new IllegalStateException("Could not log in authenticated user '" + id + "'");
    }
    return session;
}
Also used : WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) Principal(java.security.Principal) HashSet(java.util.HashSet) Session(org.apache.wiki.api.core.Session)

Example 48 with Session

use of org.apache.wiki.api.core.Session in project jspwiki by apache.

the class WikiSessionTest method anonymousSession.

/**
 * Creates an anonymous user session.
 * @param engine the wiki engine
 * @return the new session
 * @throws Exception session not anonymous.
 */
public static Session anonymousSession(final TestEngine engine) throws Exception {
    // Build anon session
    final MockHttpServletRequest request = engine.newHttpRequest();
    // Log in
    runSecurityFilter(engine, request);
    // Make sure the user is actually anonymous
    final Session session = Wiki.session().find(engine, request);
    if (!session.isAnonymous()) {
        throw new IllegalStateException("Session is not anonymous.");
    }
    return session;
}
Also used : MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) Session(org.apache.wiki.api.core.Session)

Example 49 with Session

use of org.apache.wiki.api.core.Session in project jspwiki by apache.

the class IfPluginTest method getJanneBasedWikiContextFor.

/**
 * Returns a {@link WikiContext} for the given page, with user {@link Users#JANNE} logged in.
 *
 * @param page given {@link Page}.
 * @return {@link WikiContext} associated to given {@link Page}.
 * @throws WikiException problems while logging in.
 */
Context getJanneBasedWikiContextFor(final Page page) throws WikiException {
    final MockHttpServletRequest request = testEngine.newHttpRequest();
    final Session session = WikiSession.getWikiSession(testEngine, request);
    testEngine.getManager(AuthenticationManager.class).login(session, request, Users.JANNE, Users.JANNE_PASS);
    return Wiki.context().create(testEngine, request, page);
}
Also used : AuthenticationManager(org.apache.wiki.auth.AuthenticationManager) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) WikiSession(org.apache.wiki.WikiSession) Session(org.apache.wiki.api.core.Session)

Example 50 with Session

use of org.apache.wiki.api.core.Session in project jspwiki by apache.

the class DefaultRSSGenerator method generateFullWikiRSS.

/**
 * {@inheritDoc}
 */
@Override
public String generateFullWikiRSS(final Context wikiContext, final Feed feed) {
    feed.setChannelTitle(m_engine.getApplicationName());
    feed.setFeedURL(m_engine.getBaseURL());
    feed.setChannelLanguage(m_channelLanguage);
    feed.setChannelDescription(m_channelDescription);
    final Set<Page> changed = m_engine.getManager(PageManager.class).getRecentChanges();
    final Session session = Wiki.session().guest(m_engine);
    int items = 0;
    for (final Iterator<Page> i = changed.iterator(); i.hasNext() && items < 15; items++) {
        final Page page = i.next();
        // Check if the anonymous user has view access to this page.
        if (!m_engine.getManager(AuthorizationManager.class).checkPermission(session, new PagePermission(page, PagePermission.VIEW_ACTION))) {
            // No permission, skip to the next one.
            continue;
        }
        final String url;
        if (page instanceof Attachment) {
            url = m_engine.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), page.getName(), null);
        } else {
            url = m_engine.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), page.getName(), null);
        }
        final Entry e = new Entry();
        e.setPage(page);
        e.setURL(url);
        e.setTitle(page.getName());
        e.setContent(getEntryDescription(page));
        e.setAuthor(getAuthor(page));
        feed.addEntry(e);
    }
    return feed.getString();
}
Also used : PageManager(org.apache.wiki.pages.PageManager) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) PagePermission(org.apache.wiki.auth.permissions.PagePermission) Session(org.apache.wiki.api.core.Session)

Aggregations

Session (org.apache.wiki.api.core.Session)51 Test (org.junit.jupiter.api.Test)25 WikiSessionTest (org.apache.wiki.WikiSessionTest)19 Principal (java.security.Principal)18 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)11 AllPermission (org.apache.wiki.auth.permissions.AllPermission)9 PageManager (org.apache.wiki.pages.PageManager)9 HttpSession (javax.servlet.http.HttpSession)8 Group (org.apache.wiki.auth.authorize.Group)8 PagePermission (org.apache.wiki.auth.permissions.PagePermission)8 Permission (java.security.Permission)7 Page (org.apache.wiki.api.core.Page)7 AuthenticationManager (org.apache.wiki.auth.AuthenticationManager)7 WikiPermission (org.apache.wiki.auth.permissions.WikiPermission)7 UnresolvedPrincipal (org.apache.wiki.auth.acl.UnresolvedPrincipal)6 UserProfile (org.apache.wiki.auth.user.UserProfile)6 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)5 WikiPrincipal (org.apache.wiki.auth.WikiPrincipal)5 Role (org.apache.wiki.auth.authorize.Role)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4