Search in sources :

Example 26 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class TestDeployZimlet method testEmptyAid.

@Test
public void testEmptyAid() throws Exception {
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
    com.zimbra.soap.admin.message.AuthRequest authReq = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
    authReq.setCsrfSupported(false);
    Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
    com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(response);
    String authToken = authResp.getAuthToken();
    transport.setAdmin(true);
    transport.setAuthToken(authToken);
    AttachmentIdAttrib att = new AttachmentIdAttrib("");
    DeployZimletRequest deployReq = new DeployZimletRequest(AdminConstants.A_DEPLOYLOCAL, false, true, att);
    Element req = JaxbUtil.jaxbToElement(deployReq);
    try {
        Element res = transport.invoke(req);
        JaxbUtil.elementToJaxb(res);
        fail("Should throw SoapFaultException");
    } catch (SoapFaultException e) {
    //expected
    }
}
Also used : Element(com.zimbra.common.soap.Element) SoapFaultException(com.zimbra.common.soap.SoapFaultException) DeployZimletRequest(com.zimbra.soap.admin.message.DeployZimletRequest) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) AttachmentIdAttrib(com.zimbra.soap.admin.type.AttachmentIdAttrib) Test(org.junit.Test)

Example 27 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class TestUtil method getAdminSoapTransport.

/**
     * Returns an authenticated transport for the <tt>zimbra</tt> account.
     */
public static SoapTransport getAdminSoapTransport() throws SoapFaultException, IOException, ServiceException {
    SoapHttpTransport transport = new SoapHttpTransport(getAdminSoapUrl());
    // Create auth element
    Element auth = new XMLElement(AdminConstants.AUTH_REQUEST);
    auth.addNonUniqueElement(AdminConstants.E_NAME).setText(LC.zimbra_ldap_user.value());
    auth.addNonUniqueElement(AdminConstants.E_PASSWORD).setText(LC.zimbra_ldap_password.value());
    // Authenticate and get auth token
    Element response = transport.invoke(auth);
    String authToken = response.getElement(AccountConstants.E_AUTH_TOKEN).getText();
    transport.setAuthToken(authToken);
    transport.setAdmin(true);
    return transport;
}
Also used : Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) XMLElement(com.zimbra.common.soap.Element.XMLElement)

Example 28 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class TestAuth method OAuth.

@Test
public void OAuth() throws Exception {
    Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain);
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
    Element eAuthReq = Element.create(transport.getRequestProtocol(), AccountConstants.AUTH_REQUEST);
    // <authtoken>
    Element eAuthToken = eAuthReq.addElement(AccountConstants.E_AUTH_TOKEN);
    eAuthToken.addAttribute(AccountConstants.A_TYPE, "oauth");
    String accessToken = "whatever";
    Element eAccessToken = eAuthToken.addElement(AccountConstants.E_A);
    eAccessToken.addAttribute(AccountConstants.A_N, ZimbraOAuthProvider.OAUTH_ACCESS_TOKEN);
    eAccessToken.setText(accessToken);
    // <account>
    Element eAcct = eAuthReq.addElement(AccountConstants.E_ACCOUNT);
    eAcct.addAttribute(AccountConstants.A_BY, AccountBy.name.name());
    eAcct.setText(acct.getName());
    Element eResp = transport.invoke(eAuthReq);
    Element eAuthTokenResp = eResp.getElement(AccountConstants.E_AUTH_TOKEN);
    String authToken = eAuthTokenResp.getText();
    assertNotNull(authToken);
}
Also used : Account(com.zimbra.cs.account.Account) Element(com.zimbra.common.soap.Element) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Test(org.junit.Test)

Example 29 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class TestAutoProvision method authRequestByKrb5.

/*
     * Note: need to restart server each time before re-run.  Otherwise server would still 
     * have the account created in previous run cached.
     */
@Test
// need to setup KDC
@Ignore
public void authRequestByKrb5() throws Exception {
    String testName = getTestName();
    String externalPassword = "test456";
    String extAcctLocalPart = testName;
    String extAcctName = createExternalAcctEntry(extAcctLocalPart, externalPassword, null);
    String krb5Realm = "MYREALM";
    Map<String, Object> zimbraDomainAttrs = commonZimbraDomainAttrs();
    // setup auto prov
    StringUtil.addToMultiMap(zimbraDomainAttrs, Provisioning.A_zimbraAutoProvAuthMech, AutoProvAuthMech.KRB5.name());
    zimbraDomainAttrs.put(Provisioning.A_zimbraAutoProvLdapSearchBase, extDomainDn);
    zimbraDomainAttrs.put(Provisioning.A_zimbraAutoProvLdapSearchFilter, "(uid=%u)");
    // setup auth mech and krb5 realm on domain
    zimbraDomainAttrs.put(Provisioning.A_zimbraAuthMech, AuthMech.kerberos5.name());
    zimbraDomainAttrs.put(Provisioning.A_zimbraAuthKerberos5Realm, krb5Realm);
    zimbraDomainAttrs.put(Provisioning.A_zimbraAuthKerberos5Realm, krb5Realm);
    Domain zimbraDomain = createZimbraDomain(testName, zimbraDomainAttrs);
    String loginName = extAcctLocalPart + "@" + krb5Realm;
    // make the soap request
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
    Element request = Element.create(transport.getRequestProtocol(), AccountConstants.AUTH_REQUEST);
    request.addElement(AccountConstants.E_ACCOUNT).addAttribute(AccountConstants.A_BY, AccountBy.krb5Principal.name()).setText(loginName);
    request.addElement(AccountConstants.E_PASSWORD).setText(externalPassword);
    Element response = transport.invoke(request);
    String encodedAuthToken = response.getElement(AccountConstants.E_AUTH_TOKEN).getText();
    assertNotNull(encodedAuthToken);
    String acctId = getAuthTokenAcctId(encodedAuthToken);
    Account acct = prov.get(AccountBy.id, acctId);
    verifyAcctAutoProvisioned(acct, loginName.toLowerCase());
}
Also used : Account(com.zimbra.cs.account.Account) Element(com.zimbra.common.soap.Element) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Domain(com.zimbra.cs.account.Domain) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with SoapHttpTransport

use of com.zimbra.common.soap.SoapHttpTransport in project zm-mailbox by Zimbra.

the class TestFileUpload method testAdminUploadWithCsrfInHeader.

@Test
public void testAdminUploadWithCsrfInHeader() 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;
    PostMethod post = new PostMethod(Url);
    FilePart part = new FilePart(FILE_NAME, new ByteArrayPartSource(FILE_NAME, "some file content".getBytes()));
    String contentType = "application/x-msdownload";
    part.setContentType(contentType);
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    HttpState state = new HttpState();
    state.addCookie(new org.apache.commons.httpclient.Cookie("localhost", ZimbraCookie.authTokenCookieName(true), authToken, "/", null, false));
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    client.setState(state);
    post.setRequestEntity(new MultipartRequestEntity(new Part[] { part }, post.getParams()));
    post.addRequestHeader(Constants.CSRF_TOKEN, csrfToken);
    int statusCode = HttpClientUtil.executeMethod(client, post);
    Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
    String resp = post.getResponseBodyAsString();
    Assert.assertNotNull("Response should not be empty", resp);
    Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HeaderElement(org.apache.commons.httpclient.HeaderElement) Element(com.zimbra.common.soap.Element) HttpState(org.apache.commons.httpclient.HttpState) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) ServiceException(com.zimbra.common.service.ServiceException) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Test(org.junit.Test)

Aggregations

SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)69 Element (com.zimbra.common.soap.Element)54 Test (org.junit.Test)32 SoapFaultException (com.zimbra.common.soap.SoapFaultException)16 ServiceException (com.zimbra.common.service.ServiceException)13 Account (com.zimbra.cs.account.Account)13 DeployZimletRequest (com.zimbra.soap.admin.message.DeployZimletRequest)13 AuthRequest (com.zimbra.soap.account.message.AuthRequest)12 AttachmentIdAttrib (com.zimbra.soap.admin.type.AttachmentIdAttrib)12 XMLElement (com.zimbra.common.soap.Element.XMLElement)11 AuthToken (com.zimbra.cs.account.AuthToken)10 ZAuthToken (com.zimbra.common.auth.ZAuthToken)8 AuthResponse (com.zimbra.soap.account.message.AuthResponse)8 IOException (java.io.IOException)8 ZMailbox (com.zimbra.client.ZMailbox)7 ZimbraAuthToken (com.zimbra.cs.account.ZimbraAuthToken)6 AccountSelector (com.zimbra.soap.type.AccountSelector)6 HashMap (java.util.HashMap)5 Domain (com.zimbra.cs.account.Domain)4 Server (com.zimbra.cs.account.Server)4