PlanBasedAuthenticationHandlerEnhancer implements AuthenticationHandlerEnhancer { @Override public List<AuthenticationHandler> filter(List<AuthenticationHandler> authenticationHandlers) { logger.debug("Filtering authentication handlers according to published API's plans"); List<AuthenticationHandler> providers = new ArrayList<>(); api.getPlans().forEach(plan -> { Optional<AuthenticationHandler> optionalProvider = authenticationHandlers .stream() .filter(provider -> provider.name().equalsIgnoreCase(plan.getSecurity())) .findFirst(); if (optionalProvider.isPresent()) { AuthenticationHandler provider = optionalProvider.get(); logger.debug("Authentication handler [{}] is required by the plan [{}]. Installing...", provider.name(), plan.getName()); if (provider.name().equals("api_key")) { provider = new ApiKeyPlanBasedAuthenticationHandler(provider, plan); } if (plan.getSelectionRule() != null && ! plan.getSelectionRule().isEmpty()) { providers.add(new SelectionRulePlanBasedAuthenticationHandler(provider, plan)); } else { providers.add(new PlanBasedAuthenticationHandler(provider, plan)); } } }); if (! providers.isEmpty()) { logger.info("{} requires the following authentication handlers:", api); providers.forEach(authenticationProvider -> logger.info("\t* {}", authenticationProvider.name())); } else { logger.warn("No authentication handler is provided for {}", api); } return providers; }  @Override List<AuthenticationHandler> filter(List<AuthenticationHandler> authenticationHandlers); void setApi(Api api);  }
@Test public void shouldNotResolveKeylessPolicy_becauseNoPlan() { AuthenticationHandler authenticationHandler = mock(AuthenticationHandler.class); when(authenticationHandler.name()).thenReturn("keyless"); List<AuthenticationHandler> SecurityProviders = authenticationHandlerEnhancer.filter(Collections.singletonList(authenticationHandler)); assertNotNull(SecurityProviders); assertTrue(SecurityProviders.isEmpty()); }
@Test public void shouldNotResolveKeylessPolicy_becauseOnePlanApikey() { AuthenticationHandler authenticationHandler = mock(AuthenticationHandler.class); when(authenticationHandler.name()).thenReturn("keyless"); Plan plan1 = new Plan(); plan1.setSecurity("apikey"); when(api.getPlans()).thenReturn(Collections.singletonList(plan1)); List<AuthenticationHandler> SecurityProviders = authenticationHandlerEnhancer.filter(Collections.singletonList(authenticationHandler)); assertNotNull(SecurityProviders); assertTrue(SecurityProviders.isEmpty()); }
@Test public void shouldResolveKeylessPolicy_becauseOnePlanKeyless() { AuthenticationHandler authenticationHandler = mock(AuthenticationHandler.class); when(authenticationHandler.name()).thenReturn("keyless"); Plan plan1 = new Plan(); plan1.setSecurity("keyless"); when(api.getPlans()).thenReturn(Collections.singletonList(plan1)); List<AuthenticationHandler> SecurityProviders = authenticationHandlerEnhancer.filter(Collections.singletonList(authenticationHandler)); assertNotNull(SecurityProviders); assertFalse(SecurityProviders.isEmpty()); assertEquals("keyless", SecurityProviders.iterator().next().name()); }