/** * Returns {@link #getUserArn()}. * * @return name */ @Override default String getName() { return getUserArn(); } }
/** * Returns {@link #getCognitoIdentityId()}. * * @return name */ @Override default String getName() { return getCognitoIdentityId(); } }
/** * Returns the principalId. * * @return the principalId */ @Nonnull @Override default String getName() { return getClaims().getPrincipalId(); } }
@Test public void testCognitoCustomAuthorizerPrincipal() { Map<String, Object> authorizerDate = new HashMap<>(); authorizerDate.put("principalId", "123"); authorizerDate.put("custom:value", "blub"); Principal principal = testPrincipal(authorizerDate); assertTrue(principal instanceof CustomAuthorizerPrincipal); CustomAuthorizerPrincipal cognitoCustomPrincipal = (CustomAuthorizerPrincipal) principal; assertEquals("123", cognitoCustomPrincipal.getName()); assertEquals("123", cognitoCustomPrincipal.getClaims().getPrincipalId()); assertEquals("blub", cognitoCustomPrincipal.getClaims().getAllClaims().get("custom:value")); }
@Test public void testCognitoUserPoolAuthorizerPrincipal() { Map<String, Object> claims = new HashMap<>(); claims.put("sub", "123"); Map<String, Object> authorizerData = new HashMap<>(); authorizerData.put("claims", claims); Principal principal = testPrincipal(authorizerData); assertTrue(principal instanceof CognitoUserPoolAuthorizerPrincipal); CognitoUserPoolAuthorizerPrincipal cognitoUserPoolPrincipal = (CognitoUserPoolAuthorizerPrincipal) principal; assertEquals("123", cognitoUserPoolPrincipal.getName()); assertNotNull(cognitoUserPoolPrincipal.getClaims()); assertEquals("123", cognitoUserPoolPrincipal.getClaims().getSub()); assertEquals("123", cognitoUserPoolPrincipal.getClaims().getAllClaims().get("sub")); }
@Test public void createPrincipal_AccessKeyAndUserArnAndUserAndCallerGiven_ShouldCreateIamPrincipal() { GatewayRequest request = createRequestMock("accessKey", "userArn", "user", "caller"); IamPrincipal principal = (IamPrincipal) createSecurityContextFactory(request).createPrincipal(); assertEquals("accessKey", principal.getAccessKey()); assertEquals("userArn", principal.getUserArn()); assertEquals("user", principal.getUser()); assertEquals("caller", principal.getCaller()); assertEquals(principal.getUserArn(), principal.getName()); }
private static void assertCognitoUserPoolPrincipal(Principal principal) { CognitoUserPoolAuthorizerPrincipal customAuthorizerPrincipal = (CognitoUserPoolAuthorizerPrincipal) principal; assertEquals(TEST_COGNITO_USER_POOL_SUB, customAuthorizerPrincipal.getClaims().getSub()); assertEquals(TEST_COGNITO_USER_POOL_SUB, customAuthorizerPrincipal.getClaims().getAllClaims().get("sub")); }
@Test public void getName_PrincipalIdInClaimsGiven_ShouldReturnPrincipalIdFromClaims() { CustomAuthorizerPrincipal principal = mock(CustomAuthorizerPrincipal.class); CustomAuthorizerClaims claims = mock(CustomAuthorizerClaims.class); when(claims.getPrincipalId()).thenReturn("somePrincipalId"); when(principal.getClaims()).thenReturn(claims); when(principal.getName()).thenCallRealMethod(); assertEquals("somePrincipalId", principal.getName()); } }
@Test public void getName_SubInClaimsGiven_ShouldReturnSubFromClaims() { CognitoUserPoolAuthorizerPrincipal principal = mock(CognitoUserPoolAuthorizerPrincipal.class); CognitoUserPoolAuthorizerClaims claims = mock(CognitoUserPoolAuthorizerClaims.class); when(claims.getSub()).thenReturn("someSubValue"); when(principal.getClaims()).thenReturn(claims); when(principal.getName()).thenCallRealMethod(); assertEquals("someSubValue", principal.getName()); }
private static void assertCustomAuthorizerPrincipal(Principal principal) { CustomAuthorizerPrincipal customAuthorizerPrincipal = (CustomAuthorizerPrincipal) principal; assertEquals(TEST_CUSTOM_AUTHORIZER_PRINCIPAL_ID, customAuthorizerPrincipal.getClaims().getPrincipalId()); assertEquals(TEST_CUSTOM_AUTHORIZER_PRINCIPAL_ID, customAuthorizerPrincipal.getClaims().getAllClaims().get("principalId")); }
private static void assertIamPrincipal(Principal principal) { IamPrincipal iamPrincipal = (IamPrincipal) principal; assertEquals(TEST_IAM_ACCESS_KEY, iamPrincipal.getAccessKey()); assertEquals(TEST_IAM_USER, iamPrincipal.getUser()); assertEquals(TEST_IAM_USER_ARN, iamPrincipal.getUserArn()); }
@Test public void testNameReturnsCognitoIdentityId() { IamPrincipal principal = mock(IamPrincipal.class); when(principal.getName()).thenCallRealMethod(); when(principal.getUserArn()).thenReturn("userArn"); assertEquals("userArn", principal.getName()); } }
@Test public void testNameReturnsCognitoIdentityId() { CognitoIdentityPrincipal principal = mock(CognitoIdentityPrincipal.class); when(principal.getName()).thenCallRealMethod(); when(principal.getCognitoIdentityId()).thenReturn("cognitoIdentityId"); assertEquals("cognitoIdentityId", principal.getName()); }
@Test public void getName_PrincipalIdInClaimsGiven_ShouldReturnPrincipalIdFromClaims() { CognitoUserPoolAuthorizerClaims claims = mock(CognitoUserPoolAuthorizerClaims.class); when(claims.getAllClaims()).thenReturn(Collections.singletonMap("cognito:username", "someCognitoUsernameValue")); when(claims.getCognitoUserName()).thenCallRealMethod(); assertEquals("someCognitoUsernameValue", claims.getCognitoUserName()); } }
private static void assertCognitoIdentityPrincipal(Principal principal) { CognitoIdentityPrincipal cognitoIdentityPrincipal = (CognitoIdentityPrincipal) principal; assertEquals(TEST_COGNITO_IDENTITY_AUTH_TYPE, cognitoIdentityPrincipal.getCognitoAuthenticationType()); assertEquals(TEST_COGNITO_IDENTITY_IDENTITY_ID, cognitoIdentityPrincipal.getCognitoIdentityId()); }
default String getCognitoUserName() { return (String) getAllClaims().get("cognito:username"); } }
@Test public void createPrincipal_PrincipalIdGiven_ShouldCreatePrincipal() { GatewayRequest request = createRequestMock("principalId"); CustomAuthorizerPrincipal principal = (CustomAuthorizerPrincipal) createSecurityContextFactory(request) .createPrincipal(); assertEquals("principalId", principal.getName()); assertEquals("principalId", principal.getClaims().getPrincipalId()); assertEquals("principalId", principal.getClaims().getAllClaims().get("principalId")); }
@Test public void createPrincipal_OnlyAccessKeyGiven_ShouldCreateIamPrincipal() { GatewayRequest request = createRequestMock("accessKey", null, null, null); IamPrincipal principal = (IamPrincipal) createSecurityContextFactory(request).createPrincipal(); assertEquals("accessKey", principal.getAccessKey()); assertNull(principal.getUserArn()); assertNull(principal.getUser()); assertNull(principal.getCaller()); assertEquals(principal.getUserArn(), principal.getName()); }
@Test public void createPrincipal_PrincipalIdWithAdditionalClaimsGiven_ShouldCreatePrincipal() { Map<String, Object> claims = new HashMap<>(); claims.put("principalId", "principalId"); claims.put("someOtherClaim", "someOtherClaimValue"); GatewayRequest request = createRequestMock(claims); CustomAuthorizerPrincipal principal = (CustomAuthorizerPrincipal) createSecurityContextFactory(request) .createPrincipal(); assertEquals("principalId", principal.getName()); assertEquals("principalId", principal.getClaims().getPrincipalId()); assertEquals("principalId", principal.getClaims().getAllClaims().get("principalId")); assertEquals("someOtherClaimValue", principal.getClaims().getAllClaims().get("someOtherClaim")); }