Search in sources :

Example 61 with AuthToken

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

the class Auth method needTwoFactorAuth.

private Element needTwoFactorAuth(Account account, TwoFactorAuth auth, ZimbraSoapContext zsc) throws ServiceException {
    /* two cases here:
         * 1) the user needs to provide a two-factor code.
         *    in this case, the server returns a two-factor auth token in the response header that the client
         *    must send back, along with the code, in order to finish the authentication process.
         * 2) the user needs to set up two-factor auth.
         *    this can happen if it's required for the account but the user hasn't received a secret yet.
         */
    if (!auth.twoFactorAuthEnabled()) {
        throw AccountServiceException.TWO_FACTOR_SETUP_REQUIRED();
    } else {
        Element response = zsc.createElement(AccountConstants.AUTH_RESPONSE);
        AuthToken twoFactorToken = AuthProvider.getAuthToken(account, Usage.TWO_FACTOR_AUTH);
        response.addUniqueElement(AccountConstants.E_TWO_FACTOR_AUTH_REQUIRED).setText("true");
        response.addAttribute(AccountConstants.E_LIFETIME, twoFactorToken.getExpires() - System.currentTimeMillis(), Element.Disposition.CONTENT);
        twoFactorToken.encodeAuthResp(response, false);
        response.addUniqueElement(AccountConstants.E_TRUSTED_DEVICES_ENABLED).setText(account.isFeatureTrustedDevicesEnabled() ? "true" : "false");
        return response;
    }
}
Also used : Element(com.zimbra.common.soap.Element) AuthToken(com.zimbra.cs.account.AuthToken)

Example 62 with AuthToken

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

the class CsrfUtilTest method testDecodeValidCsrfToken.

@Test
public final void testDecodeValidCsrfToken() {
    try {
        Account acct = Provisioning.getInstance().getAccountByName("test@zimbra.com");
        AuthToken authToken = new ZimbraAuthToken(acct);
        String csrfToken = CsrfUtil.generateCsrfToken(acct.getId(), AUTH_TOKEN_EXPR, CSRFTOKEN_SALT, authToken);
        Pair<String, String> tokenParts = CsrfUtil.parseCsrfToken(csrfToken);
        assertNotNull(tokenParts.getFirst());
        assertNotNull(tokenParts.getSecond());
        assertEquals("0", tokenParts.getSecond());
    } catch (ServiceException | AuthTokenException e) {
        fail("Should not throw exception.");
    }
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthTokenException(com.zimbra.cs.account.AuthTokenException) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthToken(com.zimbra.cs.account.AuthToken) Test(org.junit.Test)

Example 63 with AuthToken

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

the class GetDomainInfo method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext lc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    AuthToken at = lc.getAuthToken();
    boolean hasAuth = at != null;
    boolean applyConfig = request.getAttributeBool(AdminConstants.A_APPLY_CONFIG, true);
    Element d = request.getElement(AdminConstants.E_DOMAIN);
    String key = d.getAttribute(AdminConstants.A_BY);
    String value = d.getText();
    Key.DomainBy domainBy = Key.DomainBy.fromString(key);
    Domain domain = prov.getDomain(domainBy, value, true);
    Element response = lc.createElement(AdminConstants.GET_DOMAIN_INFO_RESPONSE);
    if (domain == null && domainBy != Key.DomainBy.name && domainBy != Key.DomainBy.virtualHostname) {
        // domain not found, and we don't have info for walking up sub domains
        // return attributes on global config
        toXML(response, prov.getConfig(), applyConfig, hasAuth);
    } else {
        if (domain == null) {
            if (domainBy == Key.DomainBy.virtualHostname)
                domain = prov.getDomain(Key.DomainBy.name, value, true);
            if (domain == null)
                domain = findDomain(prov, value);
        }
        if (domain != null)
            toXML(response, domain, applyConfig, hasAuth);
        else
            toXML(response, prov.getConfig(), applyConfig, hasAuth);
    }
    return response;
}
Also used : ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) AuthToken(com.zimbra.cs.account.AuthToken) Domain(com.zimbra.cs.account.Domain) ZAttrProvisioning(com.zimbra.common.account.ZAttrProvisioning) Provisioning(com.zimbra.cs.account.Provisioning) Key(com.zimbra.common.account.Key)

Example 64 with AuthToken

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

the class SendInviteReply method getRemoteZMailbox.

private static ZMailbox getRemoteZMailbox(OperationContext octxt, Account authAcct, Account targetAcct) throws ServiceException {
    AuthToken authToken = null;
    if (octxt != null)
        authToken = AuthToken.getCsrfUnsecuredAuthToken(octxt.getAuthToken());
    if (authToken == null)
        authToken = AuthProvider.getAuthToken(authAcct);
    String pxyAuthToken = authToken.getProxyAuthToken();
    ZAuthToken zat = pxyAuthToken == null ? authToken.toZAuthToken() : new ZAuthToken(pxyAuthToken);
    ZMailbox.Options zoptions = new ZMailbox.Options(zat, AccountUtil.getSoapUri(targetAcct));
    zoptions.setNoSession(true);
    zoptions.setTargetAccount(targetAcct.getId());
    zoptions.setTargetAccountBy(Key.AccountBy.id);
    return ZMailbox.getMailbox(zoptions);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) AuthToken(com.zimbra.cs.account.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken)

Example 65 with AuthToken

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

the class TestAuthentication method testAdminAuthViaSOAPToken.

/**
     * test admin auth request with authtoken in SOAP instead of login/password
     * @throws Exception
     */
public void testAdminAuthViaSOAPToken() throws Exception {
    AuthToken at = AuthProvider.getAdminAuthToken();
    SoapTransport transport = TestUtil.getAdminSoapTransport();
    com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest();
    req.setAuthToken(at.getEncoded());
    Element resp = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
    com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(resp);
    String newAuthToken = authResp.getAuthToken();
    assertNotNull("should have received a new authtoken", newAuthToken);
    at = ZimbraAuthToken.getAuthToken(newAuthToken);
    assertTrue("new auth token should be registered", at.isRegistered());
    assertFalse("new auth token should not be expired yet", at.isExpired());
}
Also used : AuthRequest(com.zimbra.soap.account.message.AuthRequest) Element(com.zimbra.common.soap.Element) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthToken(com.zimbra.cs.account.AuthToken) SoapTransport(com.zimbra.common.soap.SoapTransport)

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