Search in sources :

Example 1 with AuthToken

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

the class CsrfUtilTest method testIsValidCsrfTokenForAccountWithMultipleTokens.

@Test
public final void testIsValidCsrfTokenForAccountWithMultipleTokens() {
    try {
        Account acct = Provisioning.getInstance().getAccountByName("test@zimbra.com");
        AuthToken authToken = new ZimbraAuthToken(acct);
        String csrfToken1 = CsrfUtil.generateCsrfToken(acct.getId(), AUTH_TOKEN_EXPR, CSRFTOKEN_SALT, authToken);
        boolean validToken = CsrfUtil.isValidCsrfToken(csrfToken1, authToken);
        assertTrue(validToken);
    } catch (ServiceException e) {
        fail("Should not throw exception.");
    }
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthToken(com.zimbra.cs.account.AuthToken) Test(org.junit.Test)

Example 2 with AuthToken

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

the class OAuthAccessTokenServlet method processRequest.

public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    try {
        String origUrl = request.getHeader("X-Zimbra-Orig-Url");
        OAuthMessage oAuthMessage = StringUtil.isNullOrEmpty(origUrl) ? OAuthServlet.getMessage(request, null) : OAuthServlet.getMessage(request, origUrl);
        OAuthAccessor accessor = OAuthServiceProvider.getAccessor(oAuthMessage);
        OAuthServiceProvider.VALIDATOR.validateAccTokenMessage(oAuthMessage, accessor);
        // make sure token is authorized
        if (!Boolean.TRUE.equals(accessor.getProperty("authorized"))) {
            OAuthProblemException problem = new OAuthProblemException("permission_denied");
            LOG.debug("permission_denied");
            throw problem;
        }
        AuthToken userAuthToken = ZimbraAuthToken.getAuthToken((String) accessor.getProperty("ZM_AUTH_TOKEN"));
        String accountId = userAuthToken.getAccountId();
        Account account = Provisioning.getInstance().getAccountById(accountId);
        // generate access token and secret
        OAuthServiceProvider.generateAccessToken(accessor);
        account.addForeignPrincipal("oAuthAccessToken:" + accessor.accessToken);
        account.addOAuthAccessor(accessor.accessToken + "::" + new OAuthAccessorSerializer().serialize(accessor));
        response.setContentType("text/plain");
        OutputStream out = response.getOutputStream();
        OAuth.formEncode(OAuth.newList("oauth_token", accessor.accessToken, "oauth_token_secret", accessor.tokenSecret), out);
        out.close();
    } catch (Exception e) {
        LOG.debug("AccessTokenHandler exception", e);
        OAuthServiceProvider.handleException(e, request, response, true);
    }
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) OAuthProblemException(net.oauth.OAuthProblemException) Account(com.zimbra.cs.account.Account) OAuthMessage(net.oauth.OAuthMessage) OutputStream(java.io.OutputStream) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthToken(com.zimbra.cs.account.AuthToken) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) OAuthProblemException(net.oauth.OAuthProblemException)

Example 3 with AuthToken

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

the class MailItemResource method getZMailbox.

private static ZMailbox getZMailbox(DavContext ctxt, Collection col) throws ServiceException {
    AuthToken authToken = AuthProvider.getAuthToken(ctxt.getAuthAccount());
    Account acct = Provisioning.getInstance().getAccountById(col.getItemId().getAccountId());
    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 : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) AuthToken(com.zimbra.cs.account.AuthToken)

Example 4 with AuthToken

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

the class GetMiniCal method doRemoteFolders.

private static void doRemoteFolders(ZimbraSoapContext zsc, String remoteAccountId, List<String> remoteFolders, long rangeStart, long rangeEnd, Set<String> busyDates, Element response, Map<ItemId, ItemId> reverseIidMap, ItemIdFormatter ifmt) {
    try {
        Account target = Provisioning.getInstance().get(Key.AccountBy.id, remoteAccountId);
        if (target == null)
            throw AccountServiceException.NO_SUCH_ACCOUNT(remoteAccountId);
        AuthToken authToken = AuthToken.getCsrfUnsecuredAuthToken(zsc.getAuthToken());
        ZMailbox.Options zoptions = new ZMailbox.Options(authToken.toZAuthToken(), AccountUtil.getSoapUri(target));
        zoptions.setTargetAccount(remoteAccountId);
        zoptions.setTargetAccountBy(AccountBy.id);
        zoptions.setNoSession(true);
        ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
        String[] remoteIds = new String[remoteFolders.size()];
        for (int i = 0; i < remoteIds.length; i++) remoteIds[i] = remoteFolders.get(i).toString();
        ZGetMiniCalResult result = zmbx.getMiniCal(rangeStart, rangeEnd, remoteIds);
        Set<String> dates = result.getDates();
        if (dates != null) {
            for (String datestamp : dates) {
                busyDates.add(datestamp);
            }
        }
        List<ZMiniCalError> errors = result.getErrors();
        if (errors != null) {
            for (ZMiniCalError error : errors) {
                try {
                    ItemId iid = new ItemId(error.getFolderId(), zsc);
                    // Error must mention folder id requested by client.
                    ItemId reqIid = reverseIidMap.get(iid);
                    String fid = ifmt.formatItemId(reqIid != null ? reqIid : iid);
                    addError(response, fid, error.getErrCode(), error.getErrMsg());
                } catch (ServiceException e) {
                }
            }
        }
    } catch (ServiceException e) {
        ZimbraLog.calendar.warn("Error making remote GetMiniCalRequest", e);
        // Mark all remote folders with the same error.
        for (String remoteFid : remoteFolders) {
            try {
                ItemId iid = new ItemId(remoteFid, zsc);
                // Error must mention folder id requested by client.
                ItemId reqIid = reverseIidMap.get(iid);
                String fid = ifmt.formatItemId(reqIid != null ? reqIid : iid);
                addError(response, fid, e.getCode(), e.getMessage());
            } catch (ServiceException e2) {
            }
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account) ZGetMiniCalResult(com.zimbra.client.ZMailbox.ZGetMiniCalResult) ItemId(com.zimbra.cs.service.util.ItemId) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) ZMiniCalError(com.zimbra.client.ZMailbox.ZMiniCalError) ZMailbox(com.zimbra.client.ZMailbox) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) AuthToken(com.zimbra.cs.account.AuthToken)

Example 5 with AuthToken

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

the class Mailbox method getRemoteCalItemByUID.

public com.zimbra.soap.mail.type.CalendarItemInfo getRemoteCalItemByUID(Account ownerAccount, String uid, boolean includeInvites, boolean includeContent) throws ServiceException {
    Options options = new Options();
    AuthToken authToken = AuthToken.getCsrfUnsecuredAuthToken(getAuthToken(getOperationContext()));
    options.setAuthToken(authToken.toZAuthToken());
    options.setTargetAccount(getAccount().getName());
    options.setTargetAccountBy(AccountBy.name);
    options.setUri(AccountUtil.getSoapUri(ownerAccount));
    options.setNoSession(true);
    ZMailbox zmbox = ZMailbox.getMailbox(options);
    try {
        return zmbox.getRemoteCalItemByUID(ownerAccount.getId(), uid, includeInvites, includeContent);
    } catch (ServiceException e) {
        String exceptionCode = e.getCode();
        if (exceptionCode.equals(AccountServiceException.NO_SUCH_ACCOUNT) || exceptionCode.equals(MailServiceException.NO_SUCH_CALITEM)) {
            ZimbraLog.calendar.debug("Either remote acct or calendar item not found [%s]", exceptionCode);
        } else {
            ZimbraLog.calendar.debug("Unexpected exception thrown when getting remote calendar item - ignoring", e);
        }
        return null;
    }
}
Also used : ParsedMessageOptions(com.zimbra.cs.mime.ParsedMessageOptions) Options(com.zimbra.client.ZMailbox.Options) ZMailbox(com.zimbra.client.ZMailbox) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) AuthToken(com.zimbra.cs.account.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken)

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