use of com.nike.cerberus.domain.CerberusAuthToken in project cerberus by Nike-Inc.
the class DatabaseTokenAuthenticationProcessingFilterTest method testExtractCerberusPrincipalFromRequest.
@Test
public void testExtractCerberusPrincipalFromRequest() {
CerberusAuthToken cerberusAuthToken1 = CerberusAuthToken.Builder.create().withPrincipal("principal").build();
Optional<CerberusAuthToken> cerberusAuthToken = Optional.of(cerberusAuthToken1);
Mockito.when(authTokenService.getCerberusAuthToken(anyString())).thenReturn(cerberusAuthToken);
Mockito.when(request.getHeader(HEADER_X_CERBERUS_TOKEN)).thenReturn("token");
assertNotNull(databaseTokenAuthenticationProcessingFilter.extractCerberusPrincipalFromRequest(request));
}
use of com.nike.cerberus.domain.CerberusAuthToken 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());
}
use of com.nike.cerberus.domain.CerberusAuthToken in project cerberus by Nike-Inc.
the class AuthTokenServiceTest method test_that_when_a_token_is_expired_empty_is_returned_jwt.
@Test
public void test_that_when_a_token_is_expired_empty_is_returned_jwt() {
final String tokenId = "abc.123.def";
when(jwtService.isJwt(tokenId)).thenReturn(true);
when(jwtService.parseAndValidateToken(tokenId)).thenReturn(Optional.of(new CerberusJwtClaims().setExpiresTs(OffsetDateTime.now().minusHours(1))));
Optional<CerberusAuthToken> tokenOptional = authTokenService.getCerberusAuthToken(tokenId);
assertTrue("optional should be empty", !tokenOptional.isPresent());
}
use of com.nike.cerberus.domain.CerberusAuthToken in project cerberus by Nike-Inc.
the class AuditableEventContextTest method testCheckAuthTokenIsEmptyIfPrincipleIsInstanceOfCerberusAuthToken.
@Test
public void testCheckAuthTokenIsEmptyIfPrincipleIsInstanceOfCerberusAuthToken() {
CerberusAuthToken cerberusAuthToken = CerberusAuthToken.Builder.create().build();
auditableEventContext.setPrincipal(cerberusAuthToken);
Optional<CerberusAuthToken> principalAsCerberusPrincipal = auditableEventContext.getPrincipalAsCerberusPrincipal();
Assert.assertTrue(principalAsCerberusPrincipal.isPresent());
Assert.assertSame(cerberusAuthToken, principalAsCerberusPrincipal.get());
}
use of com.nike.cerberus.domain.CerberusAuthToken in project cerberus by Nike-Inc.
the class AuditableEventContextTest method testGetPrincipalNameIfPrincipleIsInstanceOfCerberusAuthToken.
@Test
public void testGetPrincipalNameIfPrincipleIsInstanceOfCerberusAuthToken() {
String cerberusPrinciple = "cerberusPrinciple";
CerberusAuthToken cerberusAuthToken = CerberusAuthToken.Builder.create().withPrincipal(cerberusPrinciple).build();
auditableEventContext.setPrincipal(cerberusAuthToken);
String principalName = auditableEventContext.getPrincipalName();
Assert.assertEquals(cerberusPrinciple, principalName);
}
Aggregations