Search in sources :

Example 6 with Security

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

the class OutboundOverrideJwtExampleTest method setup.

@BeforeAll
public static void setup() {
    CompletionStage<Void> first = startClientService(-1);
    CompletionStage<Void> second = startServingService(-1);
    first.toCompletableFuture().join();
    second.toCompletableFuture().join();
    Security security = Security.builder().addProvider(HttpBasicAuthProvider.builder().build()).build();
    webClient = WebClient.builder().baseUri("http://localhost:" + clientPort()).addService(WebClientSecurity.create(security)).build();
}
Also used : Security(io.helidon.security.Security) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 7 with Security

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

the class VaultsExampleMain method main.

/**
 * Start the server.
 *
 * @param args ignored
 */
public static void main(String[] args) {
    LogConfig.configureRuntime();
    // as I cannot share my configuration of OCI, let's combine the configuration
    // from my home directory with the one compiled into the jar
    // when running this example, you can either update the application.yaml in resources directory
    // or use the same approach
    Config config = buildConfig();
    System.out.println("This example requires a valid OCI Vault, Secret and keys configured. It also requires " + "a Hashicorp Vault running with preconfigured data. Please see README.md");
    Security security = Security.create(config.get("security"));
    WebServer server = WebServer.builder().config(config.get("server")).routing(Routing.builder().register("/secrets", new SecretsService(security)).register("/encryption", new EncryptionService(security)).register("/digests", new DigestService(security))).build().start().await(10, TimeUnit.SECONDS);
    System.out.println("Server started on port: " + server.port());
    String baseAddress = "http://localhost:" + server.port() + "/";
    System.out.println("Secrets endpoints:");
    System.out.println();
    System.out.println("OCI secret:");
    System.out.println("\t" + baseAddress + "secrets/password");
    System.out.println("Config secret:");
    System.out.println("\t" + baseAddress + "secrets/token");
    System.out.println("HCP Vault secret:");
    System.out.println("\t" + baseAddress + "secrets/username");
    System.out.println();
    System.out.println("Encryption endpoints:");
    System.out.println("OCI encrypted:");
    System.out.println("\t" + baseAddress + "encryption/encrypt/crypto-1/text");
    System.out.println("\t" + baseAddress + "encryption/decrypt/crypto-1/cipherText");
    System.out.println("Config encrypted:");
    System.out.println("\t" + baseAddress + "encryption/encrypt/crypto-2/text");
    System.out.println("\t" + baseAddress + "encryption/decrypt/crypto-2/cipherText");
    System.out.println("HCP Vault encrypted:");
    System.out.println("\t" + baseAddress + "encryption/encrypt/crypto-3/text");
    System.out.println("\t" + baseAddress + "encryption/decrypt/crypto-3/cipherText");
    System.out.println();
    System.out.println("Signature/HMAC endpoints:");
    System.out.println("OCI Signature:");
    System.out.println("\t" + baseAddress + "digests/digest/sig-1/text");
    System.out.println("\t" + baseAddress + "digests/verify/sig-1/text/signature");
    System.out.println("HCP Vault Signature:");
    System.out.println("\t" + baseAddress + "digests/digest/sig-2/text");
    System.out.println("\t" + baseAddress + "digests/digest/sig-2/text/signature");
    System.out.println("HCP Vault HMAC:");
    System.out.println("\t" + baseAddress + "digests/digest/hmac-1/text");
    System.out.println("\t" + baseAddress + "digests/digest/hmac-2/text/hmac");
}
Also used : WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) Security(io.helidon.security.Security)

Example 8 with Security

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

the class BasicExampleBuilderMain method startServer.

static WebServer startServer() {
    LogConfig.initClass();
    Routing routing = Routing.builder().register(buildWebSecurity().securityDefaults(WebSecurity.authenticate())).any("/static[/{*}]", WebSecurity.rolesAllowed("user")).register("/static", StaticContentSupport.create("/WEB")).get("/noRoles", WebSecurity.enforce()).get("/user[/{*}]", WebSecurity.rolesAllowed("user")).get("/admin", WebSecurity.rolesAllowed("admin")).get("/deny", WebSecurity.rolesAllowed("deny").audit()).any("/noAuthn", WebSecurity.rolesAllowed("admin").authenticationOptional().audit()).get("/{*}", (req, res) -> {
        Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
        res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
        res.send("Hello, you are: \n" + securityContext.map(ctx -> ctx.user().orElse(SecurityContext.ANONYMOUS).toString()).orElse("Security context is null"));
    }).build();
    return WebServer.builder().routing(routing).build().start().await(10, TimeUnit.SECONDS);
}
Also used : Arrays(java.util.Arrays) Security(io.helidon.security.Security) Set(java.util.Set) HashMap(java.util.HashMap) SecurityContext(io.helidon.security.SecurityContext) MediaType(io.helidon.common.http.MediaType) TimeUnit(java.util.concurrent.TimeUnit) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) Map(java.util.Map) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) SecureUserStore(io.helidon.security.providers.httpauth.SecureUserStore) Optional(java.util.Optional) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing)

Example 9 with Security

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

the class GoogleBuilderMain method start.

static int start(int port) {
    Security security = Security.builder().addProvider(GoogleTokenProvider.builder().clientId("your-client-id.apps.googleusercontent.com")).build();
    WebSecurity ws = WebSecurity.create(security);
    Routing.Builder routing = Routing.builder().register(ws).get("/rest/profile", WebSecurity.authenticate(), (req, res) -> {
        Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
        res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
        res.send("Response from builder based service, you are: \n" + securityContext.flatMap(SecurityContext::user).map(Subject::toString).orElse("Security context is null"));
        req.next();
    }).register(StaticContentSupport.create("/WEB"));
    theServer = GoogleUtil.startIt(port, routing);
    return theServer.port();
}
Also used : Security(io.helidon.security.Security) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) SecurityContext(io.helidon.security.SecurityContext) Subject(io.helidon.security.Subject) GoogleTokenProvider(io.helidon.security.providers.google.login.GoogleTokenProvider) Routing(io.helidon.webserver.Routing) MediaType(io.helidon.common.http.MediaType) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing) Security(io.helidon.security.Security) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Subject(io.helidon.security.Subject)

Example 10 with Security

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

the class IdcsMain method main.

/**
 * Start the example.
 *
 * @param args ignored
 */
public static void main(String[] args) {
    // load logging configuration
    LogConfig.configureRuntime();
    Config config = buildConfig();
    Security security = Security.create(config.get("security"));
    // this is needed for proper encryption/decryption of cookies
    Contexts.globalContext().register(security);
    Routing.Builder routing = Routing.builder().register(WebSecurity.create(security, config.get("security"))).register(OidcSupport.create(config)).get("/rest/profile", (req, res) -> {
        Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
        res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
        res.send("Response from config based service, you are: \n" + securityContext.flatMap(SecurityContext::user).map(Subject::toString).orElse("Security context is null"));
    }).get("/loggedout", (req, res) -> res.send("You have been logged out"));
    theServer = WebServer.create(routing, config.get("server"));
    IdcsUtil.start(theServer);
}
Also used : Security(io.helidon.security.Security) Config(io.helidon.config.Config) SecurityContext(io.helidon.security.SecurityContext) Contexts(io.helidon.common.context.Contexts) MediaType(io.helidon.common.http.MediaType) ConfigSources.file(io.helidon.config.ConfigSources.file) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Subject(io.helidon.security.Subject) OidcSupport(io.helidon.security.providers.oidc.OidcSupport) ConfigSources.classpath(io.helidon.config.ConfigSources.classpath) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) Optional(java.util.Optional) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) SecurityContext(io.helidon.security.SecurityContext) Routing(io.helidon.webserver.Routing) Security(io.helidon.security.Security) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Subject(io.helidon.security.Subject)

Aggregations

Security (io.helidon.security.Security)48 SecurityContext (io.helidon.security.SecurityContext)25 Config (io.helidon.config.Config)22 BeforeAll (org.junit.jupiter.api.BeforeAll)14 Test (org.junit.jupiter.api.Test)14 LogConfig (io.helidon.common.LogConfig)13 Routing (io.helidon.webserver.Routing)12 Optional (java.util.Optional)12 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)10 WebClientSecurity (io.helidon.webclient.security.WebClientSecurity)10 WebServer (io.helidon.webserver.WebServer)10 Channel (io.grpc.Channel)7 GrpcRouting (io.helidon.grpc.server.GrpcRouting)7 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)7 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)7 Set (java.util.Set)7 CoreMatchers.is (org.hamcrest.CoreMatchers.is)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Context (io.helidon.common.context.Context)6 AuthorizationResponse (io.helidon.security.AuthorizationResponse)6