Search in sources :

Example 1 with AuthTokenRecord

use of com.nike.cerberus.record.AuthTokenRecord in project cerberus by Nike-Inc.

the class AuthTokenServiceTest method test_that_when_a_valid_non_expired_token_record_is_present_the_optional_is_populated_with_valid_token_object_session.

@Test
public void test_that_when_a_valid_non_expired_token_record_is_present_the_optional_is_populated_with_valid_token_object_session() {
    String id = UUID.randomUUID().toString();
    String tokenId = "abc-123-def-456";
    OffsetDateTime now = OffsetDateTime.now();
    final String fakeHash = "kjadlkfjasdlkf;jlkj1243asdfasdf";
    String principal = "test-user@domain.com";
    String groups = "group1,group2,group3";
    when(tokenHasher.hashToken(tokenId)).thenReturn(fakeHash);
    when(authTokenDao.getAuthTokenFromHash(fakeHash)).thenReturn(Optional.of(new AuthTokenRecord().setId(id).setTokenHash(fakeHash).setCreatedTs(now).setExpiresTs(now.plusHours(1)).setPrincipal(principal).setPrincipalType(PrincipalType.USER.getName()).setIsAdmin(false).setGroups(groups).setRefreshCount(0)));
    Optional<CerberusAuthToken> tokenOptional = authTokenService.getCerberusAuthToken(tokenId);
    CerberusAuthToken token = tokenOptional.orElseThrow(() -> new AssertionFailedError("Token should be present"));
    assertEquals(tokenId, token.getToken());
    assertEquals(now, token.getCreated());
    assertEquals(now.plusHours(1), token.getExpires());
    assertEquals(principal, token.getPrincipal());
    assertEquals(PrincipalType.USER, token.getPrincipalType());
    assertEquals(false, token.isAdmin());
    assertEquals(groups, token.getGroups());
    assertEquals(0, token.getRefreshCount());
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) OffsetDateTime(java.time.OffsetDateTime) AuthTokenRecord(com.nike.cerberus.record.AuthTokenRecord) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test)

Example 2 with AuthTokenRecord

use of com.nike.cerberus.record.AuthTokenRecord in project cerberus by Nike-Inc.

the class AuthTokenDaoTest method testCreteToken.

@Test
public void testCreteToken() {
    AuthTokenRecord authTokenRecord = Mockito.mock(AuthTokenRecord.class);
    authTokenDao.createAuthToken(authTokenRecord);
    Mockito.verify(authTokenMapper).createAuthToken(authTokenRecord);
}
Also used : AuthTokenRecord(com.nike.cerberus.record.AuthTokenRecord) Test(org.junit.Test)

Example 3 with AuthTokenRecord

use of com.nike.cerberus.record.AuthTokenRecord in project cerberus by Nike-Inc.

the class AuthTokenDaoTest method testGetAuthTokenFromHash.

@Test
public void testGetAuthTokenFromHash() {
    AuthTokenRecord authTokenRecord = Mockito.mock(AuthTokenRecord.class);
    Mockito.when(authTokenMapper.getAuthTokenFromHash("hash")).thenReturn(authTokenRecord);
    Optional<AuthTokenRecord> optionalAuthTokenRecord = authTokenDao.getAuthTokenFromHash("hash");
    Assert.assertEquals(authTokenRecord, optionalAuthTokenRecord.get());
}
Also used : AuthTokenRecord(com.nike.cerberus.record.AuthTokenRecord) Test(org.junit.Test)

Example 4 with AuthTokenRecord

use of com.nike.cerberus.record.AuthTokenRecord in project cerberus by Nike-Inc.

the class AuthTokenServiceTest method test_that_when_a_token_is_expired_empty_is_returned_session.

@Test
public void test_that_when_a_token_is_expired_empty_is_returned_session() {
    final String tokenId = "abc-123-def-456";
    final String fakeHash = "kjadlkfjasdlkf;jlkj1243asdfasdf";
    when(tokenHasher.hashToken(tokenId)).thenReturn(fakeHash);
    when(authTokenDao.getAuthTokenFromHash(fakeHash)).thenReturn(Optional.of(new AuthTokenRecord().setExpiresTs(OffsetDateTime.now().minusHours(1))));
    Optional<CerberusAuthToken> tokenOptional = authTokenService.getCerberusAuthToken(tokenId);
    assertTrue("optional should be empty", !tokenOptional.isPresent());
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) AuthTokenRecord(com.nike.cerberus.record.AuthTokenRecord) Test(org.junit.Test)

Example 5 with AuthTokenRecord

use of com.nike.cerberus.record.AuthTokenRecord in project cerberus by Nike-Inc.

the class AuthTokenService method getCerberusAuthTokenFromSession.

private CerberusAuthToken getCerberusAuthTokenFromSession(String principal, PrincipalType principalType, boolean isAdmin, String groups, long ttlInMinutes, int refreshCount, String id, OffsetDateTime now) {
    String token = authTokenGenerator.generateSecureToken();
    AuthTokenRecord authTokenRecord = new AuthTokenRecord().setId(id).setTokenHash(tokenHasher.hashToken(token)).setCreatedTs(now).setExpiresTs(now.plusMinutes(ttlInMinutes)).setPrincipal(principal).setPrincipalType(principalType.getName()).setIsAdmin(isAdmin).setGroups(groups).setRefreshCount(refreshCount);
    authTokenDao.createAuthToken(authTokenRecord);
    return getCerberusAuthTokenFromRecord(token, authTokenRecord);
}
Also used : AuthTokenRecord(com.nike.cerberus.record.AuthTokenRecord)

Aggregations

AuthTokenRecord (com.nike.cerberus.record.AuthTokenRecord)5 Test (org.junit.Test)4 CerberusAuthToken (com.nike.cerberus.domain.CerberusAuthToken)2 OffsetDateTime (java.time.OffsetDateTime)1 AssertionFailedError (junit.framework.AssertionFailedError)1