Search in sources :

Example 76 with AuthToken

use of com.zimbra.cs.account.AuthToken in project zm-mailbox by Zimbra.

the class TestProvZimbraId method testFileUpload.

@Test
public void testFileUpload() throws Exception {
    Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain);
    int bodyLen = 128;
    byte[] body = new byte[bodyLen];
    SecureRandom sr = new SecureRandom();
    sr.nextBytes(body);
    Upload ulSaved = FileUploadServlet.saveUpload(new ByteArrayInputStream(body), "zimbraId-test", "text/plain", acct.getId());
    // System.out.println("Upload id is: " + ulSaved.getId());
    AuthToken authToken = AuthProvider.getAuthToken(acct);
    Upload ulFetched = FileUploadServlet.fetchUpload(acct.getId(), ulSaved.getId(), authToken);
    assertEquals(ulSaved.getId(), ulFetched.getId());
    assertEquals(ulSaved.getName(), ulFetched.getName());
    assertEquals(ulSaved.getSize(), ulFetched.getSize());
    assertEquals(ulSaved.getContentType(), ulFetched.getContentType());
    assertEquals(ulSaved.toString(), ulFetched.toString());
    byte[] bytesUploaded = ByteUtil.getContent(ulFetched.getInputStream(), -1);
    assertTrue(Arrays.equals(body, bytesUploaded));
}
Also used : Account(com.zimbra.cs.account.Account) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureRandom(java.security.SecureRandom) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) AuthToken(com.zimbra.cs.account.AuthToken)

Example 77 with AuthToken

use of com.zimbra.cs.account.AuthToken in project zm-mailbox by Zimbra.

the class OAuthAccessorSerializer method deserialize.

@Override
public OAuthAccessor deserialize(Object obj) throws ServiceException {
    String value = (String) obj;
    LOG.debug("get value: " + value);
    String consumer_key = value.substring(0, value.indexOf(",token_secret")).substring(13);
    String token_secret = value.substring(value.indexOf(",token_secret"), value.indexOf(",callback")).substring(14);
    String callback = value.substring(value.indexOf(",callback"), value.indexOf(",user")).substring(10);
    String user = value.substring(value.indexOf(",user"), value.indexOf(",authorized")).substring(6);
    String authorized = value.substring(value.indexOf(",authorized"), value.indexOf(",zauthtoken")).substring(12);
    String zauthtoken = value.substring(value.indexOf(",zauthtoken"), value.indexOf(",verifier")).substring(12);
    String verifier = value.substring(value.indexOf(",verifier"), value.indexOf(",approved_on")).substring(10);
    String approved_on = value.substring(value.indexOf(",approved_on"), value.indexOf(",device")).substring(13);
    String device = value.substring(value.indexOf(",device")).substring(8);
    LOG.debug("[consumer_key:%s, callback:%s, user:%s, authorized:%s, zauthtoken:%s, verifier:%s, approved_on:%s, device:%s]", consumer_key, callback, user, authorized, zauthtoken, verifier, approved_on, device);
    try {
        OAuthConsumer consumer = OAuthServiceProvider.getConsumer(consumer_key);
        OAuthAccessor accessor = new OAuthAccessor(consumer);
        accessor.tokenSecret = token_secret;
        accessor.setProperty(OAuth.OAUTH_CALLBACK, callback);
        if (!user.equals("null")) {
            accessor.setProperty("user", user);
        }
        if (authorized.equalsIgnoreCase(Boolean.FALSE.toString())) {
            accessor.setProperty("authorized", Boolean.FALSE);
        } else if (authorized.equalsIgnoreCase(Boolean.TRUE.toString())) {
            accessor.setProperty("authorized", Boolean.TRUE);
        }
        if (!zauthtoken.equals("null")) {
            accessor.setProperty("ZM_AUTH_TOKEN", zauthtoken);
            AuthToken zimbraAuthToken = ZimbraAuthToken.getAuthToken(zauthtoken);
            final Account account = zimbraAuthToken.getAccount();
            OAuthServiceProvider.setAccountPropertiesForAccessor(account, accessor);
        }
        if (!verifier.equals("null")) {
            accessor.setProperty(OAuth.OAUTH_VERIFIER, verifier);
        }
        if (null != approved_on) {
            accessor.consumer.setProperty("approved_on", approved_on);
        }
        if (null != device) {
            accessor.consumer.setProperty("device", device);
        }
        return accessor;
    } catch (Exception e) {
        //need more hack here for hadnling IOException properly
        throw ServiceException.FAILURE("IOException", e);
    }
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) Account(com.zimbra.cs.account.Account) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthToken(com.zimbra.cs.account.AuthToken) OAuthConsumer(net.oauth.OAuthConsumer) ServiceException(com.zimbra.common.service.ServiceException)

Example 78 with AuthToken

use of com.zimbra.cs.account.AuthToken in project zm-mailbox by Zimbra.

the class DavContext method getZMailbox.

public ZMailbox getZMailbox(Account acct) throws ServiceException {
    AuthToken authToken = AuthProvider.getAuthToken(getAuthAccount());
    ZMailbox.Options zoptions = new ZMailbox.Options(authToken.toZAuthToken(), AccountUtil.getSoapUri(acct));
    zoptions.setNoSession(true);
    zoptions.setTargetAccount(acct.getId());
    zoptions.setTargetAccountBy(Key.AccountBy.id);
    return ZMailbox.getMailbox(zoptions);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) AuthToken(com.zimbra.cs.account.AuthToken)

Example 79 with AuthToken

use of com.zimbra.cs.account.AuthToken in project zm-mailbox by Zimbra.

the class GalSearchControl method proxyGalAccountSearch.

private boolean proxyGalAccountSearch(Account galSyncAcct, boolean sync) throws IOException, ServiceException {
    try {
        Provisioning prov = Provisioning.getInstance();
        String serverUrl = URLUtil.getAdminURL(prov.getServerByName(galSyncAcct.getMailHost()));
        SoapHttpTransport transport = new SoapHttpTransport(serverUrl);
        AuthToken auth = mParams.getAuthToken();
        transport.setAuthToken((auth == null) ? AuthProvider.getAdminAuthToken().toZAuthToken() : auth.toZAuthToken());
        ZimbraSoapContext zsc = mParams.getSoapContext();
        if (zsc != null) {
            transport.setResponseProtocol(zsc.getResponseProtocol());
            String requestedAcctId = zsc.getRequestedAccountId();
            String authTokenAcctId = zsc.getAuthtokenAccountId();
            if (requestedAcctId != null && !requestedAcctId.equalsIgnoreCase(authTokenAcctId))
                transport.setTargetAcctId(requestedAcctId);
        }
        Element req = mParams.getRequest();
        if (req == null) {
            req = Element.create(mParams.getProxyProtocol(), AccountConstants.SEARCH_GAL_REQUEST);
            req.addAttribute(AccountConstants.A_TYPE, mParams.getType().toString());
            req.addAttribute(AccountConstants.A_LIMIT, mParams.getLimit());
            req.addAttribute(AccountConstants.A_NAME, mParams.getQuery());
            req.addAttribute(AccountConstants.A_REF, mParams.getSearchEntryByDn());
        }
        req.addAttribute(AccountConstants.A_GAL_ACCOUNT_ID, galSyncAcct.getId());
        req.addAttribute(AccountConstants.A_GAL_ACCOUNT_PROXIED, true);
        if (sync && mParams.getGalSyncToken() != null) {
            req.addAttribute(MailConstants.A_TOKEN, mParams.getGalSyncToken().toString());
            ZimbraLog.gal.debug("setting token for proxied request %s", mParams.getGalSyncToken().toString());
        }
        Element resp = transport.invokeWithoutSession(req.detach());
        GalSearchResultCallback callback = mParams.getResultCallback();
        if (callback.passThruProxiedGalAcctResponse()) {
            callback.handleProxiedResponse(resp);
            return true;
        }
        Iterator<Element> iter = resp.elementIterator(MailConstants.E_CONTACT);
        while (iter.hasNext()) callback.handleElement(iter.next());
        iter = resp.elementIterator(MailConstants.E_DELETED);
        while (iter.hasNext()) callback.handleElement(iter.next());
        String newTokenStr = resp.getAttribute(MailConstants.A_TOKEN, null);
        if (newTokenStr != null) {
            GalSyncToken newToken = new GalSyncToken(newTokenStr);
            ZimbraLog.gal.debug("computing new sync token for proxied account " + galSyncAcct.getId() + ": " + newToken);
            callback.setNewToken(newToken);
        }
        boolean hasMore = resp.getAttributeBool(MailConstants.A_QUERY_MORE, false);
        callback.setHasMoreResult(hasMore);
        if (hasMore && !sync) {
            callback.setSortBy(resp.getAttribute(MailConstants.A_SORTBY));
            callback.setQueryOffset((int) resp.getAttributeLong(MailConstants.A_QUERY_OFFSET));
        }
    } catch (SoapFaultException e) {
        GalSearchResultCallback callback = mParams.getResultCallback();
        if (callback.passThruProxiedGalAcctResponse()) {
            Element fault = e.getFault();
            callback.handleProxiedResponse(fault);
            // return true so we do *not* fallback to do the ldap search.
            return true;
        } else {
            ZimbraLog.gal.warn("remote search on GalSync account failed for " + galSyncAcct.getName(), e);
            return false;
        }
    }
    return true;
}
Also used : ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) AuthToken(com.zimbra.cs.account.AuthToken) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Provisioning(com.zimbra.cs.account.Provisioning) SoapFaultException(com.zimbra.common.soap.SoapFaultException)

Example 80 with AuthToken

use of com.zimbra.cs.account.AuthToken in project zm-mailbox by Zimbra.

the class GalSearchControl method checkFeatureEnabled.

private void checkFeatureEnabled(String extraFeatAttr) throws ServiceException {
    AuthToken authToken = mParams.getAuthToken();
    boolean isAdmin = authToken == null ? false : AuthToken.isAnyAdmin(authToken);
    // admin is always allowed.
    if (isAdmin)
        return;
    // check feature enabling attrs
    Account acct = mParams.getAccount();
    if (acct == null) {
        if (authToken != null)
            acct = Provisioning.getInstance().get(AccountBy.id, authToken.getAccountId());
        if (acct == null)
            throw ServiceException.PERM_DENIED("unable to get account for GAL feature checking");
    }
    if (!acct.getBooleanAttr(Provisioning.A_zimbraFeatureGalEnabled, false))
        throw ServiceException.PERM_DENIED("GAL feature (" + Provisioning.A_zimbraFeatureGalEnabled + ") is not enabled");
    if (extraFeatAttr != null) {
        if (!acct.getBooleanAttr(extraFeatAttr, false))
            throw ServiceException.PERM_DENIED("GAL feature (" + extraFeatAttr + ") is not enabled");
    }
}
Also used : Account(com.zimbra.cs.account.Account) AuthToken(com.zimbra.cs.account.AuthToken)

Aggregations

AuthToken (com.zimbra.cs.account.AuthToken)98 ServiceException (com.zimbra.common.service.ServiceException)46 Account (com.zimbra.cs.account.Account)44 ZimbraAuthToken (com.zimbra.cs.account.ZimbraAuthToken)27 AuthTokenException (com.zimbra.cs.account.AuthTokenException)26 Element (com.zimbra.common.soap.Element)24 Provisioning (com.zimbra.cs.account.Provisioning)23 ZMailbox (com.zimbra.client.ZMailbox)19 ZAuthToken (com.zimbra.common.auth.ZAuthToken)18 IOException (java.io.IOException)14 Server (com.zimbra.cs.account.Server)12 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)12 HttpClient (org.apache.commons.httpclient.HttpClient)12 HashMap (java.util.HashMap)11 GetMethod (org.apache.commons.httpclient.methods.GetMethod)11 Test (org.junit.Test)11 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)10 ServletException (javax.servlet.ServletException)10 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)8 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)8