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.");
}
}
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);
}
}
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);
}
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) {
}
}
}
}
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;
}
}
Aggregations