Exemple #1
0
 public static ClientResource findClientByClientId(RealmResource realm, String clientId) {
   for (ClientRepresentation c : realm.clients().findAll()) {
     if (c.getClientId().equals(clientId)) {
       return realm.clients().get(c.getId());
     }
   }
   return null;
 }
  public static void uploadSP() {
    try {
      Keycloak keycloak =
          Keycloak.getInstance(
              "http://localhost:8081/auth",
              "master",
              "admin",
              "admin",
              Constants.ADMIN_CLI_CLIENT_ID,
              null);
      RealmResource admin = keycloak.realm("demo");

      admin.toRepresentation();

      ClientRepresentation clientRep =
          admin.convertClientDescription(
              IOUtils.toString(
                  SamlPicketlinkSPTest.class.getResourceAsStream("/saml/sp-metadata.xml")));
      Response response = admin.clients().create(clientRep);

      assertEquals(201, response.getStatus());

      keycloak.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  @Before
  public void addClients() {
    List<ClientRepresentation> clients = bc.createProviderClients(suiteContext);
    if (clients != null) {
      RealmResource providerRealm = adminClient.realm(bc.providerRealmName());
      for (ClientRepresentation client : clients) {
        log.debug("adding client " + client.getName() + " to realm " + bc.providerRealmName());

        providerRealm.clients().create(client);
      }
    }

    clients = bc.createConsumerClients(suiteContext);
    if (clients != null) {
      RealmResource consumerRealm = adminClient.realm(bc.consumerRealmName());
      for (ClientRepresentation client : clients) {
        log.debug("adding client " + client.getName() + " to realm " + bc.consumerRealmName());

        consumerRealm.clients().create(client);
      }
    }
  }
  public static void uploadSP(String AUTH_SERVER_URL) {
    try {
      Keycloak keycloak =
          Keycloak.getInstance(
              AUTH_SERVER_URL, "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID, null);
      RealmResource admin = keycloak.realm("demo");

      admin.toRepresentation();

      ClientRepresentation clientRep =
          admin.convertClientDescription(
              IOUtils.toString(
                  SamlAdapterTestStrategy.class.getResourceAsStream(
                      "/keycloak-saml/sp-metadata.xml")));
      Response response = admin.clients().create(clientRep);

      assertEquals(201, response.getStatus());

      keycloak.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
Exemple #5
0
  @Test
  public void addUserTest() throws Throwable {
    AddUser.main(new String[] {"-u", "addusertest-admin", "-p", "password"});
    assertEquals(1, dir.listFiles().length);

    List<RealmRepresentation> realms =
        JsonSerialization.readValue(
            new FileInputStream(new File(dir, "keycloak-add-user.json")),
            new TypeReference<List<RealmRepresentation>>() {});
    assertEquals(1, realms.size());
    assertEquals(1, realms.get(0).getUsers().size());

    UserRepresentation user = realms.get(0).getUsers().get(0);
    assertEquals(new Integer(100000), user.getCredentials().get(0).getHashIterations());
    assertNull(user.getCredentials().get(0).getValue());

    CredentialRepresentation credentials = user.getCredentials().get(0);

    assertEquals(Pbkdf2PasswordHashProvider.ID, credentials.getAlgorithm());
    assertEquals(new Integer(100000), credentials.getHashIterations());

    KeycloakServer server = new KeycloakServer();
    try {
      server.start();

      Keycloak keycloak =
          Keycloak.getInstance(
              "http://localhost:8081/auth",
              "master",
              "addusertest-admin",
              "password",
              Constants.ADMIN_CLI_CLIENT_ID);
      keycloak.realms().findAll();

      RealmRepresentation testRealm = new RealmRepresentation();
      testRealm.setEnabled(true);
      testRealm.setId("test");
      testRealm.setRealm("test");

      keycloak.realms().create(testRealm);

      RealmResource realm = keycloak.realm("master");

      List<UserRepresentation> users =
          realm.users().search("addusertest-admin", null, null, null, null, null);
      assertEquals(1, users.size());

      UserRepresentation created = users.get(0);
      assertNotNull(created.getCreatedTimestamp());

      UserResource userResource = realm.users().get(created.getId());

      List<RoleRepresentation> realmRoles = userResource.roles().realmLevel().listAll();

      assertRoles(realmRoles, "admin", "offline_access");

      List<ClientRepresentation> clients = realm.clients().findAll();
      String accountId = null;
      for (ClientRepresentation c : clients) {
        if (c.getClientId().equals("account")) {
          accountId = c.getId();
        }
      }

      List<RoleRepresentation> accountRoles = userResource.roles().clientLevel(accountId).listAll();

      assertRoles(accountRoles, "view-profile", "manage-account");

      keycloak.close();

      assertEquals(0, dir.listFiles().length);
    } finally {
      server.stop();
    }
  }