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;
}
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();
}
}
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();
}
}
Aggregations