Search in sources :

Example 6 with RealmConfigData

use of org.keycloak.client.registration.cli.config.RealmConfigData in project keycloak by keycloak.

the class ConfigRegistrationTokenCmd method process.

public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
    List<String> args = new ArrayList<>();
    Iterator<String> it = parent.args.iterator();
    // skip the first argument 'registration-token'
    it.next();
    while (it.hasNext()) {
        String arg = it.next();
        switch(arg) {
            case "-d":
            case "--delete":
                {
                    delete = true;
                    break;
                }
            default:
                {
                    args.add(arg);
                }
        }
    }
    if (args.size() > 1) {
        throw new IllegalArgumentException("Invalid option: " + args.get(1));
    }
    String token = args.size() == 1 ? args.get(0) : null;
    if (server == null) {
        throw new IllegalArgumentException("Required option not specified: --server");
    }
    if (realm == null) {
        throw new IllegalArgumentException("Required option not specified: --realm");
    }
    if (clientId == null) {
        throw new IllegalArgumentException("Required option not specified: --client");
    }
    checkUnsupportedOptions("--user", user, "--password", password, "--secret", secret, "--keystore", keystore, "--storepass", storePass, "--keypass", keyPass, "--alias", alias, "--truststore", trustStore, "--trustpass", keyPass, "--no-config", booleanOptionForCheck(noconfig));
    if (!delete && token == null) {
        token = IoUtil.readSecret("Enter Registration Access Token: ", commandInvocation);
    }
    // now update the config
    processGlobalOptions();
    String registrationToken = token;
    saveMergeConfig(config -> {
        RealmConfigData rdata = config.getRealmConfigData(server, realm);
        if (delete) {
            if (rdata != null) {
                rdata.getClients().remove(clientId);
            }
        } else {
            config.ensureRealmConfigData(server, realm).getClients().put(clientId, registrationToken);
        }
    });
    return CommandResult.SUCCESS;
}
Also used : RealmConfigData(org.keycloak.client.registration.cli.config.RealmConfigData) ArrayList(java.util.ArrayList)

Example 7 with RealmConfigData

use of org.keycloak.client.registration.cli.config.RealmConfigData in project keycloak by keycloak.

the class KcRegUpdateTokenTest method testUpdateToken.

@Test
public void testUpdateToken() throws IOException {
    FileConfigHandler handler = initCustomConfigFile();
    ConfigUtil.setHandler(handler);
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        KcRegExec exe = execute("config credentials --config '" + configFile.getName() + "' --server " + serverUrl + " --realm master --user admin --password admin");
        assertExitCodeAndStreamSizes(exe, 0, 0, 1);
        // read current registration access token
        ConfigData data = ConfigUtil.loadConfig();
        RealmConfigData rdata = data.getRealmConfigData(serverUrl, "test");
        Assert.assertNull("realm info set", rdata);
        // update registration access token
        exe = execute("update-token --config '" + configFile.getName() + "' reg-cli-secret-direct  --server " + serverUrl + " --realm test --user user1 --password userpass");
        assertExitCodeAndStreamSizes(exe, 0, 0, 1);
        // read current registration token
        data = ConfigUtil.loadConfig();
        rdata = data.getRealmConfigData(serverUrl, "test");
        Assert.assertEquals("current session realm unchanged", "master", data.getRealm());
        Assert.assertNotNull("realm info set", rdata);
        Assert.assertNull("on the fly login was transient", rdata.getToken());
        Assert.assertNotNull("client info has registration access token", rdata.getClients().get("reg-cli-secret-direct"));
        // use --no-config and on-the-fly auth
        exe = execute("update-token reg-cli-secret-direct --no-config --server " + serverUrl + " --realm test --user user1 --password userpass");
        assertExitCodeAndStreamSizes(exe, 0, 1, 1);
        // save the token
        String token = exe.stdoutLines().get(0);
        // test that the token works
        exe = execute("get reg-cli-secret-direct --no-config --server " + serverUrl + " --realm test -t " + token);
        assertExitCodeAndStdErrSize(exe, 0, 0);
        ClientRepresentation client = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
        Assert.assertEquals("client representation returned", "reg-cli-secret-direct", client.getClientId());
    }
}
Also used : RealmConfigData(org.keycloak.client.registration.cli.config.RealmConfigData) FileConfigHandler(org.keycloak.client.registration.cli.config.FileConfigHandler) RealmConfigData(org.keycloak.client.registration.cli.config.RealmConfigData) ConfigData(org.keycloak.client.registration.cli.config.ConfigData) KcRegExec(org.keycloak.testsuite.cli.KcRegExec) TempFileResource(org.keycloak.testsuite.util.TempFileResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 8 with RealmConfigData

use of org.keycloak.client.registration.cli.config.RealmConfigData in project keycloak by keycloak.

the class KcRegTest method testUserLoginWithCustomConfig.

@Test
@AuthServerContainerExclude(AuthServer.REMOTE)
public void testUserLoginWithCustomConfig() {
    /*
         *  Test user login using a custom config file
         */
    FileConfigHandler handler = initCustomConfigFile();
    File configFile = new File(handler.getConfigFile());
    try {
        KcRegExec exe = execute("config credentials --server " + serverUrl + " --realm master" + " --user admin --password admin --config '" + configFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 0, 1);
        Assert.assertEquals("stderr first line", "Logging into " + serverUrl + " as user admin of realm master", exe.stderrLines().get(0));
        // make sure the config file exists, and has the right content
        ConfigData config = handler.loadConfig();
        Assert.assertEquals("serverUrl", serverUrl, config.getServerUrl());
        Assert.assertEquals("realm", "master", config.getRealm());
        RealmConfigData realmcfg = config.sessionRealmConfigData();
        Assert.assertNotNull("realm config data no null", realmcfg);
        Assert.assertEquals("realm cfg serverUrl", serverUrl, realmcfg.serverUrl());
        Assert.assertEquals("realm cfg realm", "master", realmcfg.realm());
        Assert.assertEquals("client id", "admin-cli", realmcfg.getClientId());
        Assert.assertNotNull("token not null", realmcfg.getToken());
        Assert.assertNotNull("refresh token not null", realmcfg.getRefreshToken());
        Assert.assertNotNull("token expires not null", realmcfg.getExpiresAt());
        Assert.assertNotNull("token expires in future", realmcfg.getExpiresAt() > System.currentTimeMillis());
        Assert.assertNotNull("refresh token expires not null", realmcfg.getRefreshExpiresAt());
        Assert.assertNotNull("refresh token expires in future", realmcfg.getRefreshExpiresAt() > System.currentTimeMillis());
        Assert.assertTrue("clients is empty", realmcfg.getClients().isEmpty());
    } finally {
        configFile.delete();
    }
}
Also used : RealmConfigData(org.keycloak.client.registration.cli.config.RealmConfigData) FileConfigHandler(org.keycloak.client.registration.cli.config.FileConfigHandler) RealmConfigData(org.keycloak.client.registration.cli.config.RealmConfigData) ConfigData(org.keycloak.client.registration.cli.config.ConfigData) KcRegExec(org.keycloak.testsuite.cli.KcRegExec) File(java.io.File) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test)

Aggregations

RealmConfigData (org.keycloak.client.registration.cli.config.RealmConfigData)8 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ConfigData (org.keycloak.client.registration.cli.config.ConfigData)2 FileConfigHandler (org.keycloak.client.registration.cli.config.FileConfigHandler)2 KcRegExec (org.keycloak.testsuite.cli.KcRegExec)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AccessTokenResponse (org.keycloak.representations.AccessTokenResponse)1 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)1 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)1 TempFileResource (org.keycloak.testsuite.util.TempFileResource)1