Esempio n. 1
0
  /**
   * Base path for the admin REST API for one particular realm.
   *
   * @param headers
   * @param name realm name (not id!)
   * @return
   */
  @Path("{realm}")
  public RealmAdminResource getRealmAdmin(
      @Context final HttpHeaders headers, @PathParam("realm") final String name) {
    RealmManager realmManager = new RealmManager(session);
    RealmModel realm = realmManager.getRealmByName(name);
    if (realm == null) throw new NotFoundException("Realm not found.");

    if (!auth.getRealm().equals(realmManager.getKeycloakAdminstrationRealm())
        && !auth.getRealm().equals(realm)) {
      throw new ForbiddenException();
    }
    RealmAuth realmAuth;

    if (auth.getRealm().equals(realmManager.getKeycloakAdminstrationRealm())) {
      realmAuth = new RealmAuth(auth, realm.getMasterAdminClient());
    } else {
      realmAuth =
          new RealmAuth(
              auth, realm.getClientByClientId(realmManager.getRealmAdminClientId(auth.getRealm())));
    }

    AdminEventBuilder adminEvent = new AdminEventBuilder(realm, auth, session, clientConnection);
    session.getContext().setRealm(realm);

    RealmAdminResource adminResource =
        new RealmAdminResource(realmAuth, realm, tokenManager, adminEvent);
    ResteasyProviderFactory.getInstance().injectProperties(adminResource);
    // resourceContext.initResource(adminResource);
    return adminResource;
  }
Esempio n. 2
0
  /**
   * Import a realm
   *
   * <p>Imports a realm from a full representation of that realm. Realm name must be unique.
   *
   * @param uriInfo
   * @param rep JSON representation of the realm
   * @return
   */
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  public Response importRealm(@Context final UriInfo uriInfo, final RealmRepresentation rep) {
    RealmManager realmManager = new RealmManager(session);
    realmManager.setContextPath(keycloak.getContextPath());
    if (!auth.getRealm().equals(realmManager.getKeycloakAdminstrationRealm())) {
      throw new ForbiddenException();
    }
    if (!auth.hasRealmRole(AdminRoles.CREATE_REALM)) {
      throw new ForbiddenException();
    }

    logger.debugv("importRealm: {0}", rep.getRealm());

    try {
      RealmModel realm = realmManager.importRealm(rep);
      grantPermissionsToRealmCreator(realm);

      URI location = AdminRoot.realmsUrl(uriInfo).path(realm.getName()).build();
      logger.debugv("imported realm success, sending back: {0}", location.toString());

      return Response.created(location).build();
    } catch (ModelDuplicateException e) {
      return ErrorResponse.exists("Realm with same name exists");
    }
  }
Esempio n. 3
0
 protected void addRealmRep(
     List<RealmRepresentation> reps, RealmModel realm, ClientModel realmManagementClient) {
   if (auth.hasAppRole(realmManagementClient, AdminRoles.VIEW_REALM)) {
     reps.add(ModelToRepresentation.toRepresentation(realm, false));
   } else if (auth.hasOneOfAppRole(realmManagementClient, AdminRoles.ALL_REALM_ROLES)) {
     RealmRepresentation rep = new RealmRepresentation();
     rep.setRealm(realm.getName());
     reps.add(rep);
   }
 }
Esempio n. 4
0
  private void grantPermissionsToRealmCreator(RealmModel realm) {
    if (auth.hasRealmRole(AdminRoles.ADMIN)) {
      return;
    }

    RealmModel adminRealm = new RealmManager(session).getKeycloakAdminstrationRealm();
    ClientModel realmAdminApp = realm.getMasterAdminClient();
    for (String r : AdminRoles.ALL_REALM_ROLES) {
      RoleModel role = realmAdminApp.getRole(r);
      auth.getUser().grantRole(role);
    }
  }
Esempio n. 5
0
  /**
   * General information about the server
   *
   * @param headers
   * @return
   */
  @Path("serverinfo")
  public ServerInfoAdminResource getServerInfo(@Context final HttpHeaders headers) {
    handlePreflightRequest();

    AdminAuth auth = authenticateRealmAdminRequest(headers);
    if (auth != null) {
      logger.debug("authenticated admin access for: " + auth.getUser().getUsername());
    }
    Cors.add(request)
        .allowedOrigins(auth.getToken())
        .allowedMethods("GET", "PUT", "POST", "DELETE")
        .auth()
        .build(response);

    ServerInfoAdminResource adminResource = new ServerInfoAdminResource();
    ResteasyProviderFactory.getInstance().injectProperties(adminResource);
    // resourceContext.initResource(adminResource);
    return adminResource;
  }
Esempio n. 6
0
  /**
   * Get accessible realms
   *
   * <p>Returns a list of accessible realms. The list is filtered based on what realms the caller is
   * allowed to view.
   *
   * @return
   */
  @GET
  @NoCache
  @Produces(MediaType.APPLICATION_JSON)
  public List<RealmRepresentation> getRealms() {
    RealmManager realmManager = new RealmManager(session);
    List<RealmRepresentation> reps = new ArrayList<RealmRepresentation>();
    if (auth.getRealm().equals(realmManager.getKeycloakAdminstrationRealm())) {
      List<RealmModel> realms = session.realms().getRealms();
      for (RealmModel realm : realms) {
        addRealmRep(reps, realm, realm.getMasterAdminClient());
      }
    } else {
      ClientModel adminApp =
          auth.getRealm().getClientByClientId(realmManager.getRealmAdminClientId(auth.getRealm()));
      addRealmRep(reps, auth.getRealm(), adminApp);
    }

    if (reps.isEmpty()) {
      throw new ForbiddenException();
    }

    logger.debug(("getRealms()"));
    return reps;
  }