Search in sources :

Example 1 with LmcSession

use of com.zimbra.cs.client.LmcSession in project zm-mailbox by Zimbra.

the class SoapCLI method auth.

/**
     * Authenticates using the provided ZAuthToken
     * @throws IOException
     * @throws com.zimbra.common.soap.SoapFaultException
     * @throws ServiceException
     */
protected LmcSession auth(ZAuthToken zAuthToken) throws SoapFaultException, IOException, ServiceException {
    if (zAuthToken == null)
        return auth();
    URL url = new URL("https", mHost, mPort, AdminConstants.ADMIN_SERVICE_URI);
    mServerUrl = url.toExternalForm();
    SoapTransport trans = getTransport();
    mAuth = false;
    Element authReq = new Element.XMLElement(AdminConstants.AUTH_REQUEST);
    zAuthToken.encodeAuthReq(authReq, true);
    try {
        Element authResp = trans.invokeWithoutSession(authReq);
        ZAuthToken zat = new ZAuthToken(authResp.getElement(AdminConstants.E_AUTH_TOKEN), true);
        trans.setAuthToken(zat);
        mAuth = true;
        return new LmcSession(zat, null);
    } catch (UnknownHostException e) {
        // UnknownHostException's error message is not clear; rethrow with a more descriptive message
        throw new IOException("Unknown host: " + mHost);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) Element(com.zimbra.common.soap.Element) LmcSession(com.zimbra.cs.client.LmcSession) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) URL(java.net.URL) SoapTransport(com.zimbra.common.soap.SoapTransport)

Example 2 with LmcSession

use of com.zimbra.cs.client.LmcSession in project zm-mailbox by Zimbra.

the class SoapCLI method auth.

/**
     * Authenticates using the username and password from the local config.
     * @throws IOException
     * @throws com.zimbra.common.soap.SoapFaultException
     * @throws ServiceException
     */
protected LmcSession auth() throws SoapFaultException, IOException, ServiceException {
    URL url = new URL("https", mHost, mPort, AdminConstants.ADMIN_SERVICE_URI);
    mServerUrl = url.toExternalForm();
    SoapTransport trans = getTransport();
    mAuth = false;
    Element authReq = new Element.XMLElement(AdminConstants.AUTH_REQUEST);
    authReq.addAttribute(AdminConstants.E_NAME, mUser, Element.Disposition.CONTENT);
    authReq.addAttribute(AdminConstants.E_PASSWORD, mPassword, Element.Disposition.CONTENT);
    try {
        Element authResp = trans.invokeWithoutSession(authReq);
        String authToken = authResp.getAttribute(AdminConstants.E_AUTH_TOKEN);
        ZAuthToken zat = new ZAuthToken(null, authToken, null);
        trans.setAuthToken(authToken);
        mAuth = true;
        return new LmcSession(zat, null);
    } catch (UnknownHostException e) {
        // UnknownHostException's error message is not clear; rethrow with a more descriptive message
        throw new IOException("Unknown host: " + mHost);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) Element(com.zimbra.common.soap.Element) LmcSession(com.zimbra.cs.client.LmcSession) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) URL(java.net.URL) SoapTransport(com.zimbra.common.soap.SoapTransport)

Example 3 with LmcSession

use of com.zimbra.cs.client.LmcSession in project zm-mailbox by Zimbra.

the class TestAccount method testDeleteAccount.

public void testDeleteAccount() throws Exception {
    ZimbraLog.test.debug("testDeleteAccount()");
    // Get the account and mailbox
    Account account = TestUtil.getAccount(USER_NAME);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    String dbName = DbMailbox.getDatabaseName(mbox);
    ZimbraLog.test.debug("Account=" + account.getId() + ", mbox=" + mbox.getId());
    // Confirm that the mailbox database exists
    DbResults results = DbUtil.executeQuery("SELECT COUNT(*) FROM mailbox WHERE id = " + mbox.getId());
    assertEquals("Could not find row in mailbox table", 1, results.getInt(1));
    results = DbUtil.executeQuery("SHOW DATABASES LIKE '" + dbName + "'");
    assertEquals("Could not find mailbox database", 1, results.size());
    // Add a message to the account and confirm that the message directory exists
    TestUtil.addMessage(mbox, "TestAccount testDeleteAccount");
    String storePath = VolumeManager.getInstance().getCurrentMessageVolume().getMessageRootDir(mbox.getId());
    File storeDir = new File(storePath);
    if (TestUtil.checkLocalBlobs()) {
        assertTrue(storePath + " does not exist", storeDir.exists());
        assertTrue(storePath + " is not a directory", storeDir.isDirectory());
    }
    // Delete the account
    LmcSession session = TestUtil.getAdminSoapSession();
    LmcDeleteAccountRequest req = new LmcDeleteAccountRequest(account.getId());
    req.setSession(session);
    req.invoke(TestUtil.getAdminSoapUrl());
    // Confirm that the mailbox was deleted
    results = DbUtil.executeQuery("SELECT COUNT(*) FROM mailbox WHERE id = " + mbox.getId());
    assertEquals("Unexpected row in mailbox table", 0, results.getInt(1));
    if (TestUtil.checkLocalBlobs()) {
        // Confirm that the message directory was deleted
        assertFalse(storePath + " exists", storeDir.exists());
    }
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) DbMailbox(com.zimbra.cs.db.DbMailbox) LmcSession(com.zimbra.cs.client.LmcSession) DbResults(com.zimbra.cs.db.DbResults) File(java.io.File) LmcDeleteAccountRequest(com.zimbra.cs.client.soap.LmcDeleteAccountRequest)

Example 4 with LmcSession

use of com.zimbra.cs.client.LmcSession in project zm-mailbox by Zimbra.

the class TestAuthentication method testAccessDeletedAccount.

/**
     * Attempts to access a deleted account and confirms that the attempt
     * fails with an auth error.
     */
public void testAccessDeletedAccount() throws Exception {
    // Log in and check the inbox
    LmcSession session = TestUtil.getSoapSession(USER_NAME);
    LmcSearchRequest req = new LmcSearchRequest();
    req.setQuery("in:inbox");
    req.setSession(session);
    req.invoke(TestUtil.getSoapUrl());
    // Delete the account
    Account account = getAccount();
    assertNotNull("Account does not exist", account);
    mProv.deleteAccount(account.getId());
    // Submit another request and make sure it fails with an auth error
    try {
        req.invoke(TestUtil.getSoapUrl());
    } catch (SoapFaultException ex) {
        assertTrue("Unexpected error: " + ex.getMessage(), ex.getMessage().indexOf("auth credentials have expired") >= 0);
    }
}
Also used : LmcSearchRequest(com.zimbra.cs.client.soap.LmcSearchRequest) Account(com.zimbra.cs.account.Account) LmcSession(com.zimbra.cs.client.LmcSession) SoapFaultException(com.zimbra.common.soap.SoapFaultException)

Example 5 with LmcSession

use of com.zimbra.cs.client.LmcSession in project zm-mailbox by Zimbra.

the class TestAuthentication method testAccessInactiveAccount.

/**
     * Attempts to access a deleted account and confirms that the attempt
     * fails with an auth error.
     */
public void testAccessInactiveAccount() throws Exception {
    // Log in and check the inbox
    LmcSession session = TestUtil.getSoapSession(USER_NAME);
    LmcSearchRequest req = new LmcSearchRequest();
    req.setQuery("in:inbox");
    req.setSession(session);
    req.invoke(TestUtil.getSoapUrl());
    // Deactivate the account
    Account account = getAccount();
    mProv.modifyAccountStatus(account, Provisioning.ACCOUNT_STATUS_MAINTENANCE);
    // Submit another request and make sure it fails with an auth error
    try {
        req.invoke(TestUtil.getSoapUrl());
    } catch (SoapFaultException ex) {
        String substring = "auth credentials have expired";
        String msg = String.format("Error message '%s' does not contain '%s'", ex.getMessage(), substring);
        assertTrue(msg, ex.getMessage().contains(substring));
    }
}
Also used : LmcSearchRequest(com.zimbra.cs.client.soap.LmcSearchRequest) Account(com.zimbra.cs.account.Account) LmcSession(com.zimbra.cs.client.LmcSession) SoapFaultException(com.zimbra.common.soap.SoapFaultException)

Aggregations

LmcSession (com.zimbra.cs.client.LmcSession)5 Account (com.zimbra.cs.account.Account)3 ZAuthToken (com.zimbra.common.auth.ZAuthToken)2 Element (com.zimbra.common.soap.Element)2 SoapFaultException (com.zimbra.common.soap.SoapFaultException)2 SoapTransport (com.zimbra.common.soap.SoapTransport)2 LmcSearchRequest (com.zimbra.cs.client.soap.LmcSearchRequest)2 IOException (java.io.IOException)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 LmcDeleteAccountRequest (com.zimbra.cs.client.soap.LmcDeleteAccountRequest)1 DbMailbox (com.zimbra.cs.db.DbMailbox)1 DbResults (com.zimbra.cs.db.DbResults)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 File (java.io.File)1