Search in sources :

Example 76 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.

the class ZimbraJWToken method encode.

@Override
public void encode(BasicCookieStore state, boolean isAdminReq, String cookieDomain) throws ServiceException {
    BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.COOKIE_ZM_JWT, JWTUtil.getJWTSalt(properties.getEncoded()));
    cookie.setDomain(cookieDomain);
    cookie.setPath("/");
    cookie.setSecure(false);
    state.addCookie(cookie);
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 77 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.

the class ZimbraJWToken method encode.

/* (non-Javadoc)
     * @see com.zimbra.cs.account.AuthToken#encode(org.apache.http.impl.client.HttpClientBuilder, boolean, java.lang.String)
     */
@Override
public void encode(HttpClientBuilder clientBuilder, HttpRequestBase method, boolean isAdminReq, String cookieDomain) throws ServiceException {
    String jwt = properties.getEncoded();
    method.addHeader(Constants.AUTH_HEADER, Constants.BEARER + " " + jwt);
    String jwtSalt = JWTUtil.getJWTSalt(jwt);
    BasicCookieStore state = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.COOKIE_ZM_JWT, jwtSalt);
    cookie.setDomain(cookieDomain);
    cookie.setPath("/");
    cookie.setSecure(false);
    state.addCookie(cookie);
    clientBuilder.setDefaultCookieStore(state);
    RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    clientBuilder.setDefaultRequestConfig(reqConfig);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 78 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.

the class ZimbraJWToken method encode.

@Override
public void encode(HttpClient client, HttpRequestBase method, boolean isAdminReq, String cookieDomain) throws ServiceException {
    String jwt = properties.getEncoded();
    method.addHeader(Constants.AUTH_HEADER, Constants.BEARER + " " + jwt);
    String jwtSalt = JWTUtil.getJWTSalt(jwt);
    BasicCookieStore state = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.COOKIE_ZM_JWT, jwtSalt);
    cookie.setDomain(cookieDomain);
    cookie.setPath("/");
    cookie.setSecure(false);
    state.addCookie(cookie);
    HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    clientBuilder.setDefaultCookieStore(state);
    RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    clientBuilder.setDefaultRequestConfig(reqConfig);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Example 79 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.

the class TestCookieReuse method testWebLogOut.

/**
 * Verify that we canNOT RE-use the cookie for REST session after logging out of plain HTML client
 * @throws URISyntaxException
 * @throws InterruptedException
 * @throws HttpException
 */
@Test
public void testWebLogOut() throws ServiceException, IOException, URISyntaxException, InterruptedException, HttpException {
    // establish legitimate connection
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraForceClearCookies, "FALSE");
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    URI uri = mbox.getRestURI("Inbox?fmt=rss");
    HttpClientContext context = HttpClientContext.create();
    HttpClient alice = mbox.getHttpClient(uri);
    // create evesdropper's connection
    HttpClientBuilder eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    List<Cookie> cookies = context.getCookieStore().getCookies();
    BasicCookieStore cookieStore = new BasicCookieStore();
    for (Cookie cookie : cookies) {
        BasicClientCookie basicCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
        basicCookie.setDomain(uri.getHost());
        basicCookie.setPath("/");
        basicCookie.setSecure(false);
        cookieStore.addCookie(cookie);
    }
    eve.setDefaultCookieStore(cookieStore);
    Account a = TestUtil.getAccount(USER_NAME);
    a.setForceClearCookies(false);
    URI logoutUri = new URI(String.format("%s://%s%s/?loginOp=logout", uri.getScheme(), uri.getHost(), (uri.getPort() > 80 ? (":" + uri.getPort()) : "")));
    HttpGet logoutMethod = new HttpGet(logoutUri.toString());
    HttpResponse httpResp = alice.execute(logoutMethod);
    int statusCode = httpResp.getStatusLine().getStatusCode();
    Assert.assertEquals("Log out request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
    HttpGet get = new HttpGet(uri.toString());
    httpResp = HttpClientUtil.executeMethod(eve.build(), get, context);
    statusCode = httpResp.getStatusLine().getStatusCode();
    Assert.assertEquals("This request should not succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Account(com.zimbra.cs.account.Account) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) URI(java.net.URI) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) ZMailbox(com.zimbra.client.ZMailbox) HttpClient(org.apache.http.client.HttpClient) Test(org.junit.Test)

Example 80 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.

the class TestFileUpload method testAdminUploadWithCsrfInFormField.

@Test
public void testAdminUploadWithCsrfInFormField() throws Exception {
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
    com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
    req.setCsrfSupported(true);
    Element response = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
    com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(response);
    String authToken = authResp.getAuthToken();
    String csrfToken = authResp.getCsrfToken();
    int port = 7071;
    try {
        port = Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraAdminPort, 0);
    } catch (ServiceException e) {
        ZimbraLog.test.error("Unable to get admin SOAP port", e);
    }
    String Url = "https://localhost:" + port + ADMIN_UPLOAD_URL;
    HttpPost post = new HttpPost(Url);
    String contentType = "application/x-msdownload";
    HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    BasicCookieStore state = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie(ZimbraCookie.authTokenCookieName(true), authToken);
    cookie.setDomain("localhost");
    cookie.setPath("/");
    cookie.setSecure(false);
    state.addCookie(cookie);
    clientBuilder.setDefaultCookieStore(state);
    RequestConfig reqConfig = RequestConfig.copy(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getZimbraConnMgrParams().getReqConfig()).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    clientBuilder.setDefaultRequestConfig(reqConfig);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody(FILE_NAME, "some file content".getBytes(), ContentType.create(contentType), FILE_NAME);
    builder.addPart(FormBodyPartBuilder.create().addField("csrfToken", csrfToken).build());
    HttpEntity httpEntity = builder.build();
    post.setEntity(httpEntity);
    HttpClient client = clientBuilder.build();
    HttpResponse httpResponse = HttpClientUtil.executeMethod(client, post);
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
    String resp = EntityUtils.toString(httpResponse.getEntity());
    Assert.assertNotNull("Response should not be empty", resp);
    Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) Element(com.zimbra.common.soap.Element) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) ServiceException(com.zimbra.common.service.ServiceException) HttpClient(org.apache.http.client.HttpClient) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Test(org.junit.Test)

Aggregations

BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)88 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)36 Test (org.junit.Test)24 HttpResponse (org.apache.http.HttpResponse)19 Cookie (org.apache.http.cookie.Cookie)17 HttpClient (org.apache.http.client.HttpClient)15 HttpGet (org.apache.http.client.methods.HttpGet)15 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)15 Map (java.util.Map)14 RequestConfig (org.apache.http.client.config.RequestConfig)13 HttpPost (org.apache.http.client.methods.HttpPost)9 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)9 CookieOrigin (org.apache.http.cookie.CookieOrigin)9 URL (java.net.URL)8 HttpEntity (org.apache.http.HttpEntity)8 CookieStore (org.apache.http.client.CookieStore)8 IOException (java.io.IOException)7 Date (java.util.Date)7 ClientCookie (org.apache.http.cookie.ClientCookie)7 HashMap (java.util.HashMap)6