Skip to content

Commit

Permalink
Fix several issues related to PAM tokens. (#44)
Browse files Browse the repository at this point in the history
fix(PAM): Fix Auth Token related functions

Fix GrantToken, ParseToken and SetAuthToken functions.

fix(PAM): Fix SetSecretKeyAutomatically setting to set the key properly

SetSecretKeyAutomatically was called before Initialization phase is fully finished, so it could not set the key.

fix(UserID): Fix PubnubLog: Error: Pubnub user ID is not set. Aborting operation. false error.

This error was printed for no reason when user started and stopped "playing" the project without setting UserID.
  • Loading branch information
KGronek-Pubnub authored Feb 12, 2025
1 parent 82dd7c5 commit 9036e11
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 60 deletions.
11 changes: 10 additions & 1 deletion .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
name: unreal-engine
version: 0.2.4
version: 0.2.6
schema: 1
scm: github.com/pubnub/unreal-engine
changelog:
- date: 2024-02-12
version: 0.2.6
changes:
- type: bug
text: "Fix Auth Token related functions."
- type: bug
text: "Fix SetSecretKeyAutomatically setting to set the key properly."
- type: bug
text: "Fix 'PubnubLog: Error: Pubnub user ID is not set. Aborting operation.' false error."
- date: 2024-02-06
version: 0.2.5
changes:
Expand Down
4 changes: 2 additions & 2 deletions PubnubLibrary.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 5,
"VersionName": "0.2.5",
"Version": 6,
"VersionName": "0.2.6",
"FriendlyName": "Pubnub Unreal SDK",
"Description": "Quickly add interactive features to your game that scale without building your backend infrastructure.",
"Category": "Code",
Expand Down
97 changes: 42 additions & 55 deletions Source/PubnubLibrary/Private/PubnubSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,18 @@ FString UPubnubSubsystem::GetUserID()

void UPubnubSubsystem::SetSecretKey()
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
if(!CheckIsPubnubInitialized())
{return;}

QuickActionThread->AddFunctionToQueue( [this]
if(std::strlen(SecretKey) == 0)
{
SetSecretKey_priv();
});
PubnubError("Can't set Secret Key. Secret Key is empty.");
return;
}

//This function only changes data locally, doesn't do any networking operations, so no need to call it on separate thread
pubnub_set_secret_key(ctx_pub, SecretKey);
pubnub_set_secret_key(ctx_sub, SecretKey);
}

void UPubnubSubsystem::PublishMessage(FString Channel, FString Message, FPubnubPublishSettings PublishSettings)
Expand Down Expand Up @@ -349,11 +354,15 @@ void UPubnubSubsystem::SetAuthToken(FString Token)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, Token]
{
SetAuthToken_priv(Token);
});

if(!CheckIsUserIDSet())
{return;}

if(CheckIsFieldEmpty(Token, "Token", "SetAuthToken"))
{return;}

//This is just a setter, so no need to call it on a separate thread
pubnub_set_auth_token(ctx_pub, TCHAR_TO_ANSI(*Token));
}

void UPubnubSubsystem::FetchHistory(FString Channel, FOnFetchHistoryResponse OnFetchHistoryResponse, FPubnubFetchHistorySettings FetchHistorySettings)
Expand Down Expand Up @@ -850,6 +859,9 @@ void UPubnubSubsystem::StartPubnubSubscribeLoop()

FString UPubnubSubsystem::StringArrayToCommaSeparated(TArray<FString> StringArray)
{
if(StringArray.IsEmpty())
{return "";}

FString CommaSeparatedString;
for(FString StringElement : SubscribedChannels)
{
Expand Down Expand Up @@ -1070,13 +1082,12 @@ void UPubnubSubsystem::InitPubnub_priv()

pubnub_init(ctx_pub, PublishKey, SubscribeKey);
pubnub_init(ctx_sub, PublishKey, SubscribeKey);

IsInitialized = true;

if(PubnubSettings->SetSecretKeyAutomatically)
{
SetSecretKey();
}

IsInitialized = true;
}

void UPubnubSubsystem::DeinitPubnub_priv()
Expand Down Expand Up @@ -1116,18 +1127,6 @@ void UPubnubSubsystem::SetUserID_priv(FString UserID)
IsUserIDSet = true;
}

void UPubnubSubsystem::SetSecretKey_priv()
{
if(std::strlen(SecretKey) == 0)
{
PubnubError("Can't set Secret Key. Secret Key is empty.");
return;
}

pubnub_set_secret_key(ctx_pub, SecretKey);
pubnub_set_secret_key(ctx_sub, SecretKey);
}

void UPubnubSubsystem::PublishMessage_priv(FString Channel, FString Message, FPubnubPublishSettings PublishSettings)
{
if(!CheckIsUserIDSet())
Expand Down Expand Up @@ -1268,6 +1267,9 @@ void UPubnubSubsystem::UnsubscribeFromGroup_priv(FString GroupName)

void UPubnubSubsystem::UnsubscribeFromAll_priv()
{
if(SubscribedChannels.IsEmpty() && SubscribedGroups.IsEmpty())
{return;}

if(!CheckIsUserIDSet())
{return;}

Expand Down Expand Up @@ -1622,46 +1624,19 @@ void UPubnubSubsystem::ParseToken_priv(FString Token, FOnPubnubResponse OnParseT
if(CheckIsFieldEmpty(Token, "Token", "ParseToken"))
{return;}

pubnub_parse_token(ctx_pub, TCHAR_TO_ANSI(*Token));

pubnub_res PubnubResponse = pubnub_await(ctx_pub);
if(PubnubResponse != PNR_OK)
{
PubnubResponseError(PubnubResponse, "Failed to Parse Token.");
}

pubnub_chamebl_t grant_token_resp = pubnub_get_grant_token(ctx_pub);
if(!grant_token_resp.ptr)
{
PubnubError("Failed to get Parse Token - pointer to token is invalid.");
return;
}
char* TokenResponse = pubnub_parse_token(ctx_pub, TCHAR_TO_ANSI(*Token));

FString JsonResponse(grant_token_resp.ptr);
FString JsonResponse(TokenResponse);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, OnParseTokenResponse, JsonResponse]()
{
//Broadcast bound delegate with JsonResponse
OnParseTokenResponse.ExecuteIfBound(JsonResponse);
});
}

void UPubnubSubsystem::SetAuthToken_priv(FString Token)
{
if(!CheckIsUserIDSet())
{return;}

if(CheckIsFieldEmpty(Token, "Token", "SetAuthToken"))
{return;}

pubnub_set_auth_token(ctx_pub, TCHAR_TO_ANSI(*Token));

pubnub_res PubnubResponse = pubnub_await(ctx_pub);
if(PubnubResponse != PNR_OK)
{
PubnubResponseError(PubnubResponse, "Failed to Set Auth Token.");
}
//Free this char, as it's allocated with malloc inside of pubnub_parse_token
free(TokenResponse);
}

FString UPubnubSubsystem::FetchHistory_pn(FString Channel, FPubnubFetchHistorySettings FetchHistorySettings)
Expand Down Expand Up @@ -2520,6 +2495,7 @@ TSharedPtr<FJsonObject> UPubnubSubsystem::AddChannelPermissionsToJson(TArray<FSt
ChPerm.update = CurrentPermissions.Update;
ChPerm.manage = CurrentPermissions.Manage;
ChPerm.join = CurrentPermissions.Join;
ChPerm.create = false;
int PermBitMask = pubnub_get_grant_bit_mask_value(ChPerm);

JsonObject->SetNumberField(Channels[i], PermBitMask);
Expand Down Expand Up @@ -2550,6 +2526,12 @@ TSharedPtr<FJsonObject> UPubnubSubsystem::AddChannelGroupPermissionsToJson(TArra
struct pam_permission ChPerm;
ChPerm.read = CurrentPermissions.Read;
ChPerm.manage = CurrentPermissions.Manage;
ChPerm.write = false;
ChPerm.del = false;
ChPerm.get = false;
ChPerm.update = false;
ChPerm.join = false;
ChPerm.create = false;
int PermBitMask = pubnub_get_grant_bit_mask_value(ChPerm);

JsonObject->SetNumberField(ChannelGroups[i], PermBitMask);
Expand Down Expand Up @@ -2581,6 +2563,11 @@ TSharedPtr<FJsonObject> UPubnubSubsystem::AddUserPermissionsToJson(TArray<FStrin
ChPerm.del = CurrentPermissions.Delete;
ChPerm.get = CurrentPermissions.Get;
ChPerm.update = CurrentPermissions.Update;
ChPerm.read = false;
ChPerm.write = false;
ChPerm.manage = false;
ChPerm.join = false;
ChPerm.create = false;
int PermBitMask = pubnub_get_grant_bit_mask_value(ChPerm);

JsonObject->SetNumberField(Users[i], PermBitMask);
Expand Down
12 changes: 10 additions & 2 deletions Source/PubnubLibrary/Public/PubnubSubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
* Parses an access token and retrieves information about its permissions.
*
* @Note Requires the *Access Manager* add-on to be enabled for your key in the PubNub Admin Portal
*
* Permissions are written in bit mask int:
* READ = 1
* WRITE = 2
* MANAGE = 4
* DELETE = 8
* CREATE = 16
* GET = 32
* UPDATE = 64
* JOIN = 128
*
* @param Token The access token to parse.
* @param OnParseTokenResponse The callback function used to handle the result in JSON format.
Expand Down Expand Up @@ -812,7 +822,6 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
void InitPubnub_priv();
void DeinitPubnub_priv();
void SetUserID_priv(FString UserID);
void SetSecretKey_priv();
void PublishMessage_priv(FString Channel, FString Message, FPubnubPublishSettings PublishSettings = FPubnubPublishSettings());
void Signal_priv(FString Channel, FString Message, FPubnubSignalSettings SignalSettings = FPubnubSignalSettings());
void SubscribeToChannel_priv(FString Channel);
Expand All @@ -838,7 +847,6 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
void GrantToken_priv(FString PermissionObject, FOnPubnubResponse OnGrantTokenResponse);
void RevokeToken_priv(FString Token);
void ParseToken_priv(FString Token, FOnPubnubResponse OnParseTokenResponse);
void SetAuthToken_priv(FString Token);
FString FetchHistory_pn(FString Channel, FPubnubFetchHistorySettings FetchHistorySettings = FPubnubFetchHistorySettings());
void FetchHistory_JSON_priv(FString Channel, FOnPubnubResponse OnFetchHistoryResponse, FPubnubFetchHistorySettings FetchHistorySettings = FPubnubFetchHistorySettings());
void FetchHistory_DATA_priv(FString Channel, FOnFetchHistoryResponse OnFetchHistoryResponse, FPubnubFetchHistorySettings FetchHistorySettings = FPubnubFetchHistorySettings());
Expand Down
Binary file modified Source/ThirdParty/sdk/lib/IOS/libpubnub.a
Binary file not shown.
Binary file modified Source/ThirdParty/sdk/lib/MacOS/libpubnub.a
Binary file not shown.
Binary file modified Source/ThirdParty/sdk/lib/arm64/libpubnub.a
Binary file not shown.
Binary file modified Source/ThirdParty/sdk/lib/linux/libpubnub.a
Binary file not shown.
Binary file modified Source/ThirdParty/sdk/lib/win64/pubnub.lib
Binary file not shown.

0 comments on commit 9036e11

Please sign in to comment.