Search in sources :

Example 1 with EndpointType

use of org.keycloak.client.registration.cli.common.EndpointType in project keycloak by keycloak.

the class ParseUtil method mergeAttributes.

public static CmdStdinContext mergeAttributes(CmdStdinContext ctx, List<AttributeOperation> attrs) {
    String content = ctx.getContent();
    ClientRepresentation client = ctx.getClient();
    OIDCClientRepresentation oidcClient = ctx.getOidcClient();
    EndpointType type = ctx.getEndpointType();
    try {
        if (content == null) {
            if (type == EndpointType.DEFAULT) {
                client = new ClientRepresentation();
            } else if (type == EndpointType.OIDC) {
                oidcClient = new OIDCClientRepresentation();
            }
        }
        Object rep = client != null ? client : oidcClient;
        if (rep != null) {
            try {
                setAttributes(rep, attrs);
            } catch (AttributeException e) {
                throw new RuntimeException("Failed to set attribute '" + e.getAttributeName() + "' on document type '" + type.getName() + "'", e);
            }
            content = JsonSerialization.writeValueAsString(rep);
        } else {
            throw new RuntimeException("Setting attributes is not supported for type: " + type.getName());
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to merge set attributes with configuration from file", e);
    }
    ctx.setContent(content);
    ctx.setClient(client);
    ctx.setOidcClient(oidcClient);
    return ctx;
}
Also used : OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) IOException(java.io.IOException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation)

Example 2 with EndpointType

use of org.keycloak.client.registration.cli.common.EndpointType in project keycloak by keycloak.

the class GetCmd method execute.

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
    try {
        if (printHelp()) {
            return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
        }
        processGlobalOptions();
        if (args == null || args.isEmpty()) {
            throw new IllegalArgumentException("CLIENT not specified");
        }
        if (args.size() > 1) {
            throw new IllegalArgumentException("Invalid option: " + args.get(1));
        }
        String clientId = args.get(0);
        EndpointType regType = endpoint != null ? EndpointType.of(endpoint) : EndpointType.DEFAULT;
        if (clientId.startsWith("-")) {
            warnfErr(ParseUtil.CLIENT_OPTION_WARN, clientId);
        }
        ConfigData config = loadConfig();
        config = copyWithServerInfo(config);
        if (token == null) {
            // if registration access token is not set via -t, try use the one from configuration
            token = getRegistrationToken(config.sessionRealmConfigData(), clientId);
        }
        setupTruststore(config, commandInvocation);
        String auth = token;
        if (auth == null) {
            config = ensureAuthInfo(config, commandInvocation);
            config = copyWithServerInfo(config);
            if (credentialsAvailable(config)) {
                auth = ensureToken(config);
            }
        }
        auth = auth != null ? "Bearer " + auth : null;
        final String server = config.getServerUrl();
        final String realm = config.getRealm();
        InputStream response = doGet(server + "/realms/" + realm + "/clients-registrations/" + regType.getEndpoint() + "/" + urlencode(clientId), APPLICATION_JSON, auth);
        try {
            String json = readFully(response);
            Object result = null;
            switch(regType) {
                case DEFAULT:
                    {
                        ClientRepresentation client = JsonSerialization.readValue(json, ClientRepresentation.class);
                        result = client;
                        saveMergeConfig(cfg -> {
                            setRegistrationToken(cfg.ensureRealmConfigData(server, realm), client.getClientId(), client.getRegistrationAccessToken());
                        });
                        break;
                    }
                case OIDC:
                    {
                        OIDCClientRepresentation client = JsonSerialization.readValue(json, OIDCClientRepresentation.class);
                        result = client;
                        saveMergeConfig(cfg -> {
                            setRegistrationToken(cfg.ensureRealmConfigData(server, realm), client.getClientId(), client.getRegistrationAccessToken());
                        });
                        break;
                    }
                case INSTALL:
                    {
                        result = JsonSerialization.readValue(json, AdapterConfig.class);
                        break;
                    }
                case SAML2:
                    {
                        break;
                    }
                default:
                    {
                        throw new RuntimeException("Unexpected type: " + regType);
                    }
            }
            if (!compressed && result != null) {
                json = JsonSerialization.writeValueAsPrettyString(result);
            }
            printOut(json);
        // } catch (UnrecognizedPropertyException e) {
        // throw new RuntimeException("Failed to parse returned JSON - " + e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException("Failed to process HTTP response", e);
        }
        return CommandResult.SUCCESS;
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(e.getMessage() + suggestHelp(), e);
    } finally {
        commandInvocation.stop();
    }
}
Also used : APPLICATION_JSON(org.keycloak.client.registration.cli.util.HttpUtil.APPLICATION_JSON) CMD(org.keycloak.client.registration.cli.util.OsUtil.CMD) CommandResult(org.jboss.aesh.console.command.CommandResult) CommandDefinition(org.jboss.aesh.cl.CommandDefinition) AuthUtil.ensureToken(org.keycloak.client.registration.cli.util.AuthUtil.ensureToken) ConfigUtil.setRegistrationToken(org.keycloak.client.registration.cli.util.ConfigUtil.setRegistrationToken) IoUtil.printOut(org.keycloak.client.registration.cli.util.IoUtil.printOut) ConfigUtil.saveMergeConfig(org.keycloak.client.registration.cli.util.ConfigUtil.saveMergeConfig) CommandInvocation(org.jboss.aesh.console.command.invocation.CommandInvocation) HttpUtil.doGet(org.keycloak.client.registration.cli.util.HttpUtil.doGet) ParseUtil(org.keycloak.client.registration.cli.util.ParseUtil) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) ConfigUtil.getRegistrationToken(org.keycloak.client.registration.cli.util.ConfigUtil.getRegistrationToken) IoUtil.warnfErr(org.keycloak.client.registration.cli.util.IoUtil.warnfErr) EOL(org.keycloak.client.registration.cli.util.OsUtil.EOL) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) PrintWriter(java.io.PrintWriter) HttpUtil.urlencode(org.keycloak.client.registration.cli.util.HttpUtil.urlencode) Arguments(org.jboss.aesh.cl.Arguments) StringWriter(java.io.StringWriter) DEFAULT_CONFIG_FILE_STRING(org.keycloak.client.registration.cli.util.ConfigUtil.DEFAULT_CONFIG_FILE_STRING) IOException(java.io.IOException) ConfigUtil.loadConfig(org.keycloak.client.registration.cli.util.ConfigUtil.loadConfig) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) JsonSerialization(org.keycloak.util.JsonSerialization) ConfigUtil.credentialsAvailable(org.keycloak.client.registration.cli.util.ConfigUtil.credentialsAvailable) PROMPT(org.keycloak.client.registration.cli.util.OsUtil.PROMPT) Option(org.jboss.aesh.cl.Option) List(java.util.List) CommandException(org.jboss.aesh.console.command.CommandException) IoUtil.readFully(org.keycloak.client.registration.cli.util.IoUtil.readFully) AdapterConfig(org.keycloak.representations.adapters.config.AdapterConfig) ConfigData(org.keycloak.client.registration.cli.config.ConfigData) InputStream(java.io.InputStream) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) ConfigData(org.keycloak.client.registration.cli.config.ConfigData) InputStream(java.io.InputStream) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) IOException(java.io.IOException) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 3 with EndpointType

use of org.keycloak.client.registration.cli.common.EndpointType in project keycloak by keycloak.

the class AttrsCmd method execute.

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
    try {
        processGlobalOptions();
        if (printHelp()) {
            return CommandResult.SUCCESS;
        }
        EndpointType regType = EndpointType.DEFAULT;
        PrintStream out = commandInvocation.getShell().out();
        if (endpoint != null) {
            regType = EndpointType.of(endpoint);
        }
        if (args != null) {
            if (args.size() > 1) {
                throw new IllegalArgumentException("Invalid option: " + args.get(1));
            }
            attr = args.get(0);
        }
        Class type = regType == EndpointType.DEFAULT ? ClientRepresentation.class : (regType == EndpointType.OIDC ? OIDCClientRepresentation.class : null);
        if (type == null) {
            throw new IllegalArgumentException("Endpoint not supported: " + regType);
        }
        AttributeKey key = attr == null ? new AttributeKey() : new AttributeKey(attr);
        Field f = ReflectionUtil.resolveField(type, key);
        String ts = f != null ? ReflectionUtil.getTypeString(null, f) : null;
        if (f == null) {
            out.printf("Attributes for %s format:\n", regType.getEndpoint());
            LinkedHashMap<String, String> items = getAttributeListWithJSonTypes(type, key);
            for (Map.Entry<String, String> item : items.entrySet()) {
                out.printf("  %-40s %s\n", item.getKey(), item.getValue());
            }
        } else {
            out.printf("%-40s %s", attr, ts);
            boolean eol = false;
            Type t = f.getGenericType();
            if (isListType(f.getType()) && t instanceof ParameterizedType) {
                t = ((ParameterizedType) t).getActualTypeArguments()[0];
                if (!isBasicType(t) && t instanceof Class) {
                    eol = true;
                    System.out.printf(", where value is:\n", ts);
                    LinkedHashMap<String, String> items = ReflectionUtil.getAttributeListWithJSonTypes((Class) t, null);
                    for (Map.Entry<String, String> item : items.entrySet()) {
                        out.printf("    %-36s %s\n", item.getKey(), item.getValue());
                    }
                }
            } else if (isMapType(f.getType()) && t instanceof ParameterizedType) {
                t = ((ParameterizedType) t).getActualTypeArguments()[1];
                if (!isBasicType(t) && t instanceof Class) {
                    eol = true;
                    out.printf(", where value is:\n", ts);
                    LinkedHashMap<String, String> items = ReflectionUtil.getAttributeListWithJSonTypes((Class) t, null);
                    for (Map.Entry<String, String> item : items.entrySet()) {
                        out.printf("    %-36s %s\n", item.getKey(), item.getValue());
                    }
                }
            }
            if (!eol) {
                // add end of line
                out.println();
            }
        }
        return CommandResult.SUCCESS;
    } finally {
        commandInvocation.stop();
    }
}
Also used : PrintStream(java.io.PrintStream) LinkedHashMap(java.util.LinkedHashMap) AttributeKey(org.keycloak.client.registration.cli.common.AttributeKey) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ReflectionUtil.isListType(org.keycloak.client.registration.cli.util.ReflectionUtil.isListType) ReflectionUtil.isMapType(org.keycloak.client.registration.cli.util.ReflectionUtil.isMapType) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) ReflectionUtil.isBasicType(org.keycloak.client.registration.cli.util.ReflectionUtil.isBasicType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

EndpointType (org.keycloak.client.registration.cli.common.EndpointType)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Arguments (org.jboss.aesh.cl.Arguments)1 CommandDefinition (org.jboss.aesh.cl.CommandDefinition)1 Option (org.jboss.aesh.cl.Option)1 CommandException (org.jboss.aesh.console.command.CommandException)1 CommandResult (org.jboss.aesh.console.command.CommandResult)1 CommandInvocation (org.jboss.aesh.console.command.invocation.CommandInvocation)1 AttributeKey (org.keycloak.client.registration.cli.common.AttributeKey)1 ConfigData (org.keycloak.client.registration.cli.config.ConfigData)1