Java Code Examples for org.keycloak.representations.idm.ClientRepresentation#setBaseUrl()

The following examples show how to use org.keycloak.representations.idm.ClientRepresentation#setBaseUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void createClientValidation() {
    ClientRepresentation rep = new ClientRepresentation();
    rep.setClientId("my-app");
    rep.setDescription("my-app description");
    rep.setEnabled(true);

    rep.setRootUrl("invalid");
    createClientExpectingValidationError(rep, "Invalid URL in rootUrl");

    rep.setRootUrl(null);
    rep.setBaseUrl("invalid");
    createClientExpectingValidationError(rep, "Invalid URL in baseUrl");

    rep.setRootUrl(null);
    rep.setBaseUrl("/valid");
    createClientExpectingSuccessfulClientCreation(rep);

    rep.setRootUrl("");
    rep.setBaseUrl("/valid");
    createClientExpectingSuccessfulClientCreation(rep);
}
 
Example 2
Source File: OAuth2OnlyTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
    ClientRepresentation client = new ClientRepresentation();
    client.setClientId("more-uris-client");
    client.setEnabled(true);
    client.setRedirectUris(Arrays.asList("http://localhost:8180/auth/realms/master/app/auth", "http://localhost:8180/foo",
          "https://localhost:8543/auth/realms/master/app/auth", "https://localhost:8543/foo"));
    client.setBaseUrl("http://localhost:8180/auth/realms/master/app/auth");

    testRealm.getClients().add(client);

    ClientRepresentation testApp = testRealm.getClients().stream()
            .filter(cl -> cl.getClientId().equals("test-app"))
            .findFirst().get();
    testApp.setImplicitFlowEnabled(true);
    trimRedirectUris(testApp);
}
 
Example 3
Source File: AbstractAdapterTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
/**
 * Modifies baseUrl, adminUrl and redirectUris for client based on real
 * deployment url of the app.
 *
 * @param realm
 * @param clientId
 * @param deploymentUrl
 */
protected void fixClientUrisUsingDeploymentUrl(RealmRepresentation realm, String clientId, String deploymentUrl) {
    for (ClientRepresentation client : realm.getClients()) {
        if (clientId.equals(client.getClientId())) {
            if (client.getBaseUrl() != null) {
                client.setBaseUrl(deploymentUrl);
            }
            if (client.getAdminUrl() != null) {
                client.setAdminUrl(deploymentUrl);
            }
            List<String> redirectUris = client.getRedirectUris();
            if (redirectUris != null) {
                List<String> newRedirectUris = new ArrayList<>();
                for (String uri : redirectUris) {
                    newRedirectUris.add(deploymentUrl + "/*");
                }
                client.setRedirectUris(newRedirectUris);
            }
        }
    }
}
 
Example 4
Source File: KcOidcBrokerConfiguration.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<ClientRepresentation> createConsumerClients() {
    ClientRepresentation client = new ClientRepresentation();
    client.setId("broker-app");
    client.setClientId("broker-app");
    client.setName("broker-app");
    client.setSecret("broker-app-secret");
    client.setEnabled(true);
    client.setDirectAccessGrantsEnabled(true);

    client.setRedirectUris(Collections.singletonList(getConsumerRoot() +
            "/auth/*"));

    client.setBaseUrl(getConsumerRoot() +
            "/auth/realms/" + REALM_CONS_NAME + "/app");

    return Collections.singletonList(client);
}
 
Example 5
Source File: CustomAuthFlowCookieTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Before
@Override
public void beforeTest() {
    super.beforeTest();

    ClientRepresentation testApp = new ClientRepresentation();
    testApp.setClientId("test-app");
    testApp.setEnabled(true);
    testApp.setBaseUrl(APP_ROOT);
    testApp.setRedirectUris(Arrays.asList(new String[]{APP_ROOT + "/*"}));
    testApp.setAdminUrl(APP_ROOT + "/logout");
    testApp.setSecret("password");
    Response response = testRealmResource().clients().create(testApp);
    assertEquals(201, response.getStatus());
    getCleanup().addClientUuid(ApiUtil.getCreatedId(response));
    response.close();
}
 
Example 6
Source File: AudienceTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
    // Create service client with some client role
    ClientRepresentation client1 = new ClientRepresentation();
    client1.setClientId("service-client");
    client1.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    client1.setBearerOnly(true);
    client1.setBaseUrl("http://foo/service-client");
    testRealm.getClients().add(client1);

    RoleRepresentation role1 = new RoleRepresentation();
    role1.setName("role1");
    testRealm.getRoles().getClient().put("service-client", Arrays.asList(role1));

    // Disable FullScopeAllowed for the 'test-app' client
    ClientRepresentation testApp = testRealm.getClients().stream().filter((ClientRepresentation client) -> {
        return "test-app".equals(client.getClientId());
    }).findFirst().get();

    testApp.setFullScopeAllowed(false);

    // Create sample user
    UserRepresentation user = UserBuilder.create()
            .id(userId)
            .username("john")
            .enabled(true)
            .email("[email protected]")
            .firstName("John")
            .lastName("Doe")
            .password("password")
            .role("account", "manage-account")
            .role("account", "view-profile")
            .role("service-client", "role1")
            .build();
    testRealm.getUsers().add(user);
}
 
Example 7
Source File: ClientTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void updateClientValidation() {
    ClientRepresentation rep = createClient();

    rep.setClientId("my-app");
    rep.setDescription("my-app description");
    rep.setEnabled(true);

    rep.setRootUrl("invalid");
    updateClientExpectingValidationError(rep, "Invalid URL in rootUrl");

    rep.setRootUrl(null);
    rep.setBaseUrl("invalid");
    updateClientExpectingValidationError(rep, "Invalid URL in baseUrl");

    ClientRepresentation stored = realm.clients().get(rep.getId()).toRepresentation();
    assertNull(stored.getRootUrl());
    assertNull(stored.getBaseUrl());

    rep.setRootUrl(null);
    rep.setBaseUrl("/valid");
    updateClientExpectingSuccessfulClientUpdate(rep, null, "/valid");

    rep.setRootUrl("");
    rep.setBaseUrl("/valid");
    updateClientExpectingSuccessfulClientUpdate(rep, "", "/valid");
}
 
Example 8
Source File: AbstractKeycloakTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public void fixAuthServerHostAndPortForClientRepresentation(ClientRepresentation cr) {
    cr.setBaseUrl(removeDefaultPorts(replaceAuthHostWithRealHost(cr.getBaseUrl())));
    cr.setAdminUrl(removeDefaultPorts(replaceAuthHostWithRealHost(cr.getAdminUrl())));

    if (cr.getRedirectUris() != null && !cr.getRedirectUris().isEmpty()) {
        List<String> fixedUrls = new ArrayList<>(cr.getRedirectUris().size());
        for (String url : cr.getRedirectUris()) {
            fixedUrls.add(removeDefaultPorts(replaceAuthHostWithRealHost(url)));
        }

        cr.setRedirectUris(fixedUrls);
    }
}
 
Example 9
Source File: AbstractAdapterTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected void modifyClientUrls(RealmRepresentation realm, String regex, String replacement) {
    if (realm.getClients() != null) {
        for (ClientRepresentation client : realm.getClients()) {
            String baseUrl = client.getBaseUrl();
            if (baseUrl != null) {
                client.setBaseUrl(baseUrl.replaceAll(regex, replacement));
            }
            String adminUrl = client.getAdminUrl();
            if (adminUrl != null) {
                client.setAdminUrl(adminUrl.replaceAll(regex, replacement));
            }
        }
    }
}
 
Example 10
Source File: AbstractSpringBootTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private ClientRepresentation createClient() {
    ClientRepresentation clientRepresentation = new ClientRepresentation();

    clientRepresentation.setId(CLIENT_ID);
    clientRepresentation.setSecret(SECRET);

    clientRepresentation.setBaseUrl(BASE_URL);
    clientRepresentation.setRedirectUris(Collections.singletonList(BASE_URL + "/*"));
    clientRepresentation.setAdminUrl(BASE_URL);

    return clientRepresentation;
}
 
Example 11
Source File: ClientTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void updateClient() {
    ClientRepresentation client = createClient();

    ClientRepresentation newClient = new ClientRepresentation();
    newClient.setId(client.getId());
    newClient.setClientId(client.getClientId());
    newClient.setBaseUrl("http://baseurl");

    realm.clients().get(client.getId()).update(newClient);

    assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, AdminEventPaths.clientResourcePath(client.getId()), newClient, ResourceType.CLIENT);

    ClientRepresentation storedClient = realm.clients().get(client.getId()).toRepresentation();

    assertClient(client, storedClient);

    newClient.setSecret("new-secret");

    realm.clients().get(client.getId()).update(newClient);

    assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, AdminEventPaths.clientResourcePath(client.getId()), newClient, ResourceType.CLIENT);

    storedClient = realm.clients().get(client.getId()).toRepresentation();
    assertClient(client, storedClient);
}
 
Example 12
Source File: AbstractKeycloakTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void modifyMainUrls(ClientRepresentation cr) {
    cr.setBaseUrl(replaceHttpValuesWithHttps(cr.getBaseUrl()));
    cr.setAdminUrl(replaceHttpValuesWithHttps(cr.getAdminUrl()));
}