Search in sources :

Example 1 with Environment

use of com.b2international.snowowl.core.setup.Environment in project snow-owl by b2ihealthcare.

the class IdentityPlugin method createProviders.

private List<IdentityProvider> createProviders(Environment env, List<IdentityProviderConfig> providerConfigurations) {
    final List<IdentityProvider> providers = newArrayListWithExpectedSize(3);
    env.plugins().getPlugins().stream().filter(IdentityProviderFactory.class::isInstance).map(IdentityProviderFactory.class::cast).forEach(factory -> {
        Optional<IdentityProviderConfig> providerConfig = providerConfigurations.stream().filter(conf -> conf.getClass() == factory.getConfigType()).findFirst();
        if (providerConfig.isPresent()) {
            try {
                providers.add(factory.create(env, providerConfig.get()));
            } catch (Exception e) {
                throw new SnowowlRuntimeException(String.format("Couldn't initialize '%s' identity provider", factory), e);
            }
        }
    });
    return providers;
}
Also used : JWT(com.auth0.jwt.JWT) java.util(java.util) JwkProvider(com.auth0.jwk.JwkProvider) Iterables(com.google.common.collect.Iterables) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) URL(java.net.URL) BiFunction(java.util.function.BiFunction) Plugin(com.b2international.snowowl.core.setup.Plugin) Hashing(com.google.common.hash.Hashing) Strings(com.google.common.base.Strings) Algorithm(com.auth0.jwt.algorithms.Algorithm) Environment(com.b2international.snowowl.core.setup.Environment) RSAPublicKey(java.security.interfaces.RSAPublicKey) ImmutableList(com.google.common.collect.ImmutableList) JWTVerifier(com.auth0.jwt.interfaces.JWTVerifier) JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) SnowOwlConfiguration(com.b2international.snowowl.core.config.SnowOwlConfiguration) RSAKeyProvider(com.auth0.jwt.interfaces.RSAKeyProvider) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ClassPathScanner(com.b2international.snowowl.core.plugin.ClassPathScanner) Charsets(com.google.common.base.Charsets) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) JwkException(com.auth0.jwk.JwkException) MalformedURLException(java.net.MalformedURLException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) ConfigurationRegistry(com.b2international.snowowl.core.setup.ConfigurationRegistry) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) JwkProviderBuilder(com.auth0.jwk.JwkProviderBuilder) KeyFactory(java.security.KeyFactory) SnowOwl(com.b2international.snowowl.core.SnowOwl) TimeUnit(java.util.concurrent.TimeUnit) Lists.newArrayListWithExpectedSize(com.google.common.collect.Lists.newArrayListWithExpectedSize) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Component(com.b2international.snowowl.core.plugin.Component) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ApplicationContext(com.b2international.snowowl.core.ApplicationContext) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) JwkException(com.auth0.jwk.JwkException) MalformedURLException(java.net.MalformedURLException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 2 with Environment

use of com.b2international.snowowl.core.setup.Environment in project snow-owl by b2ihealthcare.

the class SnowOwlCommandProvider method _snowowl.

public void _snowowl(CommandInterpreter interpreter) throws Exception {
    // first read all args into an array
    List<String> args = newArrayList();
    String arg;
    while ((arg = interpreter.nextArgument()) != null) {
        args.add(arg);
    }
    final Environment env = ApplicationContext.getServiceForClass(Environment.class);
    final List<CommandLine> commands = cli(env).parse(args.toArray(new String[] {}));
    try (InterpreterStream out = new InterpreterStream(interpreter)) {
        // print help if requested for any command
        if (CommandLine.printHelpIfRequested(commands, out, out, CommandLine.Help.Ansi.AUTO)) {
            return;
        }
        // get the last command used in the cli
        CommandLine cli = Iterables.getLast(commands, null);
        if (cli == null) {
            return;
        }
        // we should get an executable Snow Owl Command, so execute it
        BaseCommand cmd = (BaseCommand) cli.getCommand();
        final String authorizationToken = ApplicationContext.getServiceForClass(JWTGenerator.class).generate(User.SYSTEM);
        final ServiceProvider context = env.inject().bind(IEventBus.class, new AuthorizedEventBus(ApplicationContext.getServiceForClass(IEventBus.class), ImmutableMap.of(AuthorizedRequest.AUTHORIZATION_HEADER, authorizationToken))).build();
        cmd.setContext(context);
        cmd.run(out);
    } catch (Exception e) {
        interpreter.println("Unknown error occured");
        interpreter.printStackTrace(e);
    }
}
Also used : CommandLine(picocli.CommandLine) JWTGenerator(com.b2international.snowowl.core.identity.JWTGenerator) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) Environment(com.b2international.snowowl.core.setup.Environment) AuthorizedEventBus(com.b2international.snowowl.core.authorization.AuthorizedEventBus) IEventBus(com.b2international.snowowl.eventbus.IEventBus) IOException(java.io.IOException)

Aggregations

Environment (com.b2international.snowowl.core.setup.Environment)2 JwkException (com.auth0.jwk.JwkException)1 JwkProvider (com.auth0.jwk.JwkProvider)1 JwkProviderBuilder (com.auth0.jwk.JwkProviderBuilder)1 JWT (com.auth0.jwt.JWT)1 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 JWTVerifier (com.auth0.jwt.interfaces.JWTVerifier)1 RSAKeyProvider (com.auth0.jwt.interfaces.RSAKeyProvider)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 ApplicationContext (com.b2international.snowowl.core.ApplicationContext)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 SnowOwl (com.b2international.snowowl.core.SnowOwl)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 AuthorizedEventBus (com.b2international.snowowl.core.authorization.AuthorizedEventBus)1 SnowOwlConfiguration (com.b2international.snowowl.core.config.SnowOwlConfiguration)1 JWTGenerator (com.b2international.snowowl.core.identity.JWTGenerator)1 ClassPathScanner (com.b2international.snowowl.core.plugin.ClassPathScanner)1 Component (com.b2international.snowowl.core.plugin.Component)1