Search in sources :

Example 11 with Subject

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

the class JakartaElPolicyExecutorTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    JavaxElPolicyExecutor ex = JavaxElPolicyExecutor.create();
    SecurityEnvironment env = SecurityEnvironment.create();
    Subject user = Subject.builder().principal(Principal.create("unit-test-user")).addGrant(Role.create("unit-test-user-role")).build();
    Subject service = Subject.builder().principal(Principal.create("unit-test-service")).addGrant(Role.create("unit-test-service-role")).build();
    MyResource object = new MyResource("unit-test-user");
    ProviderRequest request = mock(ProviderRequest.class);
    when(request.service()).thenReturn(Optional.of(service));
    when(request.subject()).thenReturn(Optional.of(user));
    when(request.env()).thenReturn(env);
    when(request.getObject()).thenReturn(Optional.of(object));
    Errors.Collector collector = Errors.collector();
    ex.executePolicy("${user.principal.id == object.owner}", collector, request);
    collector.collect().checkValid();
    collector = Errors.collector();
    ex.executePolicy("${user.principal.id == object.owner}", collector, request);
    collector.collect().checkValid();
    collector = Errors.collector();
    ex.executePolicy("${inRole(user, 'unit-test-user-role') && inRole(service, 'unit-test-service-role')}", collector, request);
    collector.collect().checkValid();
    collector = Errors.collector();
    ex.executePolicy("${service.principal.id == object.owner}", collector, request);
    if (collector.collect().isValid()) {
        fail("Should have failed, as service is not the owner of the object");
    }
    collector = Errors.collector();
    ex.executePolicy("${env.time.year >= 2017}", collector, request);
    collector.collect().checkValid();
}
Also used : Errors(io.helidon.common.Errors) SecurityEnvironment(io.helidon.security.SecurityEnvironment) Subject(io.helidon.security.Subject) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 12 with Subject

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

the class JavaxElPolicyExecutor method executePolicy.

@Override
public void executePolicy(String policyStatement, Errors.Collector collector, ProviderRequest request) {
    StandardELContext context = new StandardELContext(ef);
    context.addELResolver(ATTRIBUTE_RESOLVER);
    FunctionMapper functions = context.getFunctionMapper();
    VariableMapper variables = context.getVariableMapper();
    customMethods.forEach(customFunction -> functions.mapFunction(customFunction.prefix, customFunction.localName, customFunction.method));
    Subject userSubject = request.subject().orElse(SecurityContext.ANONYMOUS);
    variable(variables, "user", userSubject, Subject.class);
    variable(variables, "subject", userSubject, Subject.class);
    variable(variables, "service", request.service().orElse(SecurityContext.ANONYMOUS), Subject.class);
    variable(variables, "env", request.env(), SecurityEnvironment.class);
    variable(variables, "object", request.getObject().orElse(null), Object.class);
    variable(variables, "request", request, ProviderRequest.class);
    try {
        ValueExpression expression = ef.createValueExpression(context, policyStatement, boolean.class);
        boolean value = (boolean) expression.getValue(context);
        if (!value) {
            collector.fatal(this, "Policy statement \"" + policyStatement + "\" evaluated to false");
        }
    } catch (Exception e) {
        LOGGER.log(Level.FINEST, e, () -> "Statement " + policyStatement + " evaluation failed");
        throw new SecurityException("Policy statement \"" + policyStatement + "\" evaluated to an exception", e);
    }
}
Also used : VariableMapper(jakarta.el.VariableMapper) StandardELContext(jakarta.el.StandardELContext) ValueExpression(jakarta.el.ValueExpression) Subject(io.helidon.security.Subject) ELException(jakarta.el.ELException) FunctionMapper(jakarta.el.FunctionMapper)

Example 13 with Subject

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

the class HeaderAtnProviderConfigTest method testProviderService.

@Test
public void testProviderService() {
    String username = "username";
    Security security = Security.create(config.get("security"));
    SecurityContext context = security.contextBuilder("unit-test").env(SecurityEnvironment.builder().header("Authorization", "bearer " + username).build()).build();
    AuthenticationResponse response = context.atnClientBuilder().buildAndGet();
    assertThat(response.status(), is(SecurityResponse.SecurityStatus.SUCCESS));
    assertThat(response.user(), is(not(Optional.empty())));
    response.user().map(Subject::principal).map(Principal::getName).ifPresent(user -> {
        assertThat(user, is(username));
    });
    assertThat(response.service(), is(Optional.empty()));
}
Also used : SecurityContext(io.helidon.security.SecurityContext) Security(io.helidon.security.Security) AuthenticationResponse(io.helidon.security.AuthenticationResponse) Subject(io.helidon.security.Subject) Test(org.junit.jupiter.api.Test)

Example 14 with Subject

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

the class HeaderAtnProviderTest method testExtraction.

@Test
public void testExtraction() {
    String username = "username";
    HeaderAtnProvider provider = getFullProvider();
    SecurityEnvironment env = SecurityEnvironment.builder().header("Authorization", "bearer " + username).build();
    ProviderRequest request = mock(ProviderRequest.class);
    when(request.env()).thenReturn(env);
    AuthenticationResponse response = provider.syncAuthenticate(request);
    assertThat(response.status(), is(SecurityResponse.SecurityStatus.SUCCESS));
    assertThat(response.user(), is(not(Optional.empty())));
    assertThat(response.service(), is(Optional.empty()));
    response.user().map(Subject::principal).map(Principal::getName).ifPresent(name -> assertThat(name, is(username)));
}
Also used : SecurityEnvironment(io.helidon.security.SecurityEnvironment) AuthenticationResponse(io.helidon.security.AuthenticationResponse) Subject(io.helidon.security.Subject) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 15 with Subject

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

the class JwtProvider method buildSubject.

Subject buildSubject(Jwt jwt, SignedJwt signedJwt) {
    Principal principal = buildPrincipal(jwt);
    TokenCredential.Builder builder = TokenCredential.builder();
    jwt.issueTime().ifPresent(builder::issueTime);
    jwt.expirationTime().ifPresent(builder::expTime);
    jwt.issuer().ifPresent(builder::issuer);
    builder.token(signedJwt.tokenContent());
    builder.addToken(Jwt.class, jwt);
    builder.addToken(SignedJwt.class, signedJwt);
    Subject.Builder subjectBuilder = Subject.builder().principal(principal).addPublicCredential(TokenCredential.class, builder.build());
    if (useJwtGroups) {
        Optional<List<String>> userGroups = jwt.userGroups();
        userGroups.ifPresent(groups -> groups.forEach(group -> subjectBuilder.addGrant(Role.create(group))));
    }
    Optional<List<String>> scopes = jwt.scopes();
    scopes.ifPresent(scopeList -> {
        scopeList.forEach(scope -> subjectBuilder.addGrant(Grant.builder().name(scope).type("scope").build()));
    });
    return subjectBuilder.build();
}
Also used : OutboundSecurityResponse(io.helidon.security.OutboundSecurityResponse) ProviderRequest(io.helidon.security.ProviderRequest) JwtException(io.helidon.security.jwt.JwtException) HashMap(java.util.HashMap) SignedJwt(io.helidon.security.jwt.SignedJwt) AuthenticationProvider(io.helidon.security.spi.AuthenticationProvider) Map(java.util.Map) Grant(io.helidon.security.Grant) Subject(io.helidon.security.Subject) TokenCredential(io.helidon.security.providers.common.TokenCredential) IdentityHashMap(java.util.IdentityHashMap) Config(io.helidon.config.Config) JwtUtil(io.helidon.security.jwt.JwtUtil) SubjectType(io.helidon.security.SubjectType) OutboundSecurityProvider(io.helidon.security.spi.OutboundSecurityProvider) SynchronousProvider(io.helidon.security.spi.SynchronousProvider) TokenHandler(io.helidon.security.util.TokenHandler) Instant(java.time.Instant) Logger(java.util.logging.Logger) Resource(io.helidon.common.configurable.Resource) AuthenticationResponse(io.helidon.security.AuthenticationResponse) OutboundConfig(io.helidon.security.providers.common.OutboundConfig) Principal(io.helidon.security.Principal) JwkKeys(io.helidon.security.jwt.jwk.JwkKeys) SecurityResponse(io.helidon.security.SecurityResponse) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) EndpointConfig(io.helidon.security.EndpointConfig) SecurityEnvironment(io.helidon.security.SecurityEnvironment) Jwt(io.helidon.security.jwt.Jwt) OutboundTarget(io.helidon.security.providers.common.OutboundTarget) Role(io.helidon.security.Role) Optional(java.util.Optional) Errors(io.helidon.common.Errors) Jwk(io.helidon.security.jwt.jwk.Jwk) List(java.util.List) TokenCredential(io.helidon.security.providers.common.TokenCredential) Principal(io.helidon.security.Principal) Subject(io.helidon.security.Subject)

Aggregations

Subject (io.helidon.security.Subject)36 ProviderRequest (io.helidon.security.ProviderRequest)22 SecurityContext (io.helidon.security.SecurityContext)18 SecurityEnvironment (io.helidon.security.SecurityEnvironment)18 AuthenticationResponse (io.helidon.security.AuthenticationResponse)17 Test (org.junit.jupiter.api.Test)17 Principal (io.helidon.security.Principal)16 EndpointConfig (io.helidon.security.EndpointConfig)15 OutboundSecurityResponse (io.helidon.security.OutboundSecurityResponse)15 SignedJwt (io.helidon.security.jwt.SignedJwt)11 Config (io.helidon.config.Config)10 Jwt (io.helidon.security.jwt.Jwt)9 Optional (java.util.Optional)8 Instant (java.time.Instant)7 Locale (java.util.Locale)7 TokenCredential (io.helidon.security.providers.common.TokenCredential)6 LinkedList (java.util.LinkedList)6 List (java.util.List)6 Errors (io.helidon.common.Errors)4 MediaType (io.helidon.common.http.MediaType)4