bool NssCache::LoadJsonGroupsToCache()

in src/oslogin_utils.cc [270:319]


bool NssCache::LoadJsonGroupsToCache(string response, int* errnop) {
  Reset();
  *errnop = ENOENT;

  json_object* root = NULL;
  root = json_tokener_parse(response.c_str());
  if (root == NULL) {
    return false;
  }

  bool ret = false;
  int arraylen = 0;
  json_object* groups = NULL;

  // First grab the page token.
  json_object* page_token_object;
  if (json_object_object_get_ex(root, "nextPageToken", &page_token_object)) {
    page_token_ = json_object_get_string(page_token_object);
  } else {
    goto cleanup;
  }
  // A page_token of 0 for groups is different than for users. This is the last
  // page, but it WILL contain groups if there are any.
  if (page_token_ == "0") {
    on_last_page_ = true;
    page_token_ = "";
  }
  if (!json_object_object_get_ex(root, "posixGroups", &groups)) {
    // Valid JSON but no groups, set ENOMSG as a 'no groups' code.
    *errnop = ENOMSG;
    goto cleanup;
  }
  if (json_object_get_type(groups) != json_type_array) {
    goto cleanup;
  }
  arraylen = json_object_array_length(groups);
  if (arraylen == 0 || arraylen > cache_size_) {
    goto cleanup;
  }
  for (int i = 0; i < arraylen; i++) {
    json_object* group = json_object_array_get_idx(groups, i);
    entry_cache_.push_back(json_object_to_json_string_ext(group, JSON_C_TO_STRING_PLAIN));
  }
  ret = true;
  *errnop = 0;

cleanup:
  json_object_put(root);
  return ret;
}