Search in sources :

Example 51 with AuthToken

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

the class AuthProvider method getAuthToken.

/**
     * Creates an AuthToken object from token string.
     *
     * @param encoded
     * @return
     * @throws AuthTokenException
     * @see #authToken(String)
     */
public static AuthToken getAuthToken(String encoded) throws AuthTokenException {
    AuthToken at = null;
    List<AuthProvider> providers = getProviders();
    AuthTokenException authTokenExp = null;
    for (AuthProvider ap : providers) {
        try {
            at = ap.authToken(encoded);
            if (at == null) {
                authTokenExp = new AuthTokenException("auth provider " + ap.getName() + " returned null");
            } else {
                return at;
            }
        } catch (AuthProviderException e) {
            // if there is no auth data for this provider, log and continue with next provider
            if (e.canIgnore()) {
                logger().warn(ap.getName() + ":" + e.getMessage());
            } else {
                authTokenExp = new AuthTokenException("auth provider error", e);
            }
        } catch (AuthTokenException e) {
            //log and store exception reference
            authTokenExp = e;
            logger().debug("getAuthToken error: provider=" + ap.getName() + ", err=" + e.getMessage(), e);
        }
    }
    //If multiple auth providers caused AuthTokenException, then last exception is rethrown from here.
    if (null != authTokenExp) {
        throw authTokenExp;
    }
    // there is no auth data for any of the enabled providers
    logger().error("unable to get AuthToken from encoded " + encoded);
    return null;
}
Also used : AuthTokenException(com.zimbra.cs.account.AuthTokenException) AuthToken(com.zimbra.cs.account.AuthToken)

Example 52 with AuthToken

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

the class AuthProvider method getAuthToken.

public static AuthToken getAuthToken(Account account, Usage usage) throws AuthProviderException {
    List<AuthProvider> providers = getProviders();
    AuthProviderException authProviderExp = null;
    for (AuthProvider ap : providers) {
        try {
            AuthToken at = ap.authToken(account, usage);
            if (at == null) {
                authProviderExp = AuthProviderException.FAILURE("auth provider " + ap.getName() + " returned null");
            } else {
                return at;
            }
        } catch (AuthProviderException e) {
            if (e.canIgnore()) {
                logger().debug(ap.getName() + ":" + e.getMessage());
            } else {
                authProviderExp = e;
            }
        }
    }
    if (null != authProviderExp) {
        throw authProviderExp;
    }
    throw AuthProviderException.FAILURE("cannot get authtoken from account " + account.getName());
}
Also used : AuthToken(com.zimbra.cs.account.AuthToken)

Example 53 with AuthToken

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

the class AuthProvider method getAuthToken.

public static AuthToken getAuthToken(Account acct, boolean isAdmin, AuthMech authMech) throws AuthProviderException {
    List<AuthProvider> providers = getProviders();
    AuthProviderException authProviderExp = null;
    for (AuthProvider ap : providers) {
        try {
            AuthToken at = ap.authToken(acct, isAdmin, authMech);
            if (at == null) {
                authProviderExp = AuthProviderException.FAILURE("auth provider " + ap.getName() + " returned null");
            } else {
                return at;
            }
        } catch (AuthProviderException e) {
            if (e.canIgnore()) {
                logger().debug(ap.getName() + ":" + e.getMessage());
            } else {
                authProviderExp = e;
            }
        }
    }
    String acctName = acct != null ? acct.getName() : "null";
    if (null != authProviderExp) {
        throw authProviderExp;
    }
    throw AuthProviderException.FAILURE("cannot get authtoken from account " + acctName);
}
Also used : AuthToken(com.zimbra.cs.account.AuthToken)

Example 54 with AuthToken

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

the class AuthProvider method getAuthToken.

public static AuthToken getAuthToken(Account acct, long expires, boolean isAdmin, Account adminAcct) throws AuthProviderException {
    List<AuthProvider> providers = getProviders();
    AuthProviderException authProviderExp = null;
    for (AuthProvider ap : providers) {
        try {
            AuthToken at = ap.authToken(acct, expires, isAdmin, adminAcct);
            if (at == null) {
                authProviderExp = AuthProviderException.FAILURE("auth provider " + ap.getName() + " returned null");
            } else {
                return at;
            }
        } catch (AuthProviderException e) {
            if (e.canIgnore()) {
                logger().debug(ap.getName() + ":" + e.getMessage());
            } else {
                authProviderExp = e;
            }
        }
    }
    if (null != authProviderExp) {
        throw authProviderExp;
    }
    throw AuthProviderException.FAILURE("cannot get authtoken from account " + acct.getName());
}
Also used : AuthToken(com.zimbra.cs.account.AuthToken)

Example 55 with AuthToken

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

the class UserServlet method getExternalRestUrl.

/**
     * Constructs the exteral url for a mount point. This gets the link back to the correct server without need for proxying it
     * @param authToken
     * @param mpt The mount point to create the url for
     * @return The url for the mountpoint/share that goes back to the original user/share/server
     * @throws ServiceException
     */
public static String getExternalRestUrl(OperationContext octxt, Mountpoint mpt) throws ServiceException {
    AuthToken authToken = AuthToken.getCsrfUnsecuredAuthToken(octxt.getAuthToken());
    // no need to do anything
    if (mpt.isLocal()) {
        return null;
    }
    String folderPath = null;
    // Figure out the target server from the target user's account.
    // This will let us get the correct server/port
    Provisioning prov = Provisioning.getInstance();
    Account targetAccount = prov.get(AccountBy.id, mpt.getOwnerId());
    if (targetAccount == null) {
        // Remote owner account has been deleted.
        return null;
    }
    Server targetServer = prov.getServer(targetAccount);
    // Avoid the soap call if its a local mailbox
    if (Provisioning.onLocalServer(targetAccount)) {
        Mailbox mailbox = MailboxManager.getInstance().getMailboxByAccountId(targetAccount.getId());
        if (mailbox == null) {
            // no mailbox (shouldn't happen normally)
            return null;
        }
        // Get the folder from the mailbox
        Folder folder = mailbox.getFolderById(octxt, mpt.getRemoteId());
        if (folder == null) {
            return null;
        }
        folderPath = folder.getPath();
    } else {
        // The remote server case
        // Get the target user's mailbox..
        ZMailbox.Options zoptions = new ZMailbox.Options(authToken.toZAuthToken(), AccountUtil.getSoapUri(targetAccount));
        zoptions.setTargetAccount(mpt.getOwnerId());
        zoptions.setTargetAccountBy(AccountBy.id);
        zoptions.setNoSession(true);
        ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
        if (zmbx == null) {
            // we didn't manage to get a mailbox
            return null;
        }
        // Get an instance of their folder so we can build the path correctly
        ZFolder folder = zmbx.getFolderById(mpt.getTarget().toString(authToken.getAccount().getId()));
        // if for some reason we can't find the folder, return null
        if (folder == null) {
            return null;
        }
        folderPath = folder.getPath();
    }
    // For now we'll always use SSL
    return URLUtil.getServiceURL(targetServer, SERVLET_PATH + HttpUtil.urlEscape(getAccountPath(targetAccount) + folderPath), true);
}
Also used : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) Server(com.zimbra.cs.account.Server) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) AuthToken(com.zimbra.cs.account.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken) ZFolder(com.zimbra.client.ZFolder) Folder(com.zimbra.cs.mailbox.Folder) ZFolder(com.zimbra.client.ZFolder) Provisioning(com.zimbra.cs.account.Provisioning)

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