Search in sources :

Example 1 with RealmConfigData

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

the class AbstractAuthOptionsCmd method initConfigData.

private void initConfigData(ConfigData data) {
    if (server != null)
        data.setServerUrl(server);
    if (realm != null)
        data.setRealm(realm);
    if (trustStore != null)
        data.setTruststore(trustStore);
    if (externalToken != null) {
        data.setExternalToken(externalToken);
    }
    RealmConfigData rdata = data.sessionRealmConfigData();
    if (clientId != null)
        rdata.setClientId(clientId);
    if (secret != null)
        rdata.setSecret(secret);
    String grantTypeForAuthentication = user == null ? OAuth2Constants.CLIENT_CREDENTIALS : OAuth2Constants.PASSWORD;
    rdata.setGrantTypeForAuthentication(grantTypeForAuthentication);
}
Also used : RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData)

Example 2 with RealmConfigData

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

the class AuthUtil method ensureToken.

public static String ensureToken(ConfigData config) {
    if (config.getExternalToken() != null) {
        return config.getExternalToken();
    }
    checkAuthInfo(config);
    RealmConfigData realmConfig = config.sessionRealmConfigData();
    long now = currentTimeMillis();
    // if it's less than 5s to expiry, renew it
    if (realmConfig.getExpiresAt() - now < 5000) {
        // if it's less than 5s to expiry, fail with credentials expired
        if (realmConfig.getRefreshExpiresAt() != null && realmConfig.getRefreshExpiresAt() - now < 5000) {
            throw new RuntimeException("Session has expired. Login again with '" + OsUtil.CMD + " config credentials'");
        }
        if (realmConfig.getSigExpiresAt() != null && realmConfig.getSigExpiresAt() - now < 5000) {
            throw new RuntimeException("Session has expired. Login again with '" + OsUtil.CMD + " config credentials'");
        }
        try {
            String authorization = null;
            StringBuilder body = new StringBuilder();
            if (realmConfig.getRefreshToken() != null) {
                body.append("grant_type=refresh_token").append("&refresh_token=").append(realmConfig.getRefreshToken());
            } else {
                body.append("grant_type=").append(realmConfig.getGrantTypeForAuthentication());
            }
            body.append("&client_id=").append(urlencode(realmConfig.getClientId()));
            if (realmConfig.getSigningToken() != null) {
                body.append("&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer").append("&client_assertion=").append(realmConfig.getSigningToken());
            } else if (realmConfig.getSecret() != null) {
                authorization = BasicAuthHelper.createHeader(realmConfig.getClientId(), realmConfig.getSecret());
            }
            InputStream result = doPost(realmConfig.serverUrl() + "/realms/" + realmConfig.realm() + "/protocol/openid-connect/token", APPLICATION_FORM_URL_ENCODED, APPLICATION_JSON, body.toString(), authorization);
            AccessTokenResponse token = JsonSerialization.readValue(result, AccessTokenResponse.class);
            saveMergeConfig(cfg -> {
                RealmConfigData realmData = cfg.sessionRealmConfigData();
                realmData.setToken(token.getToken());
                realmData.setRefreshToken(token.getRefreshToken());
                realmData.setExpiresAt(currentTimeMillis() + token.getExpiresIn() * 1000);
                if (token.getRefreshToken() != null) {
                    realmData.setRefreshExpiresAt(currentTimeMillis() + token.getRefreshExpiresIn() * 1000);
                }
            });
            return token.getToken();
        } catch (Exception e) {
            throw new RuntimeException("Failed to refresh access token - " + e.getMessage(), e);
        }
    }
    return realmConfig.getToken();
}
Also used : RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData) InputStream(java.io.InputStream) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with RealmConfigData

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

the class ConfigUtil method getEffectiveClientId.

public static String getEffectiveClientId(ConfigData config) {
    String clientId = DEFAULT_CLIENT;
    RealmConfigData realmData = config.sessionRealmConfigData();
    if (realmData != null && realmData.getClientId() != null) {
        clientId = realmData.getClientId();
    }
    return clientId;
}
Also used : RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData)

Example 4 with RealmConfigData

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

the class ConfigUtil method saveTokens.

public static void saveTokens(AccessTokenResponse tokens, String endpoint, String realm, String clientId, String signKey, Long sigExpiresAt, String secret, String grantTypeForAuthentication) {
    handler.saveMergeConfig(config -> {
        config.setServerUrl(endpoint);
        config.setRealm(realm);
        RealmConfigData realmConfig = config.ensureRealmConfigData(endpoint, realm);
        realmConfig.setToken(tokens.getToken());
        realmConfig.setRefreshToken(tokens.getRefreshToken());
        realmConfig.setSigningToken(signKey);
        realmConfig.setSecret(secret);
        realmConfig.setExpiresAt(System.currentTimeMillis() + tokens.getExpiresIn() * 1000);
        if (realmConfig.getRefreshToken() != null) {
            realmConfig.setRefreshExpiresAt(tokens.getRefreshExpiresIn() == 0 ? Long.MAX_VALUE : System.currentTimeMillis() + tokens.getRefreshExpiresIn() * 1000);
        }
        realmConfig.setSigExpiresAt(sigExpiresAt);
        realmConfig.setClientId(clientId);
        realmConfig.setGrantTypeForAuthentication(grantTypeForAuthentication);
    });
}
Also used : RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData)

Example 5 with RealmConfigData

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

the class KcAdmTest method testUserLoginWithCustomConfig.

@Test
public void testUserLoginWithCustomConfig() {
    /*
         *  Test user login using a custom config file
         */
    FileConfigHandler handler = initCustomConfigFile();
    File configFile = new File(handler.getConfigFile());
    try {
        KcAdmExec exe = KcAdmExec.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());
    } finally {
        configFile.delete();
    }
}
Also used : RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData) FileConfigHandler(org.keycloak.client.admin.cli.config.FileConfigHandler) RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData) ConfigData(org.keycloak.client.admin.cli.config.ConfigData) KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) File(java.io.File) Test(org.junit.Test)

Aggregations

RealmConfigData (org.keycloak.client.admin.cli.config.RealmConfigData)6 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 Test (org.junit.Test)1 ConfigData (org.keycloak.client.admin.cli.config.ConfigData)1 FileConfigHandler (org.keycloak.client.admin.cli.config.FileConfigHandler)1 AccessTokenResponse (org.keycloak.representations.AccessTokenResponse)1 KcAdmExec (org.keycloak.testsuite.cli.KcAdmExec)1