@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); }
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; }
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; } }
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); }
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); }
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);  }