Search in sources :

Example 1 with AuthTokenProperties

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

the class JWTBasedAuthTest method testGetJWToken.

@Test
public void testGetJWToken() {
    Account acct;
    try {
        acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        String salt = "s1";
        String salts = "s2|s3|s1";
        AuthTokenKey atkey = AuthTokenUtil.getCurrentKey();
        byte[] jwtKey = Bytes.concat(atkey.getKey(), salt.getBytes());
        long issuedAt = System.currentTimeMillis();
        long expires = issuedAt + 3600000;
        AuthTokenProperties properties = new AuthTokenProperties(acct, true, null, expires, AuthMech.zimbra, Usage.AUTH);
        String jwt = JWTUtil.generateJWT(jwtKey, salt, issuedAt, properties, atkey.getVersion());
        AuthToken at = ZimbraJWToken.getJWToken(jwt, salts);
        Assert.assertEquals(acct.getId(), at.getAccountId());
        Assert.assertEquals(Usage.AUTH, at.getUsage());
        Assert.assertEquals(expires / 1000, at.getExpires() / 1000);
        Assert.assertEquals(AuthMech.zimbra, at.getAuthMech());
        Assert.assertEquals(false, at.isAdmin());
    } catch (ServiceException | AuthTokenException e) {
        e.printStackTrace();
        Assert.fail("testGenerateAndValidateJWT failed");
    }
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) AuthTokenProperties(com.zimbra.cs.account.AuthTokenProperties) AuthTokenException(com.zimbra.cs.account.AuthTokenException) AuthToken(com.zimbra.cs.account.AuthToken) AuthTokenKey(com.zimbra.cs.account.AuthTokenKey) Test(org.junit.Test)

Example 2 with AuthTokenProperties

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

the class JWTBasedAuthTest method generateJWT.

private String generateJWT(Account acct, String salt) throws AuthFailedServiceException, AuthTokenException {
    AuthTokenKey atkey = AuthTokenUtil.getCurrentKey();
    byte[] jwtKey = Bytes.concat(atkey.getKey(), salt.getBytes());
    long issuedAt = System.currentTimeMillis();
    long expires = issuedAt + 3600000;
    AuthTokenProperties properties = new AuthTokenProperties(acct, false, null, expires, null, Usage.AUTH);
    String jwt = JWTUtil.generateJWT(jwtKey, salt, issuedAt, properties, atkey.getVersion());
    return jwt;
}
Also used : AuthTokenProperties(com.zimbra.cs.account.AuthTokenProperties) AuthTokenKey(com.zimbra.cs.account.AuthTokenKey)

Aggregations

AuthTokenKey (com.zimbra.cs.account.AuthTokenKey)2 AuthTokenProperties (com.zimbra.cs.account.AuthTokenProperties)2 ServiceException (com.zimbra.common.service.ServiceException)1 Account (com.zimbra.cs.account.Account)1 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)1 AuthToken (com.zimbra.cs.account.AuthToken)1 AuthTokenException (com.zimbra.cs.account.AuthTokenException)1 Test (org.junit.Test)1