Search in sources :

Example 1 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class SAMLServletAdapterTest method createKeys.

private PublicKey createKeys(String priority) throws Exception {
    PublicKey publicKey = NEW_KEY_PAIR.getPublic();
    ComponentRepresentation rep = new ComponentRepresentation();
    rep.setName("mycomponent");
    rep.setParentId("demo");
    rep.setProviderId(ImportedRsaKeyProviderFactory.ID);
    rep.setProviderType(KeyProvider.class.getName());
    MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
    config.addFirst("priority", priority);
    config.addFirst(Attributes.PRIVATE_KEY_KEY, NEW_KEY_PRIVATE_KEY_PEM);
    rep.setConfig(config);
    testRealmResource().components().add(rep);
    return publicKey;
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyProvider(org.keycloak.keys.KeyProvider) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) PublicKey(java.security.PublicKey)

Example 2 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class OIDCPublicKeyRotationAdapterTest method generateNewRealmKey.

private void generateNewRealmKey() {
    String realmId = adminClient.realm(DEMO).toRepresentation().getId();
    ComponentRepresentation keys = new ComponentRepresentation();
    keys.setName("generated");
    keys.setProviderType(KeyProvider.class.getName());
    keys.setProviderId("rsa-generated");
    keys.setParentId(realmId);
    keys.setConfig(new MultivaluedHashMap<>());
    keys.getConfig().putSingle("priority", "150");
    Response response = adminClient.realm(DEMO).components().add(keys);
    assertEquals(201, response.getStatus());
    response.close();
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyProvider(org.keycloak.keys.KeyProvider) Response(javax.ws.rs.core.Response) HttpResponse(org.apache.http.HttpResponse)

Example 3 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class ClientRegistrationPoliciesTest method testRedirectUriWithDomain.

@Test
public void testRedirectUriWithDomain() throws Exception {
    // Change the policy to avoid checking hosts
    ComponentRepresentation trustedHostPolicyRep = findPolicyByProviderAndAuth(TrustedHostClientRegistrationPolicyFactory.PROVIDER_ID, getPolicyAnon());
    trustedHostPolicyRep.getConfig().putSingle(TrustedHostClientRegistrationPolicyFactory.HOST_SENDING_REGISTRATION_REQUEST_MUST_MATCH, "false");
    // Configure some trusted host and domain
    trustedHostPolicyRep.getConfig().put(TrustedHostClientRegistrationPolicyFactory.TRUSTED_HOSTS, Arrays.asList("www.host.com", "*.example.com"));
    realmResource().components().component(trustedHostPolicyRep.getId()).update(trustedHostPolicyRep);
    // Verify client can be created with the redirectUri from trusted host and domain
    OIDCClientRepresentation oidcClientRep = createRepOidc("http://www.host.com", "http://www.example.com");
    reg.oidc().create(oidcClientRep);
    // Remove domain from the config
    trustedHostPolicyRep.getConfig().put(TrustedHostClientRegistrationPolicyFactory.TRUSTED_HOSTS, Arrays.asList("www.host.com", "www1.example.com"));
    realmResource().components().component(trustedHostPolicyRep.getId()).update(trustedHostPolicyRep);
    // Check new client can't be created anymore
    oidcClientRep = createRepOidc("http://www.host.com", "http://www.example.com");
    assertOidcFail(ClientRegOp.CREATE, oidcClientRep, 403, "URL doesn't match");
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) Test(org.junit.Test)

Example 4 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class ClientRegistrationPoliciesTest method testClientDisabledPolicy.

@Test
// We would need to do domain name -> ip address to set trusted host
@AuthServerContainerExclude(AuthServer.REMOTE)
public void testClientDisabledPolicy() throws Exception {
    setTrustedHost("localhost");
    // Assert new client is enabled
    OIDCClientRepresentation client = create();
    String clientId = client.getClientId();
    ClientRepresentation clientRep = ApiUtil.findClientByClientId(realmResource(), clientId).toRepresentation();
    Assert.assertTrue(clientRep.isEnabled());
    // Add client-disabled policy
    ComponentRepresentation rep = new ComponentRepresentation();
    rep.setName("Clients disabled");
    rep.setParentId(REALM_NAME);
    rep.setProviderId(ClientDisabledClientRegistrationPolicyFactory.PROVIDER_ID);
    rep.setProviderType(ClientRegistrationPolicy.class.getName());
    rep.setSubType(getPolicyAnon());
    Response response = realmResource().components().add(rep);
    String policyId = ApiUtil.getCreatedId(response);
    response.close();
    // Assert new client is disabled
    client = create();
    clientId = client.getClientId();
    clientRep = ApiUtil.findClientByClientId(realmResource(), clientId).toRepresentation();
    Assert.assertFalse(clientRep.isEnabled());
    // Try enable client. Should fail
    clientRep.setEnabled(true);
    assertFail(ClientRegOp.UPDATE, clientRep, 403, "Not permitted to enable client");
    // Try update disabled client. Should pass
    clientRep.setEnabled(false);
    reg.update(clientRep);
    // Revert
    realmResource().components().component(policyId).remove();
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) ClientRegistrationPolicy(org.keycloak.services.clientregistration.policy.ClientRegistrationPolicy) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test)

Example 5 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class ClientRegistrationPoliciesTest method testMaxClientsPolicy.

@Test
// We would need to do domain name -> ip address to set trusted host
@AuthServerContainerExclude(AuthServer.REMOTE)
public void testMaxClientsPolicy() throws Exception {
    setTrustedHost("localhost");
    int clientsCount = realmResource().clients().findAll().size();
    int newClientsLimit = clientsCount + 1;
    // Allow to create one more client to current limit
    ComponentRepresentation maxClientsPolicyRep = findPolicyByProviderAndAuth(MaxClientsClientRegistrationPolicyFactory.PROVIDER_ID, getPolicyAnon());
    maxClientsPolicyRep.getConfig().putSingle(MaxClientsClientRegistrationPolicyFactory.MAX_CLIENTS, String.valueOf(newClientsLimit));
    realmResource().components().component(maxClientsPolicyRep.getId()).update(maxClientsPolicyRep);
    // I can register one new client
    OIDCClientRepresentation client = create();
    // I can't register more clients
    assertOidcFail(ClientRegOp.CREATE, createRepOidc(), 403, "It's allowed to have max " + newClientsLimit + " clients per realm");
    // Revert
    maxClientsPolicyRep.getConfig().putSingle(MaxClientsClientRegistrationPolicyFactory.MAX_CLIENTS, String.valueOf(10000));
    realmResource().components().component(maxClientsPolicyRep.getId()).update(maxClientsPolicyRep);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test)

Aggregations

ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)120 Test (org.junit.Test)56 Response (javax.ws.rs.core.Response)44 UserStorageProvider (org.keycloak.storage.UserStorageProvider)22 KeyProvider (org.keycloak.keys.KeyProvider)17 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)13 Before (org.junit.Before)12 KeysMetadataRepresentation (org.keycloak.representations.idm.KeysMetadataRepresentation)12 RealmModel (org.keycloak.models.RealmModel)10 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)10 KeyPair (java.security.KeyPair)9 UserModel (org.keycloak.models.UserModel)9 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)8 RealmResource (org.keycloak.admin.client.resource.RealmResource)7 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)7 ComponentModel (org.keycloak.component.ComponentModel)7 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)7 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)6 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)6 List (java.util.List)5