示例#1
0
  /**
   * Get offline sessions for client
   *
   * <p>Returns a list of offline user sessions associated with this client
   *
   * @param firstResult Paging offset
   * @param maxResults Maximum results size (defaults to 100)
   * @return
   */
  @Path("offline-sessions")
  @GET
  @NoCache
  @Produces(MediaType.APPLICATION_JSON)
  public List<UserSessionRepresentation> getOfflineUserSessions(
      @QueryParam("first") Integer firstResult, @QueryParam("max") Integer maxResults) {
    auth.requireView();

    if (client == null) {
      throw new NotFoundException("Could not find client");
    }

    firstResult = firstResult != null ? firstResult : -1;
    maxResults = maxResults != null ? maxResults : Constants.DEFAULT_MAX_RESULTS;
    List<UserSessionRepresentation> sessions = new ArrayList<UserSessionRepresentation>();
    List<UserSessionModel> userSessions =
        session
            .sessions()
            .getOfflineUserSessions(client.getRealm(), client, firstResult, maxResults);
    for (UserSessionModel userSession : userSessions) {
      UserSessionRepresentation rep = ModelToRepresentation.toRepresentation(userSession);

      // Update lastSessionRefresh with the timestamp from clientSession
      for (ClientSessionModel clientSession : userSession.getClientSessions()) {
        if (client.getId().equals(clientSession.getClient().getId())) {
          rep.setLastAccess(Time.toMillis(clientSession.getTimestamp()));
          break;
        }
      }

      sessions.add(rep);
    }
    return sessions;
  }
示例#2
0
  /**
   * Get application offline session count
   *
   * <p>Returns a number of offline user sessions associated with this client
   *
   * <p>{ "count": number }
   *
   * @return
   */
  @Path("offline-session-count")
  @GET
  @NoCache
  @Produces(MediaType.APPLICATION_JSON)
  public Map<String, Long> getOfflineSessionCount() {
    auth.requireView();

    if (client == null) {
      throw new NotFoundException("Could not find client");
    }

    Map<String, Long> map = new HashMap<>();
    map.put("count", session.sessions().getOfflineSessionsCount(client.getRealm(), client));
    return map;
  }
 /**
  * Get client session stats
  *
  * <p>Returns a JSON map. The key is the client id, the value is the number of sessions that
  * currently are active with that client. Only clients that actually have a session associated
  * with them will be in this map.
  *
  * @return
  */
 @Path("client-session-stats")
 @GET
 @NoCache
 @Produces(MediaType.APPLICATION_JSON)
 public List<Map<String, String>> getClientSessionStats() {
   auth.requireView();
   List<Map<String, String>> data = new LinkedList<Map<String, String>>();
   for (ClientModel client : realm.getClients()) {
     int size = session.sessions().getActiveUserSessions(client.getRealm(), client);
     if (size == 0) continue;
     Map<String, String> map = new HashMap<String, String>();
     map.put("id", client.getId());
     map.put("clientId", client.getClientId());
     map.put("active", size + "");
     data.add(map);
   }
   return data;
 }
示例#4
0
  /**
   * Get user sessions for client
   *
   * <p>Returns a list of user sessions associated with this client
   *
   * @param firstResult Paging offset
   * @param maxResults Maximum results size (defaults to 100)
   * @return
   */
  @Path("user-sessions")
  @GET
  @NoCache
  @Produces(MediaType.APPLICATION_JSON)
  public List<UserSessionRepresentation> getUserSessions(
      @QueryParam("first") Integer firstResult, @QueryParam("max") Integer maxResults) {
    auth.requireView();

    if (client == null) {
      throw new NotFoundException("Could not find client");
    }

    firstResult = firstResult != null ? firstResult : -1;
    maxResults = maxResults != null ? maxResults : Constants.DEFAULT_MAX_RESULTS;
    List<UserSessionRepresentation> sessions = new ArrayList<UserSessionRepresentation>();
    for (UserSessionModel userSession :
        session.sessions().getUserSessions(client.getRealm(), client, firstResult, maxResults)) {
      UserSessionRepresentation rep = ModelToRepresentation.toRepresentation(userSession);
      sessions.add(rep);
    }
    return sessions;
  }
示例#5
0
  public void enableServiceAccount(ClientModel client) {
    client.setServiceAccountsEnabled(true);

    // Add dedicated user for this service account
    if (realmManager.getSession().users().getServiceAccount(client) == null) {
      String username = ServiceAccountConstants.SERVICE_ACCOUNT_USER_PREFIX + client.getClientId();
      logger.debugf("Creating service account user '%s'", username);

      // Don't use federation for service account user
      UserModel user = realmManager.getSession().userStorage().addUser(client.getRealm(), username);
      user.setEnabled(true);
      user.setEmail(username + "@placeholder.org");
      user.setServiceAccountClientLink(client.getId());
    }

    // Add protocol mappers to retrieve clientId in access token
    if (client.getProtocolMapperByName(
            OIDCLoginProtocol.LOGIN_PROTOCOL, ServiceAccountConstants.CLIENT_ID_PROTOCOL_MAPPER)
        == null) {
      logger.debugf(
          "Creating service account protocol mapper '%s' for client '%s'",
          ServiceAccountConstants.CLIENT_ID_PROTOCOL_MAPPER, client.getClientId());
      ProtocolMapperModel protocolMapper =
          UserSessionNoteMapper.createClaimMapper(
              ServiceAccountConstants.CLIENT_ID_PROTOCOL_MAPPER,
              ServiceAccountConstants.CLIENT_ID,
              ServiceAccountConstants.CLIENT_ID,
              "String",
              false,
              "",
              true,
              true);
      client.addProtocolMapper(protocolMapper);
    }

    // Add protocol mappers to retrieve hostname and IP address of client in access token
    if (client.getProtocolMapperByName(
            OIDCLoginProtocol.LOGIN_PROTOCOL, ServiceAccountConstants.CLIENT_HOST_PROTOCOL_MAPPER)
        == null) {
      logger.debugf(
          "Creating service account protocol mapper '%s' for client '%s'",
          ServiceAccountConstants.CLIENT_HOST_PROTOCOL_MAPPER, client.getClientId());
      ProtocolMapperModel protocolMapper =
          UserSessionNoteMapper.createClaimMapper(
              ServiceAccountConstants.CLIENT_HOST_PROTOCOL_MAPPER,
              ServiceAccountConstants.CLIENT_HOST,
              ServiceAccountConstants.CLIENT_HOST,
              "String",
              false,
              "",
              true,
              true);
      client.addProtocolMapper(protocolMapper);
    }

    if (client.getProtocolMapperByName(
            OIDCLoginProtocol.LOGIN_PROTOCOL,
            ServiceAccountConstants.CLIENT_ADDRESS_PROTOCOL_MAPPER)
        == null) {
      logger.debugf(
          "Creating service account protocol mapper '%s' for client '%s'",
          ServiceAccountConstants.CLIENT_ADDRESS_PROTOCOL_MAPPER, client.getClientId());
      ProtocolMapperModel protocolMapper =
          UserSessionNoteMapper.createClaimMapper(
              ServiceAccountConstants.CLIENT_ADDRESS_PROTOCOL_MAPPER,
              ServiceAccountConstants.CLIENT_ADDRESS,
              ServiceAccountConstants.CLIENT_ADDRESS,
              "String",
              false,
              "",
              true,
              true);
      client.addProtocolMapper(protocolMapper);
    }
  }