Search in sources :

Example 1 with CerberusAuthToken

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));
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) Test(org.junit.Test)

Example 2 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_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 3 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_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());
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) CerberusJwtClaims(com.nike.cerberus.jwt.CerberusJwtClaims) Test(org.junit.Test)

Example 4 with CerberusAuthToken

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());
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) Test(org.junit.Test)

Example 5 with CerberusAuthToken

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);
}
Also used : CerberusAuthToken(com.nike.cerberus.domain.CerberusAuthToken) 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