Search in sources :

Example 6 with CerberusAuthToken

use of com.nike.cerberus.domain.CerberusAuthToken in project cerberus by Nike-Inc.

the class AuthenticationServiceTest method tests_that_refreshUserToken_refreshes_token_when_count_is_less_than_limit.

@Test
public void tests_that_refreshUserToken_refreshes_token_when_count_is_less_than_limit() {
    Integer curCount = MAX_LIMIT - 1;
    CerberusAuthToken authToken = CerberusAuthToken.Builder.create().withPrincipalType(PrincipalType.USER).withPrincipal("principal").withGroups("group1,group2").withRefreshCount(curCount).withToken(UUID.randomUUID().toString()).build();
    CerberusPrincipal principal = new CerberusPrincipal(authToken);
    OffsetDateTime now = OffsetDateTime.now();
    when(authTokenService.generateToken(anyString(), any(PrincipalType.class), anyBoolean(), anyString(), anyInt(), anyInt())).thenReturn(CerberusAuthToken.Builder.create().withPrincipalType(PrincipalType.USER).withPrincipal("principal").withGroups("group1,group2").withRefreshCount(curCount + 1).withToken(UUID.randomUUID().toString()).withCreated(now).withExpires(now.plusHours(1)).build());
    AuthResponse response = authenticationService.refreshUserToken(principal);
    assertEquals(curCount + 1, Integer.parseInt(response.getData().getClientToken().getMetadata().get(CerberusPrincipal.METADATA_KEY_TOKEN_REFRESH_COUNT)));
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) OffsetDateTime(java.time.OffsetDateTime) PrincipalType(com.nike.cerberus.PrincipalType) CerberusPrincipal(com.nike.cerberus.security.CerberusPrincipal) AuthResponse(com.nike.cerberus.auth.connector.AuthResponse) Test(org.junit.Test)

Example 7 with CerberusAuthToken

use of com.nike.cerberus.domain.CerberusAuthToken in project cerberus by Nike-Inc.

the class DatabaseTokenAuthenticationProcessingFilterTest method testExtractCerberusPrincipalFromRequestWithAuthToken.

@Test
public void testExtractCerberusPrincipalFromRequestWithAuthToken() {
    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(LEGACY_AUTH_TOKN_HEADER)).thenReturn("token");
    assertNotNull(databaseTokenAuthenticationProcessingFilter.extractCerberusPrincipalFromRequest(request));
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) Test(org.junit.Test)

Example 8 with CerberusAuthToken

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_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 9 with CerberusAuthToken

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_jwt.

@Test
public void test_that_when_a_valid_non_expired_token_record_is_present_the_optional_is_populated_with_valid_token_object_jwt() {
    String id = UUID.randomUUID().toString();
    String tokenId = "abc.123.def";
    OffsetDateTime now = OffsetDateTime.now();
    String principal = "test-user@domain.com";
    String groups = "group1,group2,group3";
    when(jwtService.isJwt(tokenId)).thenReturn(true);
    when(jwtService.parseAndValidateToken(tokenId)).thenReturn(Optional.of(new CerberusJwtClaims().setId(id).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) CerberusJwtClaims(com.nike.cerberus.jwt.CerberusJwtClaims) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test)

Example 10 with CerberusAuthToken

use of com.nike.cerberus.domain.CerberusAuthToken in project cerberus by Nike-Inc.

the class AuthTokenServiceTest method test_that_generateToken_attempts_to_write_a_hashed_record_and_returns_proper_object_with_unhashed_token.

@Test
public void test_that_generateToken_attempts_to_write_a_hashed_record_and_returns_proper_object_with_unhashed_token() {
    String id = UUID.randomUUID().toString();
    String expectedTokenId = "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(tokenFlag.getIssueType()).thenReturn(AuthTokenIssueType.SESSION);
    when(uuidSupplier.get()).thenReturn(id);
    when(authTokenGenerator.generateSecureToken()).thenReturn(expectedTokenId);
    when(dateTimeSupplier.get()).thenReturn(now);
    when(tokenHasher.hashToken(expectedTokenId)).thenReturn(fakeHash);
    CerberusAuthToken token = authTokenService.generateToken(principal, PrincipalType.USER, false, groups, 5, 0);
    assertEquals("The token should have the un-hashed value returned", expectedTokenId, token.getToken());
    assertEquals("The token should have a created date of now", now, token.getCreated());
    assertEquals("The token should expire ttl minutes after now", now.plusMinutes(5), token.getExpires());
    assertEquals("The token should have the proper principal", principal, token.getPrincipal());
    assertEquals("The token should be the principal type that was passed in", PrincipalType.USER, token.getPrincipalType());
    assertEquals("The token should not have access to admin endpoints", false, token.isAdmin());
    assertEquals("The token should have the groups that where passed in", groups, token.getGroups());
    assertEquals("The newly created token should have a refresh count of 0", 0, token.getRefreshCount());
    verify(authTokenDao).createAuthToken(argThat(new ArgumentMatcher<AuthTokenRecord>() {

        @Override
        public boolean matches(Object argument) {
            return ((AuthTokenRecord) argument).getTokenHash().equals(fakeHash);
        }
    }));
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) OffsetDateTime(java.time.OffsetDateTime) ArgumentMatcher(org.mockito.ArgumentMatcher) Test(org.junit.Test)

Aggregations

CerberusAuthToken (com.nike.cerberus.domain.CerberusAuthToken)12 Test (org.junit.Test)11 OffsetDateTime (java.time.OffsetDateTime)5 CerberusJwtClaims (com.nike.cerberus.jwt.CerberusJwtClaims)2 AuthTokenRecord (com.nike.cerberus.record.AuthTokenRecord)2 AssertionFailedError (junit.framework.AssertionFailedError)2 PrincipalType (com.nike.cerberus.PrincipalType)1 AuthResponse (com.nike.cerberus.auth.connector.AuthResponse)1 AuthTokenResponse (com.nike.cerberus.domain.AuthTokenResponse)1 CerberusPrincipal (com.nike.cerberus.security.CerberusPrincipal)1 Period (org.joda.time.Period)1 PeriodFormatter (org.joda.time.format.PeriodFormatter)1 PeriodFormatterBuilder (org.joda.time.format.PeriodFormatterBuilder)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1