use of com.zimbra.cs.account.ZimbraAuthToken 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.");
}
}
use of com.zimbra.cs.account.ZimbraAuthToken in project zm-mailbox by Zimbra.
the class OAuthServiceProvider method markAsAuthorized.
/**
* Mark OAuth consumer as authorized and update accessor properties.
*/
public static synchronized void markAsAuthorized(OAuthAccessor accessor, String userId, String zauthtoken) throws OAuthException {
accessor.setProperty("user", userId);
accessor.setProperty("authorized", Boolean.TRUE);
accessor.setProperty("ZM_AUTH_TOKEN", zauthtoken);
AuthToken zimbraAuthToken;
try {
zimbraAuthToken = ZimbraAuthToken.getAuthToken(zauthtoken);
final Account account = zimbraAuthToken.getAccount();
setAccountPropertiesForAccessor(account, accessor);
} catch (AuthTokenException | UnsupportedEncodingException | ServiceException e) {
throw new OAuthException(e);
}
accessor.consumer.setProperty("approved_on", Long.toString(System.currentTimeMillis()));
}
use of com.zimbra.cs.account.ZimbraAuthToken in project zm-mailbox by Zimbra.
the class OAuthAccessorSerializer method deserialize.
@Override
public OAuthAccessor deserialize(Object obj) throws ServiceException {
String value = (String) obj;
LOG.debug("get value: " + value);
String consumer_key = value.substring(0, value.indexOf(",token_secret")).substring(13);
String token_secret = value.substring(value.indexOf(",token_secret"), value.indexOf(",callback")).substring(14);
String callback = value.substring(value.indexOf(",callback"), value.indexOf(",user")).substring(10);
String user = value.substring(value.indexOf(",user"), value.indexOf(",authorized")).substring(6);
String authorized = value.substring(value.indexOf(",authorized"), value.indexOf(",zauthtoken")).substring(12);
String zauthtoken = value.substring(value.indexOf(",zauthtoken"), value.indexOf(",verifier")).substring(12);
String verifier = value.substring(value.indexOf(",verifier"), value.indexOf(",approved_on")).substring(10);
String approved_on = value.substring(value.indexOf(",approved_on"), value.indexOf(",device")).substring(13);
String device = value.substring(value.indexOf(",device")).substring(8);
LOG.debug("[consumer_key:%s, callback:%s, user:%s, authorized:%s, zauthtoken:%s, verifier:%s, approved_on:%s, device:%s]", consumer_key, callback, user, authorized, zauthtoken, verifier, approved_on, device);
try {
OAuthConsumer consumer = OAuthServiceProvider.getConsumer(consumer_key);
OAuthAccessor accessor = new OAuthAccessor(consumer);
accessor.tokenSecret = token_secret;
accessor.setProperty(OAuth.OAUTH_CALLBACK, callback);
if (!user.equals("null")) {
accessor.setProperty("user", user);
}
if (authorized.equalsIgnoreCase(Boolean.FALSE.toString())) {
accessor.setProperty("authorized", Boolean.FALSE);
} else if (authorized.equalsIgnoreCase(Boolean.TRUE.toString())) {
accessor.setProperty("authorized", Boolean.TRUE);
}
if (!zauthtoken.equals("null")) {
accessor.setProperty("ZM_AUTH_TOKEN", zauthtoken);
AuthToken zimbraAuthToken = ZimbraAuthToken.getAuthToken(zauthtoken);
final Account account = zimbraAuthToken.getAccount();
OAuthServiceProvider.setAccountPropertiesForAccessor(account, accessor);
}
if (!verifier.equals("null")) {
accessor.setProperty(OAuth.OAUTH_VERIFIER, verifier);
}
if (null != approved_on) {
accessor.consumer.setProperty("approved_on", approved_on);
}
if (null != device) {
accessor.consumer.setProperty("device", device);
}
return accessor;
} catch (Exception e) {
//need more hack here for hadnling IOException properly
throw ServiceException.FAILURE("IOException", e);
}
}
use of com.zimbra.cs.account.ZimbraAuthToken in project zm-mailbox by Zimbra.
the class TestCookieReuse method testTokenDeregistration.
/**
* test de-registering an authtoken
* @throws Exception
*/
@Test
public void testTokenDeregistration() throws Exception {
Account a = TestUtil.getAccount(USER_NAME);
ZimbraAuthToken at = new ZimbraAuthToken(a);
Assert.assertTrue("token should be registered", at.isRegistered());
at.deRegister();
Assert.assertFalse("token should not be registered", at.isRegistered());
}
use of com.zimbra.cs.account.ZimbraAuthToken in project zm-mailbox by Zimbra.
the class TestCookieReuse method testOldClientSupport.
/**
* Test old behavior: tokens appear to be registered even when they are not registered when lowest
* supported auth version is set to 1
* @throws Exception
*/
@Test
public void testOldClientSupport() throws Exception {
Account a = TestUtil.getAccount(USER_NAME);
ZimbraAuthToken at = new ZimbraAuthToken(a, System.currentTimeMillis() - 1000);
Assert.assertTrue("token should be registered", at.isRegistered());
at.deRegister();
Assert.assertFalse("token should not be registered", at.isRegistered());
//lowering supported auth version should allow unregistered cookies
Provisioning.getInstance().getLocalServer().setLowestSupportedAuthVersion(1);
Assert.assertTrue("token should appear to be registered", at.isRegistered());
//raising supported auth version should not allow unregistered cookies
Provisioning.getInstance().getLocalServer().setLowestSupportedAuthVersion(2);
Assert.assertFalse("token should not be registered", at.isRegistered());
}
Aggregations