Search in sources :

Example 1 with ZAuthToken

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;
}
Also used : EnableTwoFactorAuthRequest(com.zimbra.soap.account.message.EnableTwoFactorAuthRequest) DisableTwoFactorAuthRequest(com.zimbra.soap.account.message.DisableTwoFactorAuthRequest) AuthRequest(com.zimbra.soap.account.message.AuthRequest) AuthToken(com.zimbra.soap.account.type.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken) AuthResponse(com.zimbra.soap.account.message.AuthResponse) EnableTwoFactorAuthResponse(com.zimbra.soap.account.message.EnableTwoFactorAuthResponse) DisableTwoFactorAuthResponse(com.zimbra.soap.account.message.DisableTwoFactorAuthResponse)

Example 2 with ZAuthToken

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;
}
Also used : EnableTwoFactorAuthRequest(com.zimbra.soap.account.message.EnableTwoFactorAuthRequest) EnableTwoFactorAuthResponse(com.zimbra.soap.account.message.EnableTwoFactorAuthResponse) ZAuthToken(com.zimbra.common.auth.ZAuthToken)

Example 3 with ZAuthToken

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);
}
Also used : Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) XMLElement(com.zimbra.common.soap.Element.XMLElement) ZAuthToken(com.zimbra.common.auth.ZAuthToken)

Example 4 with ZAuthToken

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();
    }
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) LmcSoapClientException(com.zimbra.cs.client.soap.LmcSoapClientException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpState(org.apache.commons.httpclient.HttpState) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) Map(java.util.Map)

Example 5 with ZAuthToken

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;
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpState(org.apache.commons.httpclient.HttpState) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) HttpClient(org.apache.commons.httpclient.HttpClient) Map(java.util.Map)

Aggregations

ZAuthToken (com.zimbra.common.auth.ZAuthToken)36 ZMailbox (com.zimbra.client.ZMailbox)14 Element (com.zimbra.common.soap.Element)11 AuthToken (com.zimbra.cs.account.AuthToken)7 ZFolder (com.zimbra.client.ZFolder)6 ServiceException (com.zimbra.common.service.ServiceException)6 XMLElement (com.zimbra.common.soap.Element.XMLElement)6 Account (com.zimbra.cs.account.Account)6 IOException (java.io.IOException)6 HttpClient (org.apache.commons.httpclient.HttpClient)6 HttpState (org.apache.commons.httpclient.HttpState)6 Test (org.junit.Test)6 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)5 ItemId (com.zimbra.cs.service.util.ItemId)5 URI (java.net.URI)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Folder (com.zimbra.cs.mailbox.Folder)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)3 Options (com.zimbra.client.ZMailbox.Options)2 ZMountpoint (com.zimbra.client.ZMountpoint)2