Search in sources :

Example 46 with HttpCookie

use of java.net.HttpCookie in project android-oss by kickstarter.

the class RefTagUtilsTest method testStoredCookieRefTagForProject.

@Test
public void testStoredCookieRefTagForProject() {
    final CookieManager cookieManager = new CookieManager();
    final CookieStore cookieStore = cookieManager.getCookieStore();
    final Project project = ProjectFactory.project();
    final RefTag refTag = RefTag.recommended();
    // set the cookie and retrieve the ref tag
    cookieStore.add(null, new HttpCookie("ref_" + project.id(), refTag.tag() + "%3F" + SystemUtils.secondsSinceEpoch()));
    final RefTag retrievedRefTag = RefTagUtils.storedCookieRefTagForProject(project, cookieManager, sharedPreferences);
    assertNotNull(retrievedRefTag);
    assertEquals(refTag, retrievedRefTag);
}
Also used : CookieStore(java.net.CookieStore) Project(com.kickstarter.models.Project) RefTag(com.kickstarter.libs.RefTag) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 47 with HttpCookie

use of java.net.HttpCookie in project android-oss by kickstarter.

the class RefTagUtilsTest method testFindRefTagCookieForProject_WhenCookieDoesNotExist.

@Test
public void testFindRefTagCookieForProject_WhenCookieDoesNotExist() {
    final CookieManager cookieManager = new CookieManager();
    final Project project = ProjectFactory.project();
    // retrieve the cookie
    final HttpCookie cookie = RefTagUtils.findRefTagCookieForProject(project, cookieManager, sharedPreferences);
    assertNull(cookie);
}
Also used : Project(com.kickstarter.models.Project) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 48 with HttpCookie

use of java.net.HttpCookie in project NoHttp by yanzhenjie.

the class CookieEntity method toHttpCookie.

/**
     * Into {@link HttpCookie}.
     *
     * @return {@link HttpCookie}.
     */
public HttpCookie toHttpCookie() {
    HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);
    cookie.setCommentURL(commentURL);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    if (expiry == -1L)
        cookie.setMaxAge(-1L);
    else
        cookie.setMaxAge((expiry - System.currentTimeMillis()) / 1000L);
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setSecure(secure);
    cookie.setVersion(version);
    return cookie;
}
Also used : HttpCookie(java.net.HttpCookie)

Example 49 with HttpCookie

use of java.net.HttpCookie in project zaproxy by zaproxy.

the class CookieBasedSessionManagementHelper method processMessageToMatchSession.

/**
	 * Modifies a message so its Request Header/Body matches the web session provided.
	 * 
	 * @param message the message
	 * @param requestCookies a pre-computed list with the request cookies (for optimization reasons)
	 * @param session the session
	 */
public static void processMessageToMatchSession(HttpMessage message, List<HttpCookie> requestCookies, HttpSession session) {
    // Make a copy of the session tokens set, as they will be modified
    HttpSessionTokensSet tokensSet = session.getTokensNames();
    // If no tokens exists create dummy Object -> NPE
    if (tokensSet == null) {
        tokensSet = new HttpSessionTokensSet();
    }
    Set<String> unsetSiteTokens = new LinkedHashSet<>(tokensSet.getTokensSet());
    // Iterate through the cookies in the request
    Iterator<HttpCookie> it = requestCookies.iterator();
    while (it.hasNext()) {
        HttpCookie cookie = it.next();
        String cookieName = cookie.getName();
        // If the cookie is a token
        if (tokensSet.isSessionToken(cookieName)) {
            String tokenValue = session.getTokenValue(cookieName);
            if (log.isDebugEnabled())
                log.debug("Changing value of token '" + cookieName + "' to: " + tokenValue);
            // Change it's value to the one in the active session, if any
            if (tokenValue != null) {
                cookie.setValue(tokenValue);
            } else // Or delete it, if the active session does not have a token value
            {
                it.remove();
            }
            // Remove the token from the token set so we know what tokens still have to be
            // added
            unsetSiteTokens.remove(cookieName);
        }
    }
    // value
    for (String token : unsetSiteTokens) {
        String tokenValue = session.getTokenValue(token);
        // Change it's value to the one in the active session, if any
        if (tokenValue != null) {
            if (log.isDebugEnabled())
                log.debug("Adding token '" + token + " with value: " + tokenValue);
            HttpCookie cookie = new HttpCookie(token, tokenValue);
            requestCookies.add(cookie);
        }
    }
    // Store the session in the HttpMessage for caching purpose
    message.setHttpSession(session);
    // Update the cookies in the message
    message.getRequestHeader().setCookies(requestCookies);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpSessionTokensSet(org.zaproxy.zap.extension.httpsessions.HttpSessionTokensSet) HttpCookie(java.net.HttpCookie)

Example 50 with HttpCookie

use of java.net.HttpCookie in project zaproxy by zaproxy.

the class CookieBasedSessionManagementHelper method getMatchingHttpSession.

/**
	 * Gets the matching http session, if any, for a particular message containing a list of
	 * cookies, from a set of sessions.
	 * 
	 * @param sessions the existing sessions
	 * @param cookies the cookies present in the request header of the message
	 * @param siteTokens the tokens
	 * @return the matching http session, if any, or null if no existing session was found to match
	 *         all the tokens
	 */
public static HttpSession getMatchingHttpSession(final Collection<HttpSession> sessions, List<HttpCookie> cookies, final HttpSessionTokensSet siteTokens) {
    // Pre-checks
    if (sessions.isEmpty()) {
        return null;
    }
    List<HttpSession> matchingSessions = new LinkedList<>(sessions);
    for (String token : siteTokens.getTokensSet()) {
        // Get the corresponding cookie from the cookies list
        HttpCookie matchingCookie = null;
        for (HttpCookie cookie : cookies) {
            if (cookie.getName().equals(token)) {
                matchingCookie = cookie;
                break;
            }
        }
        // Filter the sessions that do not match the cookie value
        Iterator<HttpSession> it = matchingSessions.iterator();
        while (it.hasNext()) {
            if (!it.next().matchesToken(token, matchingCookie)) {
                it.remove();
            }
        }
    }
    // Return the matching session
    if (matchingSessions.size() >= 1) {
        if (matchingSessions.size() > 1) {
            log.warn("Multiple sessions matching the cookies from response. Using first one.");
        }
        return matchingSessions.get(0);
    }
    return null;
}
Also used : HttpSession(org.zaproxy.zap.extension.httpsessions.HttpSession) HttpCookie(java.net.HttpCookie) LinkedList(java.util.LinkedList)

Aggregations

HttpCookie (java.net.HttpCookie)151 CookieManager (java.net.CookieManager)49 CookieStore (java.net.CookieStore)33 URI (java.net.URI)32 Test (org.junit.Test)31 IOException (java.io.IOException)16 Test (org.testng.annotations.Test)13 MockResponse (com.google.mockwebserver.MockResponse)11 MockWebServer (com.google.mockwebserver.MockWebServer)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Project (com.kickstarter.models.Project)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 Cookie (javax.servlet.http.Cookie)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 MockWebServer (okhttp3.mockwebserver.MockWebServer)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 ByteString (com.linkedin.data.ByteString)4