OnrampAuthorisation { public boolean isAuthorised(Authentication authentication, String roadName) { AuthenticationType type = AuthenticationType.of(authentication); AuthorisationOutcome outcome = ERROR; try { Optional<Road> road = Optional.ofNullable(store.get(roadName)); if (road.isPresent()) { Optional<Onramp> onramp = road.map(Road::getAuthorisation).map(Authorisation::getOnramp); if (type == AUTHENTICATED) { outcome = isUserAuthorised(authentication, roadName, onramp); } else { outcome = isCidrAuthorised(authentication, roadName, onramp); } } } catch (Exception e) { log.error("Error authorising for road {}: {}", roadName, authentication, e); throw e; } finally { metrics.increment(roadName, type, outcome); } return outcome == AUTHORISED; } @Autowired  OnrampAuthorisation(
      @Value("#{store}") Map<String, Road> store,
      CidrBlockAuthorisation cidrBlockAuthorisation,
      MeterRegistry registry); boolean isAuthorised(Authentication authentication, String roadName);  }
@Test public void authorised() throws Exception { onramp.setAuthorities(singletonList("AUTHORIZED")); doReturn(true).when(authentication).isAuthenticated(); doReturn(singletonList(new SimpleGrantedAuthority("AUTHORIZED"))).when(authentication).getAuthorities(); boolean result = underTest.isAuthorised(authentication, ROAD_NAME); assertThat(result, is(true)); verify(metrics).increment("road1", AUTHENTICATED, AUTHORISED); }
@Test public void authorisedWildcard() throws Exception { doReturn(true).when(authentication).isAuthenticated(); onramp.setAuthorities(singletonList("*")); boolean result = underTest.isAuthorised(authentication, ROAD_NAME); assertThat(result, is(true)); verify(metrics).increment("road1", AUTHENTICATED, AUTHORISED); }
@Test public void authorisedCidr() throws Exception { onramp.setAuthorities(singletonList("AUTHORIZED")); List<String> cidrBlocks = singletonList("0.0.0.0/0"); onramp.setCidrBlocks(cidrBlocks); doReturn(false).when(authentication).isAuthenticated(); doReturn(details).when(authentication).getDetails(); String address = "0.0.0.0"; doReturn(address).when(details).getRemoteAddress(); doReturn(true).when(cidrBlockAuthorisation).isAuthorised(cidrBlocks, address); boolean result = underTest.isAuthorised(authentication, ROAD_NAME); assertThat(result, is(true)); verify(metrics).increment("road1", ANONYMOUS, AUTHORISED); }
@Test public void unauthorisedCidr() throws Exception { onramp.setAuthorities(singletonList("AUTHORIZED")); List<String> cidrBlocks = singletonList("0.0.0.0/0"); onramp.setCidrBlocks(cidrBlocks); doReturn(false).when(authentication).isAuthenticated(); doReturn(details).when(authentication).getDetails(); String address = "0.0.0.0"; doReturn(address).when(details).getRemoteAddress(); doReturn(false).when(cidrBlockAuthorisation).isAuthorised(cidrBlocks, address); boolean result = underTest.isAuthorised(authentication, ROAD_NAME); assertThat(result, is(false)); verify(metrics).increment("road1", ANONYMOUS, UNAUTHORISED); }
@Test public void roadDoesNotExist() throws Exception { store.clear(); boolean result = underTest.isAuthorised(authentication, ROAD_NAME); assertThat(result, is(false)); verify(metrics).increment("road1", ANONYMOUS, ERROR); }