예제 #1
0
  protected void assertUser(
      String expectedUsername,
      String expectedEmail,
      String expectedFirstname,
      String expectedLastname,
      boolean updateProfileActionExpected) {
    KeycloakRule keycloakRule = getKeycloakRule();

    KeycloakSession session = keycloakRule.startSession();
    try {
      RealmManager manager = new RealmManager(session);
      RealmModel appRealm = manager.getRealm("test");

      UserModel user = session.users().getUserByUsername(expectedUsername, appRealm);
      Assert.assertNotNull(user);
      Assert.assertEquals(user.getEmail(), expectedEmail);
      Assert.assertEquals(user.getFirstName(), expectedFirstname);
      Assert.assertEquals(user.getLastName(), expectedLastname);

      if (updateProfileActionExpected) {
        Assert.assertEquals(
            UserModel.RequiredAction.UPDATE_PROFILE.toString(),
            user.getRequiredActions().iterator().next());
      } else {
        Assert.assertTrue(user.getRequiredActions().isEmpty());
      }
    } finally {
      keycloakRule.stopSession(session, true);
    }
  }
예제 #2
0
  private static String createAdminToken(String username, String realm) {
    KeycloakSession session = keycloakRule.startSession();
    try {
      RealmManager manager = new RealmManager(session);

      RealmModel adminRealm = manager.getRealm(realm);
      ClientModel adminConsole = adminRealm.getClientByClientId(Constants.ADMIN_CLI_CLIENT_ID);
      TokenManager tm = new TokenManager();
      UserModel admin = session.users().getUserByUsername(username, adminRealm);
      ClientSessionModel clientSession =
          session.sessions().createClientSession(adminRealm, adminConsole);
      clientSession.setNote(OIDCLoginProtocol.ISSUER, "http://localhost:8081/auth/realms/" + realm);
      UserSessionModel userSession =
          session
              .sessions()
              .createUserSession(adminRealm, admin, "admin", null, "form", false, null, null);
      AccessToken token =
          tm.createClientAccessToken(
              session,
              tm.getAccess(null, true, adminConsole, admin),
              adminRealm,
              adminConsole,
              admin,
              userSession,
              clientSession);
      return tm.encodeToken(adminRealm, token);
    } finally {
      keycloakRule.stopSession(session, true);
    }
  }
예제 #3
0
  @Path("{id}")
  public RealmAdminResource getRealmAdmin(
      @Context final HttpHeaders headers, @PathParam("id") final String id) {
    RealmManager realmManager = new RealmManager(session);
    RealmModel realm = realmManager.getRealm(id);
    if (realm == null) throw new NotFoundException();

    RealmAdminResource adminResource = new RealmAdminResource(admin, realm);
    resourceContext.initResource(adminResource);
    return adminResource;
  }
예제 #4
0
  @POST
  @Consumes("application/json")
  public Response importRealm(@Context final UriInfo uriInfo, final RealmRepresentation rep) {
    logger.debug("importRealm: {0}", rep.getRealm());
    RealmManager realmManager = new RealmManager(session);
    if (realmManager.getRealm(rep.getRealm()) != null) {
      return Flows.errors().exists("Realm " + rep.getRealm() + " already exists");
    }

    RealmModel realm = realmManager.importRealm(rep, admin);
    URI location = realmUrl(uriInfo).build(realm.getId());
    logger.debug("imported realm success, sending back: {0}", location.toString());
    return Response.created(location).build();
  }
예제 #5
0
  protected void setupKeycloak() {
    KeycloakSession session = server.getKeycloakSessionFactory().createSession();
    session.getTransaction().begin();

    try {
      RealmManager manager = new RealmManager(session);

      RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());

      configure(manager, adminstrationRealm);

      session.getTransaction().commit();
    } finally {
      session.close();
    }
  }
예제 #6
0
  protected void removeAllUsers() {
    KeycloakRule keycloakRule = getKeycloakRule();

    KeycloakSession session = keycloakRule.startSession();
    try {
      RealmManager manager = new RealmManager(session);

      RealmModel appRealm = manager.getRealm("test");
      List<UserModel> users = session.userStorage().getUsers(appRealm, true);
      for (UserModel user : users) {
        if (!user.getUsername().equals(AssertEvents.DEFAULT_USERNAME)) {
          session.userStorage().removeUser(appRealm, user);
        }
      }

      Assert.assertEquals(1, session.userStorage().getUsers(appRealm, true).size());
    } finally {
      keycloakRule.stopSession(session, true);
    }
  }
예제 #7
0
  private static String createToken() {
    KeycloakSession session = keycloakRule.startSession();
    try {
      RealmManager manager = new RealmManager(session);

      RealmModel adminRealm = manager.getRealm(Config.getAdminRealm());
      ApplicationModel adminConsole =
          adminRealm.getApplicationByName(Constants.ADMIN_CONSOLE_APPLICATION);
      TokenManager tm = new TokenManager();
      UserModel admin = session.users().getUserByUsername("admin", adminRealm);
      UserSessionModel userSession =
          session.sessions().createUserSession(adminRealm, admin, "admin", null, "form", false);
      AccessToken token =
          tm.createClientAccessToken(
              tm.getAccess(null, adminConsole, admin),
              adminRealm,
              adminConsole,
              admin,
              userSession);
      return tm.encodeToken(adminRealm, token);
    } finally {
      keycloakRule.stopSession(session, true);
    }
  }