Search in sources :

Example 1 with PreAuth

use of com.zimbra.soap.account.type.PreAuth in project zm-mailbox by Zimbra.

the class TestAuthentication method testAuthViaPreauthToken.

/**
     * test auth request with preauth in SOAP instead of login/password
     * @throws Exception
     */
public void testAuthViaPreauthToken() throws Exception {
    long timestamp = System.currentTimeMillis();
    long expires = timestamp + 60000;
    String domainPreAuthKey = setUpAndReturnDomainAuthKey();
    Account a = TestUtil.getAccount(USER_NAME);
    AccountSelector acctSel = new AccountSelector(com.zimbra.soap.type.AccountBy.name, a.getName());
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
    AuthRequest req = new AuthRequest(acctSel);
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("account", a.getName());
    params.put("by", "name");
    params.put("timestamp", timestamp + "");
    params.put("expires", expires + "");
    PreAuth preAuth = new PreAuth().setExpires(expires).setTimestamp(timestamp).setValue(PreAuthKey.computePreAuth(params, domainPreAuthKey));
    req = req.setPreauth(preAuth);
    Element resp = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
    AuthResponse authResp = JaxbUtil.elementToJaxb(resp);
    assertTrue("Lifetime is invalid", authResp.getLifetime() < expires - timestamp);
    String newAuthToken = authResp.getAuthToken();
    assertNotNull("should have received a new authtoken", newAuthToken);
    assertTrue("should have a received a non-empty authtoken", newAuthToken.length() > 0);
    AuthToken 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 : Account(com.zimbra.cs.account.Account) AuthRequest(com.zimbra.soap.account.message.AuthRequest) PreAuth(com.zimbra.soap.account.type.PreAuth) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) AccountSelector(com.zimbra.soap.type.AccountSelector) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthToken(com.zimbra.cs.account.AuthToken) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) AuthResponse(com.zimbra.soap.account.message.AuthResponse)

Example 2 with PreAuth

use of com.zimbra.soap.account.type.PreAuth in project zm-mailbox by Zimbra.

the class AuthRequestTest method testBuildAuthRequestWithPreAuth.

@Test
public void testBuildAuthRequestWithPreAuth() {
    AuthRequest authRequest = new AuthRequest();
    authRequest.setAccount(AccountSelector.fromName(username));
    PreAuth preAuth = new PreAuth().setExpires(expires).setTimestamp(timestamp);
    authRequest.setPreauth(preAuth);
    try {
        Element element = JaxbUtil.jaxbToElement(authRequest);
        String xml = element.toString();
        Element account = element.getElement("account");
        assertEquals("Username embedded in request is incorrect", username, account.getText());
        Element preauth = element.getElement("preauth");
        assertEquals("'expires' embedded in preauth is incorrect", Long.toString(expires), preauth.getAttribute("expires"));
        assertEquals("'timestamp' embedded in preauth is incorrect", Long.toString(timestamp), preauth.getAttribute("timestamp"));
    } catch (ServiceException e) {
        fail("Encountered a problem: " + e);
    }
}
Also used : AuthRequest(com.zimbra.soap.account.message.AuthRequest) PreAuth(com.zimbra.soap.account.type.PreAuth) ServiceException(com.zimbra.common.service.ServiceException) Element(com.zimbra.common.soap.Element) Test(org.junit.Test)

Aggregations

Element (com.zimbra.common.soap.Element)2 AuthRequest (com.zimbra.soap.account.message.AuthRequest)2 PreAuth (com.zimbra.soap.account.type.PreAuth)2 ServiceException (com.zimbra.common.service.ServiceException)1 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)1 Account (com.zimbra.cs.account.Account)1 AuthToken (com.zimbra.cs.account.AuthToken)1 ZimbraAuthToken (com.zimbra.cs.account.ZimbraAuthToken)1 AuthResponse (com.zimbra.soap.account.message.AuthResponse)1 AccountSelector (com.zimbra.soap.type.AccountSelector)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1