use of io.helidon.security.SecurityEnvironment in project helidon by oracle.
the class WebSecurity method registerContext.
private void registerContext(ServerRequest req, ServerResponse res) {
Map<String, List<String>> allHeaders = new HashMap<>(req.headers().toMap());
Optional<Map> newHeaders = req.context().get(CONTEXT_ADD_HEADERS, Map.class);
newHeaders.ifPresent(allHeaders::putAll);
// make sure there is no context
if (!req.context().get(SecurityContext.class).isPresent()) {
SecurityEnvironment env = security.environmentBuilder().targetUri(req.uri()).path(req.path().toString()).method(req.method().name()).addAttribute("userIp", req.remoteAddress()).addAttribute("userPort", req.remotePort()).transport(req.isSecure() ? "https" : "http").headers(allHeaders).build();
EndpointConfig ec = EndpointConfig.builder().build();
SecurityContext.Builder contextBuilder = security.contextBuilder(String.valueOf(SECURITY_COUNTER.incrementAndGet())).env(env).endpointConfig(ec);
// only register if exists
req.spanContext().ifPresent(contextBuilder::tracingSpan);
SecurityContext context = contextBuilder.build();
req.context().register(context);
req.context().register(defaultHandler);
}
req.next();
}
use of io.helidon.security.SecurityEnvironment in project helidon by oracle.
the class TimeValidatorTest method testDayOfWeekDeny.
@Test
public void testDayOfWeekDeny() {
// explicitly set time to 10:00
SecurityTime time = SecurityTime.builder().value(ChronoField.HOUR_OF_DAY, 12).value(ChronoField.MINUTE_OF_HOUR, 15).value(ChronoField.DAY_OF_WEEK, DayOfWeek.SUNDAY.getValue()).build();
Errors.Collector collector = Errors.collector();
SecurityEnvironment env = SecurityEnvironment.builder().time(time).build();
ProviderRequest request = mock(ProviderRequest.class);
when(request.env()).thenReturn(env);
validator.validate(timeConfig, collector, request);
if (collector.collect().isValid()) {
fail("Should have failed, as 12:15 is not in supported times");
}
}
use of io.helidon.security.SecurityEnvironment in project helidon by oracle.
the class TimeValidatorTest method testBetweenTimesAndDayOfWekPermit.
@Test
public void testBetweenTimesAndDayOfWekPermit() {
// explicitly set time to 10:00
SecurityTime time = SecurityTime.builder().value(ChronoField.HOUR_OF_DAY, 10).value(ChronoField.MINUTE_OF_HOUR, 0).value(ChronoField.DAY_OF_WEEK, DayOfWeek.TUESDAY.getValue()).build();
Errors.Collector collector = Errors.collector();
SecurityEnvironment env = SecurityEnvironment.builder().time(time).build();
ProviderRequest request = mock(ProviderRequest.class);
when(request.env()).thenReturn(env);
validator.validate(timeConfig, collector, request);
collector.collect().checkValid();
}
use of io.helidon.security.SecurityEnvironment in project helidon by oracle.
the class TimeValidatorTest method testBetweenTimesDeny.
@Test
public void testBetweenTimesDeny() {
// explicitly set time to 10:00
SecurityTime time = SecurityTime.builder().value(ChronoField.HOUR_OF_DAY, 12).value(ChronoField.MINUTE_OF_HOUR, 15).value(ChronoField.DAY_OF_WEEK, DayOfWeek.TUESDAY.getValue()).build();
Errors.Collector collector = Errors.collector();
SecurityEnvironment env = SecurityEnvironment.builder().time(time).build();
ProviderRequest request = mock(ProviderRequest.class);
when(request.env()).thenReturn(env);
validator.validate(timeConfig, collector, request);
if (collector.collect().isValid()) {
fail("Should have failed, as 12:15 is not in supported times");
}
}
use of io.helidon.security.SecurityEnvironment in project helidon by oracle.
the class WebSecurityQueryParamTest method testQueryParams.
@Test
public void testQueryParams() {
SecurityHandler securityHandler = SecurityHandler.create().queryParam("jwt", TokenHandler.builder().tokenHeader("BEARER_TOKEN").tokenPattern(Pattern.compile("bearer (.*)")).build()).queryParam("name", TokenHandler.builder().tokenHeader("NAME_FROM_REQUEST").build());
ServerRequest req = Mockito.mock(ServerRequest.class);
Parameters params = Mockito.mock(Parameters.class);
when(params.all("jwt")).thenReturn(List.of("bearer jwt_content"));
when(params.all("name")).thenReturn(List.of("name_content"));
when(req.queryParams()).thenReturn(params);
SecurityContext context = Mockito.mock(SecurityContext.class);
SecurityEnvironment env = SecurityEnvironment.create();
when(context.env()).thenReturn(env);
// context is a stub
securityHandler.extractQueryParams(context, req);
// captor captures the argument
ArgumentCaptor<SecurityEnvironment> newHeaders = ArgumentCaptor.forClass(SecurityEnvironment.class);
verify(context).env(newHeaders.capture());
// now validate the value we were called with
env = newHeaders.getValue();
assertThat(env.headers().get("BEARER_TOKEN"), is(List.of("jwt_content")));
assertThat(env.headers().get("NAME_FROM_REQUEST"), is(List.of("name_content")));
}
Aggregations