use of com.zimbra.common.auth.ZAuthToken in project zm-mailbox by Zimbra.
the class ZMailbox method authByAuthToken.
public ZAuthResult authByAuthToken(Options options) throws ServiceException {
if (mTransport == null) {
throw ZClientException.CLIENT_ERROR("must call setURI before calling authenticate", null);
}
AuthRequest req = new AuthRequest();
// cannot be null here
ZAuthToken zat = options.getAuthToken();
req.setAuthToken(new AuthToken(zat.getValue(), false));
req.setTwoFactorCode(options.getTwoFactorCode());
req.setRequestedSkin(options.getRequestedSkin());
req.setCsrfSupported(options.getCsrfSupported());
req.setDeviceTrusted(options.getTrustedDevice());
addAttrsAndPrefs(req, options);
AuthResponse res = invokeJaxb(req);
ZAuthResult r = new ZAuthResult(res);
r.setSessionId(mTransport.getSessionId());
return r;
}
use of com.zimbra.common.auth.ZAuthToken in project zm-mailbox by Zimbra.
the class ZMailbox method enableTwoFactorAuth.
public EnableTwoFactorAuthResponse enableTwoFactorAuth(String password, TOTPAuthenticator auth) throws ServiceException {
EnableTwoFactorAuthRequest req = new EnableTwoFactorAuthRequest();
req.setName(getName());
req.setPassword(password);
EnableTwoFactorAuthResponse resp = invokeJaxb(req);
String secret = resp.getSecret();
long timestamp = System.currentTimeMillis() / 1000;
String totp = auth.generateCode(secret, timestamp, Encoding.BASE32);
req.setTwoFactorCode(totp);
req.setAuthToken(resp.getAuthToken());
resp = invokeJaxb(req);
resp.setSecret(secret);
initAuthToken(new ZAuthToken(resp.getAuthToken().getValue()));
return resp;
}
use of com.zimbra.common.auth.ZAuthToken in project zm-mailbox by Zimbra.
the class SoapProvisioning method soapAdminAuthenticate.
/**
* used to authenticate via admin AuthRequest. can only be called after setting the URI with setURI.
*
* @param name
* @param password
* @throws ServiceException
* @throws IOException
*/
public void soapAdminAuthenticate(String name, String password) throws ServiceException {
if (mTransport == null)
throw ZClientException.CLIENT_ERROR("must call setURI before calling adminAuthenticate", null);
// TODO: Need to resolve issues with AuthToken support before we can
// migrate to JAXB?
// The ZAuthToken constructor below ends up invoking :
// ZAuthToken.fromSoap(Element eAuthToken, boolean isAdmin)
// which expects <authToken> to have a value but also optional
// <a> sub-elements. Would probably need @XmlMixed to support
// that as @XmlElement and @XmlValue are mutually exclusive
XMLElement req = new XMLElement(AdminConstants.AUTH_REQUEST);
req.addElement(AdminConstants.E_NAME).setText(name);
req.addElement(AdminConstants.E_PASSWORD).setText(password);
Element response = invoke(req);
mAuthToken = new ZAuthToken(response.getElement(AdminConstants.E_AUTH_TOKEN), true);
mAuthTokenLifetime = response.getAttributeLong(AdminConstants.E_LIFETIME);
mAuthTokenExpiration = System.currentTimeMillis() + mAuthTokenLifetime;
mTransport.setAuthToken(mAuthToken);
}
use of com.zimbra.common.auth.ZAuthToken in project zm-mailbox by Zimbra.
the class LmcMessage method downloadAttachment.
public byte[] downloadAttachment(String partNo, String baseURL, LmcSession session, String cookieDomain, int msTimeout) throws LmcSoapClientException, IOException {
// set the cookie.
if (session == null)
System.err.println(System.currentTimeMillis() + " " + Thread.currentThread() + " LmcMessage.downloadAttachment session=null");
HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
String url = baseURL + "?id=" + getID() + "&part=" + partNo;
GetMethod get = new GetMethod(url);
ZAuthToken zat = session.getAuthToken();
Map<String, String> cookieMap = zat.cookieMap(false);
if (cookieMap != null) {
HttpState initialState = new HttpState();
for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
Cookie cookie = new Cookie(cookieDomain, ck.getKey(), ck.getValue(), "/", -1, false);
initialState.addCookie(cookie);
}
client.setState(initialState);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
}
get.getParams().setSoTimeout(msTimeout);
int statusCode = -1;
try {
statusCode = HttpClientUtil.executeMethod(client, get);
// parse the response
if (statusCode == 200) {
return get.getResponseBody();
} else {
throw new LmcSoapClientException("Attachment download failed, status=" + statusCode);
}
} catch (IOException e) {
System.err.println("Attachment download failed");
e.printStackTrace();
throw e;
} finally {
get.releaseConnection();
}
}
use of com.zimbra.common.auth.ZAuthToken in project zm-mailbox by Zimbra.
the class LmcSendMsgRequest method postAttachment.
/*
* Post the attachment represented by File f and return the attachment ID
*/
public String postAttachment(String uploadURL, LmcSession session, File f, // cookie domain e.g. ".example.zimbra.com"
String domain, int msTimeout) throws LmcSoapClientException, IOException {
String aid = null;
// set the cookie.
if (session == null)
System.err.println(System.currentTimeMillis() + " " + Thread.currentThread() + " LmcSendMsgRequest.postAttachment session=null");
HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
PostMethod post = new PostMethod(uploadURL);
ZAuthToken zat = session.getAuthToken();
Map<String, String> cookieMap = zat.cookieMap(false);
if (cookieMap != null) {
HttpState initialState = new HttpState();
for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
Cookie cookie = new Cookie(domain, ck.getKey(), ck.getValue(), "/", -1, false);
initialState.addCookie(cookie);
}
client.setState(initialState);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
}
post.getParams().setSoTimeout(msTimeout);
int statusCode = -1;
try {
String contentType = URLConnection.getFileNameMap().getContentTypeFor(f.getName());
Part[] parts = { new FilePart(f.getName(), f, contentType, "UTF-8") };
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
statusCode = HttpClientUtil.executeMethod(client, post);
// parse the response
if (statusCode == 200) {
// paw through the returned HTML and get the attachment id
String response = post.getResponseBodyAsString();
//System.out.println("response is\n" + response);
int lastQuote = response.lastIndexOf("'");
int firstQuote = response.indexOf("','") + 3;
if (lastQuote == -1 || firstQuote == -1)
throw new LmcSoapClientException("Attachment post failed, unexpected response: " + response);
aid = response.substring(firstQuote, lastQuote);
} else {
throw new LmcSoapClientException("Attachment post failed, status=" + statusCode);
}
} catch (IOException e) {
System.err.println("Attachment post failed");
e.printStackTrace();
throw e;
} finally {
post.releaseConnection();
}
return aid;
}
Aggregations