use of com.zimbra.cs.service.UserServletContext in project zm-mailbox by Zimbra.
the class JWTBasedAuthTest method mockServletConext.
private UserServletContext mockServletConext(Account acct, boolean queryParamFlow) throws ServiceException, AuthTokenException, UserServletException {
HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
UserServlet usrSrv = EasyMock.createMock(UserServlet.class);
Cookie[] cookies = new Cookie[1];
String salt = "s1";
Cookie cookie = new Cookie("ZM_JWT", salt);
cookie.setHttpOnly(true);
cookies[0] = cookie;
int port = 7071;
String jwt = generateJWT(acct, salt);
String jwtQP = queryParamFlow ? "auth=jwt&zjwt=" + jwt : "auth=jwt";
EasyMock.expect(req.getQueryString()).andReturn(jwtQP);
EasyMock.expect(req.getPathInfo()).andReturn("/test/contacts");
EasyMock.expect(req.getCookies()).andReturn(cookies);
EasyMock.expect(req.getLocalPort()).andReturn(port);
jwt = queryParamFlow ? null : "Bearer " + jwt;
EasyMock.expect(req.getHeader(Constants.AUTH_HEADER)).andReturn(jwt);
EasyMock.replay(req);
UserServletContext context = new UserServletContext(req, resp, usrSrv);
return context;
}
use of com.zimbra.cs.service.UserServletContext in project zm-mailbox by Zimbra.
the class JWTBasedAuthTest method testUserServletJWTFlowWithJWTQueryParam.
@Test
public void testUserServletJWTFlowWithJWTQueryParam() {
try {
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
UserServletContext context = mockServletConext(acct, true);
UserServletUtil.getAccount(context);
Assert.assertNotNull(context.authToken);
Assert.assertEquals(acct.getId(), context.authToken.getAccountId());
} catch (ServiceException | AuthTokenException | IOException | ServletException | UserServletException e1) {
e1.printStackTrace();
Assert.fail("testUserServletJWTFlow failed");
}
}
use of com.zimbra.cs.service.UserServletContext in project zm-mailbox by Zimbra.
the class HtmlFormatter method dispatchJspRest.
static void dispatchJspRest(Servlet servlet, UserServletContext context) throws ServiceException, ServletException, IOException {
AuthToken auth = null;
long expiration = System.currentTimeMillis() + AUTH_EXPIRATION;
if (context.basicAuthHappened) {
Account acc = context.getAuthAccount();
if (acc instanceof GuestAccount) {
auth = AuthToken.getAuthToken(acc.getId(), acc.getName(), null, ((GuestAccount) acc).getDigest(), expiration);
} else {
auth = AuthProvider.getAuthToken(context.getAuthAccount(), expiration);
}
} else if (context.cookieAuthHappened) {
auth = UserServlet.getAuthTokenFromCookie(context.req, context.resp, true);
} else {
auth = AuthToken.getAuthToken(GuestAccount.GUID_PUBLIC, null, null, null, expiration);
}
if (auth != null && context.targetAccount != null && context.targetAccount != context.getAuthAccount()) {
auth.setProxyAuthToken(Provisioning.getInstance().getProxyAuthToken(context.targetAccount.getId(), null));
}
String authString = null;
try {
if (auth != null)
authString = auth.getEncoded();
} catch (AuthTokenException e) {
throw new ServletException("error generating the authToken", e);
}
Account targetAccount = context.targetAccount;
MailItem targetItem = context.target;
String uri = (String) context.req.getAttribute("requestedPath");
if (targetItem instanceof Mountpoint && ((Mountpoint) targetItem).getDefaultView() != MailItem.Type.APPOINTMENT) {
Mountpoint mp = (Mountpoint) targetItem;
Provisioning prov = Provisioning.getInstance();
targetAccount = prov.getAccountById(mp.getOwnerId());
Pair<Header[], HttpInputStream> remoteItem = UserServlet.getRemoteResourceAsStream((auth == null) ? null : auth.toZAuthToken(), mp.getTarget(), context.extraPath);
remoteItem.getSecond().close();
String remoteItemId = null;
String remoteItemType = null;
String remoteItemName = null;
String remoteItemPath = null;
for (Header h : remoteItem.getFirst()) if (h.getName().compareToIgnoreCase("X-Zimbra-ItemId") == 0)
remoteItemId = h.getValue();
else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemType") == 0)
remoteItemType = h.getValue();
else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemName") == 0)
remoteItemName = h.getValue();
else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemPath") == 0)
remoteItemPath = h.getValue();
context.req.setAttribute(ATTR_TARGET_ITEM_ID, remoteItemId);
context.req.setAttribute(ATTR_TARGET_ITEM_TYPE, remoteItemType);
context.req.setAttribute(ATTR_TARGET_ITEM_NAME, remoteItemName);
context.req.setAttribute(ATTR_TARGET_ITEM_PATH, remoteItemPath);
context.req.setAttribute(ATTR_TARGET_ITEM_COLOR, mp.getColor());
context.req.setAttribute(ATTR_TARGET_ITEM_VIEW, mp.getDefaultView().toByte());
targetItem = null;
}
context.req.setAttribute(ATTR_INTERNAL_DISPATCH, "yes");
context.req.setAttribute(ATTR_REQUEST_URI, uri != null ? uri : context.req.getRequestURI());
context.req.setAttribute(ATTR_AUTH_TOKEN, authString);
context.req.setAttribute(ATTR_CSRF_ENABLED, auth.isCsrfTokenEnabled());
if (targetAccount != null) {
context.req.setAttribute(ATTR_TARGET_ACCOUNT_NAME, targetAccount.getName());
context.req.setAttribute(ATTR_TARGET_ACCOUNT_ID, targetAccount.getId());
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_TIME_ZONE, targetAccount.getAttr(Provisioning.A_zimbraPrefTimeZoneId));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_SKIN, targetAccount.getAttr(Provisioning.A_zimbraPrefSkin));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_LOCALE, targetAccount.getAttr(Provisioning.A_zimbraPrefLocale));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_FIRST_DAY_OF_WEEK, targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarFirstDayOfWeek));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_START, targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarDayHourStart));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_END, targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarDayHourEnd));
} else {
// Useful when faking results - e.g. FREEBUSY html view for non-existent account
if (context.fakeTarget != null) {
context.req.setAttribute(ATTR_TARGET_ACCOUNT_NAME, context.fakeTarget.getAccount());
}
com.zimbra.cs.account.Cos defaultCos = Provisioning.getInstance().get(com.zimbra.common.account.Key.CosBy.name, Provisioning.DEFAULT_COS_NAME);
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_TIME_ZONE, defaultCos.getAttr(Provisioning.A_zimbraPrefTimeZoneId));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_SKIN, defaultCos.getAttr(Provisioning.A_zimbraPrefSkin));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_LOCALE, defaultCos.getAttr(Provisioning.A_zimbraPrefLocale));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_FIRST_DAY_OF_WEEK, defaultCos.getAttr(Provisioning.A_zimbraPrefCalendarFirstDayOfWeek));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_START, defaultCos.getAttr(Provisioning.A_zimbraPrefCalendarDayHourStart));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_END, defaultCos.getAttr(Provisioning.A_zimbraPrefCalendarDayHourEnd));
}
if (targetItem != null) {
context.req.setAttribute(ATTR_TARGET_ITEM_ID, targetItem.getId());
context.req.setAttribute(ATTR_TARGET_ITEM_PATH, targetItem.getPath());
context.req.setAttribute(ATTR_TARGET_ITEM_NAME, targetItem.getName());
context.req.setAttribute(ATTR_TARGET_ITEM_TYPE, targetItem.getType().toString());
context.req.setAttribute(ATTR_TARGET_ITEM_COLOR, targetItem.getColor());
if (targetItem instanceof Folder) {
context.req.setAttribute(ATTR_TARGET_ITEM_VIEW, ((Folder) targetItem).getDefaultView().toString());
}
context.req.setAttribute(ATTR_TARGET_ACTION, context.getAction());
context.req.setAttribute(ATTR_TARGET_BODYPART, context.getBodypart());
context.req.setAttribute(ATTR_TARGET_COLOR, context.getColor());
context.req.setAttribute(ATTR_TARGET_DATE, context.getDate());
context.req.setAttribute(ATTR_TARGET_EX_COMP_NUM, context.getExCompNum());
context.req.setAttribute(ATTR_TARGET_EX_INV_ID, context.getExInvId());
context.req.setAttribute(ATTR_TARGET_FMT, context.getFmt());
context.req.setAttribute(ATTR_TARGET_FOLDER_IDS, context.getFolderIds());
context.req.setAttribute(ATTR_TARGET_IM_ID, context.getImId());
context.req.setAttribute(ATTR_TARGET_IM_PART, context.getImPart());
context.req.setAttribute(ATTR_TARGET_IM_XIM, context.getImXim());
context.req.setAttribute(ATTR_TARGET_INST_DURATION, context.getInstDuration());
context.req.setAttribute(ATTR_TARGET_INST_START_TIME, context.getInstStartTime());
context.req.setAttribute(ATTR_TARGET_INV_COMP_NUM, context.getInvCompNum());
context.req.setAttribute(ATTR_TARGET_INV_ID, context.getInvId());
context.req.setAttribute(ATTR_TARGET_NOTOOLBAR, context.getNotoolbar());
context.req.setAttribute(ATTR_TARGET_NUMDAYS, context.getNumdays());
context.req.setAttribute(ATTR_TARGET_PSTAT, context.getPstat());
context.req.setAttribute(ATTR_TARGET_REFRESH, context.getRefresh());
context.req.setAttribute(ATTR_TARGET_SKIN, context.getSkin());
context.req.setAttribute(ATTR_TARGET_SQ, context.getSq());
context.req.setAttribute(ATTR_TARGET_TZ, context.getTz());
context.req.setAttribute(ATTR_TARGET_USE_INSTANCE, context.getUseInstance());
context.req.setAttribute(ATTR_TARGET_XIM, context.getXim());
context.req.setAttribute(ATTR_TARGET_VIEW, context.getView());
} else {
context.req.setAttribute(ATTR_TARGET_ITEM_COLOR, Color.getMappedColor(null));
}
if (context.fakeTarget != null) {
// Override to avoid address harvesting
context.req.setAttribute(ATTR_TARGET_ITEM_PATH, context.fakeTarget.getPath());
context.req.setAttribute(ATTR_TARGET_ITEM_NAME, context.fakeTarget.getName());
}
String mailUrl = PATH_MAIN_CONTEXT;
if (WebSplitUtil.isZimbraServiceSplitEnabled()) {
mailUrl = Provisioning.getInstance().getLocalServer().getWebClientURL() + PATH_JSP_REST_PAGE;
HttpClient httpclient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().getDefaultHttpClient().build();
/*
* Retest the code with POST to check whether it works
HttpPost postMethod = new HttpPost(mailUrl);
Enumeration<String> attributeNames = context.req.getAttributeNames();
List<Part> parts = new ArrayList<Part>();
while(attributeNames.hasMoreElements())
{
String attrName = (String) attributeNames.nextElement();
String attrValue = context.req.getAttribute(attrName).toString();
Part part = new StringPart(attrName, attrValue);
parts.add(part);
}
postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams()));
HttpClientUtil.executeMethod(httpclient, postMethod);
ByteUtil.copy(postMethod.getResponseBodyAsStream(), true, context.resp.getOutputStream(), true);
*/
Enumeration<String> attributeNames = context.req.getAttributeNames();
StringBuilder sb = new StringBuilder(mailUrl);
sb.append("?");
while (attributeNames.hasMoreElements()) {
String attrName = attributeNames.nextElement();
String attrValue = context.req.getAttribute(attrName).toString();
sb.append(attrName).append("=").append(HttpUtil.urlEscape(attrValue)).append("&");
}
HttpGet postMethod = new HttpGet(sb.toString());
postMethod.setHeader("Accept-Language", context.getLocale().getLanguage());
HttpResponse httpResp;
try {
httpResp = HttpClientUtil.executeMethod(httpclient, postMethod);
ByteUtil.copy(httpResp.getEntity().getContent(), true, context.resp.getOutputStream(), false);
} catch (HttpException e) {
throw new ServletException("error executing the request.", e);
}
} else {
try {
mailUrl = Provisioning.getInstance().getLocalServer().getMailURL();
} catch (Exception e) {
}
ServletContext targetContext = servlet.getServletConfig().getServletContext().getContext(mailUrl);
RequestDispatcher dispatcher = targetContext.getRequestDispatcher(PATH_JSP_REST_PAGE);
dispatcher.forward(context.req, context.resp);
}
}
use of com.zimbra.cs.service.UserServletContext in project zm-mailbox by Zimbra.
the class ArchiveFormatter method formatCallback.
@Override
public void formatCallback(UserServletContext context) throws IOException, ServiceException, UserServletException {
// Disable the jetty timeout
disableJettyTimeout(context);
HashMap<Integer, Integer> cnts = new HashMap<Integer, Integer>();
int dot;
HashMap<Integer, String> fldrs = new HashMap<Integer, String>();
String emptyname = context.params.get("emptyname");
String ext = "." + getType();
String filename = context.params.get("filename");
String lock = context.params.get("lock");
String query = context.getQueryString();
Set<String> names = new HashSet<String>(4096);
Set<MailItem.Type> sysTypes = EnumSet.of(MailItem.Type.FOLDER, MailItem.Type.SEARCHFOLDER, MailItem.Type.TAG, MailItem.Type.FLAG, MailItem.Type.MOUNTPOINT);
Set<MailItem.Type> searchTypes = EnumSet.of(MailItem.Type.MESSAGE, MailItem.Type.CONTACT, MailItem.Type.DOCUMENT, MailItem.Type.WIKI, MailItem.Type.APPOINTMENT, MailItem.Type.TASK, MailItem.Type.CHAT, MailItem.Type.NOTE);
ArchiveOutputStream aos = null;
String types = context.getTypesString();
MailboxMaintenance maintenance = null;
try {
if (filename == null || filename.equals("")) {
Date date = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
SimpleDateFormat sdf = new SimpleDateFormat(".H-m-s");
if (context.hasPart()) {
filename = "attachments";
} else {
filename = context.targetMailbox.getAccountId() + '.' + df.format(date).replace('/', '-') + sdf.format(date);
}
} else if ((dot = filename.lastIndexOf('.')) != -1) {
String userProvidedExtension = filename.substring(dot + 1);
if (BLE.contains(userProvidedExtension.toUpperCase())) {
filename = filename.substring(0, dot);
}
}
if (!filename.endsWith(ext)) {
filename += ext;
}
if (emptyname != null && !emptyname.equals("") && !emptyname.endsWith(ext)) {
emptyname += ext;
}
context.resp.addHeader("Content-Disposition", HttpUtil.createContentDisposition(context.req, Part.ATTACHMENT, filename));
if (ext.equals(".tar")) {
context.resp.setContentType("application/x-tar");
} else if (ext.equals(".tgz")) {
context.resp.setContentType("application/x-compressed-tar");
} else if (ext.equals(".zip")) {
context.resp.setContentType("application/zip");
}
if (!Strings.isNullOrEmpty(types)) {
try {
searchTypes = MailItem.Type.setOf(types);
} catch (IllegalArgumentException e) {
throw MailServiceException.INVALID_TYPE(e.getMessage());
}
sysTypes.clear();
// do not include conversations even when requested
// (to be compatible with 7.1.4 and later. bug 67407)
searchTypes.remove(MailItem.Type.CONVERSATION);
}
if (lock != null && (lock.equals("1") || lock.equals("t") || lock.equals("true"))) {
maintenance = MailboxManager.getInstance().beginMaintenance(context.targetMailbox.getAccountId(), context.targetMailbox.getId());
}
Charset charset = context.getCharset();
CharsetEncoder encoder = charset.newEncoder();
if (context.requestedItems != null) {
try {
for (UserServletContext.Item item : context.requestedItems) aos = saveItem(context, item.mailItem, fldrs, cnts, item.versioned, aos, encoder, names);
} catch (Exception e) {
warn(e);
}
} else if (context.target != null && !(context.target instanceof Folder)) {
try {
aos = saveItem(context, context.target, fldrs, cnts, false, aos, encoder, names);
} catch (Exception e) {
warn(e);
}
} else {
ZimbraQueryResults results = null;
boolean saveTargetFolder = false;
if (context.target instanceof Folder) {
Folder f = (Folder) context.target;
if (f.getId() != Mailbox.ID_FOLDER_USER_ROOT) {
saveTargetFolder = true;
query = "under:\"" + f.getPath() + "\"" + (query == null ? "" : " " + query);
}
}
int maxDays = context.targetAccount.getIntAttr(Provisioning.A_zimbraExportMaxDays, 0);
if (maxDays > 0) {
if (context.getStartTime() == TIME_UNSPECIFIED || context.getEndTime() == TIME_UNSPECIFIED || context.getEndTime() == 0) {
ZimbraLog.misc.warn("Export rejected. start and end param must be set. end param must be non-zero");
throw new ExportPeriodNotSpecifiedException(maxDays);
} else if (context.getEndTime() - context.getStartTime() > maxDays * Constants.MILLIS_PER_DAY) {
long requestDays = (long) Math.ceil((double) (context.getEndTime() - context.getStartTime()) / Constants.MILLIS_PER_DAY);
ZimbraLog.misc.warn("Export rejected. Specified period %d days is longer than zimbraExportMaxDays %d days.", requestDays, maxDays);
throw new ExportPeriodTooLongException(requestDays, maxDays);
}
}
String taskQuery = query;
if (query == null) {
query = "";
}
String calendarQuery = query;
if (context.getStartTime() != TIME_UNSPECIFIED) {
query = query + " after:" + context.getStartTime();
calendarQuery = calendarQuery + " appt-start:>=" + context.getStartTime();
}
if (context.getEndTime() != TIME_UNSPECIFIED) {
query = query + " before:" + context.getEndTime();
calendarQuery = calendarQuery + " appt-end:<" + context.getEndTime();
}
if (query == null || query.equals("")) {
SortPath sp = new SortPath();
for (MailItem.Type type : sysTypes) {
List<MailItem> items = context.targetMailbox.getItemList(context.opContext, type);
Collections.sort(items, sp);
for (MailItem item : items) {
aos = saveItem(context, item, fldrs, cnts, false, aos, encoder, names);
}
}
query = "is:local";
}
Map<Set<MailItem.Type>, String> typesMap = new HashMap<Set<MailItem.Type>, String>();
typesMap.put(searchTypes, query);
if (context.getStartTime() != TIME_UNSPECIFIED || context.getEndTime() != TIME_UNSPECIFIED) {
if (searchTypes.contains(MailItem.Type.APPOINTMENT)) {
searchTypes.remove(MailItem.Type.APPOINTMENT);
Set<MailItem.Type> calendarTypes = new HashSet<MailItem.Type>();
calendarTypes.add(MailItem.Type.APPOINTMENT);
typesMap.put(calendarTypes, calendarQuery);
}
if (searchTypes.contains(MailItem.Type.TASK)) {
searchTypes.remove(MailItem.Type.TASK);
Set<MailItem.Type> taskTypes = new HashSet<MailItem.Type>();
taskTypes.add(MailItem.Type.TASK);
typesMap.put(taskTypes, (StringUtil.isNullOrEmpty(taskQuery)) ? "is:local" : taskQuery);
}
}
for (Map.Entry<Set<MailItem.Type>, String> entry : typesMap.entrySet()) {
results = context.targetMailbox.index.search(context.opContext, entry.getValue(), entry.getKey(), SortBy.NONE, LC.zimbra_archive_formatter_search_chunk_size.intValue());
try {
while (results.hasNext()) {
if (saveTargetFolder) {
saveTargetFolder = false;
aos = saveItem(context, context.target, fldrs, cnts, false, aos, encoder, names);
}
aos = saveItem(context, results.getNext().getMailItem(), fldrs, cnts, false, aos, encoder, names);
}
IOUtil.closeQuietly(results);
results = null;
} catch (Exception e) {
warn(e);
} finally {
IOUtil.closeQuietly(results);
}
}
}
if (aos == null) {
if (emptyname == null) {
context.resp.setHeader("Content-Disposition", null);
throw new UserServletException(HttpServletResponse.SC_NO_CONTENT, "No data found");
}
context.resp.setHeader("Content-Disposition", HttpUtil.createContentDisposition(context.req, Part.ATTACHMENT, emptyname));
aos = getOutputStream(context, UTF8);
}
} finally {
if (maintenance != null) {
MailboxManager.getInstance().endMaintenance(maintenance, true, true);
}
if (aos != null) {
try {
aos.close();
} catch (Exception e) {
}
}
}
}
use of com.zimbra.cs.service.UserServletContext in project zm-mailbox by Zimbra.
the class JWTBasedAuthTest method testUserServletJWTFlowWithAuthHeader.
@Test
public void testUserServletJWTFlowWithAuthHeader() {
try {
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
UserServletContext context = mockServletConext(acct, false);
UserServletUtil.getAccount(context);
Assert.assertNotNull(context.authToken);
Assert.assertEquals(acct.getId(), context.authToken.getAccountId());
} catch (ServiceException | AuthTokenException | IOException | ServletException | UserServletException e1) {
e1.printStackTrace();
Assert.fail("testUserServletJWTFlow failed");
}
}
Aggregations