From a69bf2c3e25d8414112afb34922020a44a435ea2 Mon Sep 17 00:00:00 2001 From: Mateusz Wiktor Date: Fri, 23 Oct 2026 11:40:25 +0200 Subject: [PATCH] docs: add snippets for Access Manager categories Co-Authored-By: Claude Sonnet 5.5 --- CMakeLists.txt | 1 + ...ubnub_sync_grant_token_categories_sample.c | 107 ++++++++++++++++++ make/common/targets_app_openssl.mk | 7 ++ openssl/posix.mk | 1 + openssl/windows.mk | 1 + 5 files changed, 117 insertions(+) create mode 100644 core/samples/pubnub_sync_grant_token_categories_sample.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d2872f3f..c88f37a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -885,6 +885,7 @@ if (${EXAMPLES}) if (${USE_GRANT_TOKEN_API}) set(EXAMPLE_LIST pubnub_sync_grant_token_sample + pubnub_sync_grant_token_categories_sample ${EXAMPLE_LIST}) endif () endif () diff --git a/core/samples/pubnub_sync_grant_token_categories_sample.c b/core/samples/pubnub_sync_grant_token_categories_sample.c new file mode 100644 index 00000000..bb9f30c3 --- /dev/null +++ b/core/samples/pubnub_sync_grant_token_categories_sample.c @@ -0,0 +1,107 @@ +/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */ +#include "pubnub_sync.h" + +#include "core/pubnub_helper.h" +#include "core/pubnub_crypto.h" +#include "core/pubnub_grant_token_api.h" +#include "core/pubnub_json_parse.h" + +#include +#include +#include + +// snippet.parseTokenCategories +// Prints the category permissions of a token, `cat` section of the parsed token. +// Each value is the permission bit mask, 32 is the `get` permission. +static void print_token_categories(pubnub_t* pb, char const* token) +{ + char* parsed = pubnub_parse_token(pb, token); + if (NULL == parsed) { + printf("pubnub_parse_token() failed\n"); + return; + } + + struct pbjson_elem parsed_elem = { parsed, parsed + strlen(parsed) }; + struct pbjson_elem categories; + struct pbjson_elem channels; + struct pbjson_elem uuids; + + if (jonmpOK != pbjson_get_object_value(&parsed_elem, "cat", &categories)) { + printf("The token has no category permissions\n"); + } + else { + if (jonmpOK == pbjson_get_object_value(&categories, "chan", &channels)) { + printf("List all channel metadata: %s\n", + pbjson_elem_equals_string(&channels, "32") ? "yes" : "no"); + } + if (jonmpOK == pbjson_get_object_value(&categories, "uuid", &uuids)) { + printf("List all uuid metadata: %s\n", + pbjson_elem_equals_string(&uuids, "32") ? "yes" : "no"); + } + } + + free(parsed); +} +// snippet.end + +// snippet.grantTokenCategories +// Grants a token that can list all channel metadata and all uuid metadata. +// The categories permissions need no resources, `32` is the only valid value (`get`). +static enum pubnub_res grant_token_categories(pubnub_t* pb) +{ + char const* perm_obj = + "{\"ttl\":60, \"uuid\":\"my_authorized_uuid\", \"permissions\":{" + "\"categories\":{\"channels\":32, \"uuids\":32}}}"; + enum pubnub_res res = pubnub_grant_token(pb, perm_obj); + + if (PNR_STARTED == res) { + res = pubnub_await(pb); + } + if (PNR_OK == res) { + pubnub_chamebl_t grant_token = pubnub_get_grant_token(pb); + printf("pubnub_grant_token() Response from Pubnub: %s\n", grant_token.ptr); + } + else { + printf("pubnub_grant_token() failed with code: %d('%s')\n", + res, + pubnub_res_2_string(res)); + } + + return res; +} +// snippet.end + +int main() +{ + char const* my_env_publish_key = getenv("PUBNUB_PUBLISH_KEY"); + char const* my_env_subscribe_key = getenv("PUBNUB_SUBSCRIBE_KEY"); + char const* my_env_secret_key = getenv("PUBNUB_SECRET_KEY"); + pubnub_t* pbp = pubnub_alloc(); + enum pubnub_res res; + + if (NULL == pbp) { + printf("Failed to allocate Pubnub context!\n"); + return -1; + } + + if (NULL == my_env_publish_key) { my_env_publish_key = "demo"; } + if (NULL == my_env_subscribe_key) { my_env_subscribe_key = "demo"; } + if (NULL == my_env_secret_key) { my_env_secret_key = "demo"; } + + pubnub_init(pbp, my_env_publish_key, my_env_subscribe_key); + pubnub_set_secret_key(pbp, my_env_secret_key); + pubnub_set_user_id(pbp, "my_user_id"); + + res = grant_token_categories(pbp); + if (PNR_OK == res) { + /* The token stays valid until the next transaction on the context */ + print_token_categories(pbp, pubnub_get_grant_token(pbp).ptr); + } + + if (PN_CANCEL_STARTED == pubnub_cancel(pbp)) { + pubnub_await(pbp); + } + pubnub_free(pbp); + + return (PNR_OK == res) ? 0 : 1; +} diff --git a/make/common/targets_app_openssl.mk b/make/common/targets_app_openssl.mk index 8707c7e0..1672854f 100644 --- a/make/common/targets_app_openssl.mk +++ b/make/common/targets_app_openssl.mk @@ -47,6 +47,13 @@ pubnub_sync_grant_token_sample$(APP_EXT): \ pubnub_sync_dynamiciv$(LIB_EXT) $(COMPILER) $(OUT_FLAG)$@ $(COMPILER_FLAGS) $(CPPFLAGS) $(PREREQUISITES) $(LDLIBS) +SYNC_GRANT_TOKEN_CATEGORIES_SOURCES_ = ../core/samples/pubnub_sync_grant_token_categories_sample.c +SYNC_GRANT_TOKEN_CATEGORIES_SOURCES = $(subst /,$(PATH_SEP),$(SYNC_GRANT_TOKEN_CATEGORIES_SOURCES_)) +pubnub_sync_grant_token_categories_sample$(APP_EXT): \ + $(SYNC_GRANT_TOKEN_CATEGORIES_SOURCES) \ + pubnub_sync_dynamiciv$(LIB_EXT) + $(COMPILER) $(OUT_FLAG)$@ $(COMPILER_FLAGS) $(CPPFLAGS) $(PREREQUISITES) $(LDLIBS) + SYNC_REVOKE_TOKEN_SOURCES_ = ../core/samples/pubnub_sync_revoke_token_sample.c SYNC_REVOKE_TOKEN_SOURCES = $(subst /,$(PATH_SEP),$(SYNC_REVOKE_TOKEN_SOURCES_)) pubnub_sync_revoke_token_sample$(APP_EXT): \ diff --git a/openssl/posix.mk b/openssl/posix.mk index 1c8fe46b..b8443013 100644 --- a/openssl/posix.mk +++ b/openssl/posix.mk @@ -36,6 +36,7 @@ all: \ pubnub_publish_via_post_sample$(APP_EXT) \ pubnub_publish_via_post_secretkey_sample$(APP_EXT) \ pubnub_sync_grant_token_sample$(APP_EXT) \ + pubnub_sync_grant_token_categories_sample$(APP_EXT) \ pubnub_sync_publish_retry$(APP_EXT) \ pubnub_sync_revoke_token_sample$(APP_EXT) \ pubnub_sync_sample$(APP_EXT) \ diff --git a/openssl/windows.mk b/openssl/windows.mk index 9d737381..5b1fbc0b 100644 --- a/openssl/windows.mk +++ b/openssl/windows.mk @@ -43,6 +43,7 @@ all: \ pubnub_publish_via_post_sample$(APP_EXT) \ pubnub_publish_via_post_secretkey_sample$(APP_EXT) \ pubnub_sync_grant_token_sample$(APP_EXT) \ + pubnub_sync_grant_token_categories_sample$(APP_EXT) \ pubnub_sync_publish_retry$(APP_EXT) \ pubnub_sync_revoke_token_sample$(APP_EXT) \ pubnub_sync_sample$(APP_EXT) \