Search in sources :

Example 26 with ProviderRequest

use of io.helidon.security.ProviderRequest 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");
    }
}
Also used : Errors(io.helidon.common.Errors) SecurityEnvironment(io.helidon.security.SecurityEnvironment) SecurityTime(io.helidon.security.SecurityTime) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 27 with ProviderRequest

use of io.helidon.security.ProviderRequest 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();
}
Also used : Errors(io.helidon.common.Errors) SecurityEnvironment(io.helidon.security.SecurityEnvironment) SecurityTime(io.helidon.security.SecurityTime) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 28 with ProviderRequest

use of io.helidon.security.ProviderRequest 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");
    }
}
Also used : Errors(io.helidon.common.Errors) SecurityEnvironment(io.helidon.security.SecurityEnvironment) SecurityTime(io.helidon.security.SecurityTime) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 29 with ProviderRequest

use of io.helidon.security.ProviderRequest in project helidon by oracle.

the class GoogleTokenProvider method syncAuthenticate.

@Override
protected AuthenticationResponse syncAuthenticate(ProviderRequest providerRequest) {
    Optional<String> maybeToken;
    try {
        maybeToken = tokenHandler.extractToken(providerRequest.env().headers());
    } catch (Exception e) {
        return failInvalidRequest(e);
    }
    SecurityContext sContext = providerRequest.securityContext();
    return maybeToken.map(token -> cachedResponse(token, sContext.tracer(), sContext.tracingSpan())).orElseGet(this::failNoToken);
}
Also used : OutboundSecurityResponse(io.helidon.security.OutboundSecurityResponse) GoogleUtils(com.google.api.client.googleapis.GoogleUtils) ProviderRequest(io.helidon.security.ProviderRequest) GsonFactory(com.google.api.client.json.gson.GsonFactory) BiFunction(java.util.function.BiFunction) Supplier(java.util.function.Supplier) Level(java.util.logging.Level) AuthenticationProvider(io.helidon.security.spi.AuthenticationProvider) GeneralSecurityException(java.security.GeneralSecurityException) GoogleIdTokenVerifier(com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier) Proxy(java.net.Proxy) Map(java.util.Map) EvictableCache(io.helidon.security.providers.common.EvictableCache) Subject(io.helidon.security.Subject) TokenCredential(io.helidon.security.providers.common.TokenCredential) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) Tracer(io.opentracing.Tracer) Config(io.helidon.config.Config) OutboundSecurityProvider(io.helidon.security.spi.OutboundSecurityProvider) SynchronousProvider(io.helidon.security.spi.SynchronousProvider) Set(java.util.Set) IOException(java.io.IOException) SecurityContext(io.helidon.security.SecurityContext) TokenHandler(io.helidon.security.util.TokenHandler) GoogleIdToken(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken) Instant(java.time.Instant) Logger(java.util.logging.Logger) AuthenticationResponse(io.helidon.security.AuthenticationResponse) OutboundConfig(io.helidon.security.providers.common.OutboundConfig) InetSocketAddress(java.net.InetSocketAddress) Principal(io.helidon.security.Principal) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) EndpointConfig(io.helidon.security.EndpointConfig) SecurityEnvironment(io.helidon.security.SecurityEnvironment) TreeMap(java.util.TreeMap) JsonFactory(com.google.api.client.json.JsonFactory) Optional(java.util.Optional) Span(io.opentracing.Span) SecurityContext(io.helidon.security.SecurityContext) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 30 with ProviderRequest

use of io.helidon.security.ProviderRequest in project helidon by oracle.

the class GoogleTokenProviderTest method testInboundMissingToken.

@Test
public void testInboundMissingToken() throws ExecutionException, InterruptedException {
    ProviderRequest inboundRequest = createInboundRequest("OtherHeader", "tearer " + TOKEN_VALUE);
    AuthenticationResponse response = provider.authenticate(inboundRequest).toCompletableFuture().get();
    assertThat(response.status(), is(SecurityResponse.SecurityStatus.FAILURE));
    assertThat(response.statusCode().orElse(200), is(401));
    assertThat(response.responseHeaders().get("WWW-Authenticate"), notNullValue());
}
Also used : AuthenticationResponse(io.helidon.security.AuthenticationResponse) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ProviderRequest (io.helidon.security.ProviderRequest)80 Test (org.junit.jupiter.api.Test)73 EndpointConfig (io.helidon.security.EndpointConfig)54 SecurityEnvironment (io.helidon.security.SecurityEnvironment)46 SecurityContext (io.helidon.security.SecurityContext)32 AuthenticationResponse (io.helidon.security.AuthenticationResponse)28 Errors (io.helidon.common.Errors)27 SecurityLevel (io.helidon.security.SecurityLevel)24 OutboundSecurityResponse (io.helidon.security.OutboundSecurityResponse)21 Subject (io.helidon.security.Subject)20 ArrayList (java.util.ArrayList)19 Principal (io.helidon.security.Principal)13 AuthorizationResponse (io.helidon.security.AuthorizationResponse)9 SignedJwt (io.helidon.security.jwt.SignedJwt)8 List (java.util.List)8 RolesAllowed (jakarta.annotation.security.RolesAllowed)7 Instant (java.time.Instant)7 Locale (java.util.Locale)7 Config (io.helidon.config.Config)6 Jwt (io.helidon.security.jwt.Jwt)6