Skip to content

Commit 9036e11

Browse files
Fix several issues related to PAM tokens. (#44)
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.
1 parent 82dd7c5 commit 9036e11

File tree

9 files changed

+64
-60
lines changed

9 files changed

+64
-60
lines changed

.pubnub.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
name: unreal-engine
2-
version: 0.2.4
2+
version: 0.2.6
33
schema: 1
44
scm: github.com/pubnub/unreal-engine
55
changelog:
6+
- date: 2024-02-12
7+
version: 0.2.6
8+
changes:
9+
- type: bug
10+
text: "Fix Auth Token related functions."
11+
- type: bug
12+
text: "Fix SetSecretKeyAutomatically setting to set the key properly."
13+
- type: bug
14+
text: "Fix 'PubnubLog: Error: Pubnub user ID is not set. Aborting operation.' false error."
615
- date: 2024-02-06
716
version: 0.2.5
817
changes:

PubnubLibrary.uplugin

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"FileVersion": 3,
3-
"Version": 5,
4-
"VersionName": "0.2.5",
3+
"Version": 6,
4+
"VersionName": "0.2.6",
55
"FriendlyName": "Pubnub Unreal SDK",
66
"Description": "Quickly add interactive features to your game that scale without building your backend infrastructure.",
77
"Category": "Code",

Source/PubnubLibrary/Private/PubnubSubsystem.cpp

+42-55
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,18 @@ FString UPubnubSubsystem::GetUserID()
9494

9595
void UPubnubSubsystem::SetSecretKey()
9696
{
97-
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
97+
if(!CheckIsPubnubInitialized())
9898
{return;}
9999

100-
QuickActionThread->AddFunctionToQueue( [this]
100+
if(std::strlen(SecretKey) == 0)
101101
{
102-
SetSecretKey_priv();
103-
});
102+
PubnubError("Can't set Secret Key. Secret Key is empty.");
103+
return;
104+
}
105+
106+
//This function only changes data locally, doesn't do any networking operations, so no need to call it on separate thread
107+
pubnub_set_secret_key(ctx_pub, SecretKey);
108+
pubnub_set_secret_key(ctx_sub, SecretKey);
104109
}
105110

106111
void UPubnubSubsystem::PublishMessage(FString Channel, FString Message, FPubnubPublishSettings PublishSettings)
@@ -349,11 +354,15 @@ void UPubnubSubsystem::SetAuthToken(FString Token)
349354
{
350355
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
351356
{return;}
352-
353-
QuickActionThread->AddFunctionToQueue( [this, Token]
354-
{
355-
SetAuthToken_priv(Token);
356-
});
357+
358+
if(!CheckIsUserIDSet())
359+
{return;}
360+
361+
if(CheckIsFieldEmpty(Token, "Token", "SetAuthToken"))
362+
{return;}
363+
364+
//This is just a setter, so no need to call it on a separate thread
365+
pubnub_set_auth_token(ctx_pub, TCHAR_TO_ANSI(*Token));
357366
}
358367

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

851860
FString UPubnubSubsystem::StringArrayToCommaSeparated(TArray<FString> StringArray)
852861
{
862+
if(StringArray.IsEmpty())
863+
{return "";}
864+
853865
FString CommaSeparatedString;
854866
for(FString StringElement : SubscribedChannels)
855867
{
@@ -1070,13 +1082,12 @@ void UPubnubSubsystem::InitPubnub_priv()
10701082

10711083
pubnub_init(ctx_pub, PublishKey, SubscribeKey);
10721084
pubnub_init(ctx_sub, PublishKey, SubscribeKey);
1073-
1085+
IsInitialized = true;
1086+
10741087
if(PubnubSettings->SetSecretKeyAutomatically)
10751088
{
10761089
SetSecretKey();
10771090
}
1078-
1079-
IsInitialized = true;
10801091
}
10811092

10821093
void UPubnubSubsystem::DeinitPubnub_priv()
@@ -1116,18 +1127,6 @@ void UPubnubSubsystem::SetUserID_priv(FString UserID)
11161127
IsUserIDSet = true;
11171128
}
11181129

1119-
void UPubnubSubsystem::SetSecretKey_priv()
1120-
{
1121-
if(std::strlen(SecretKey) == 0)
1122-
{
1123-
PubnubError("Can't set Secret Key. Secret Key is empty.");
1124-
return;
1125-
}
1126-
1127-
pubnub_set_secret_key(ctx_pub, SecretKey);
1128-
pubnub_set_secret_key(ctx_sub, SecretKey);
1129-
}
1130-
11311130
void UPubnubSubsystem::PublishMessage_priv(FString Channel, FString Message, FPubnubPublishSettings PublishSettings)
11321131
{
11331132
if(!CheckIsUserIDSet())
@@ -1268,6 +1267,9 @@ void UPubnubSubsystem::UnsubscribeFromGroup_priv(FString GroupName)
12681267

12691268
void UPubnubSubsystem::UnsubscribeFromAll_priv()
12701269
{
1270+
if(SubscribedChannels.IsEmpty() && SubscribedGroups.IsEmpty())
1271+
{return;}
1272+
12711273
if(!CheckIsUserIDSet())
12721274
{return;}
12731275

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

1625-
pubnub_parse_token(ctx_pub, TCHAR_TO_ANSI(*Token));
1626-
1627-
pubnub_res PubnubResponse = pubnub_await(ctx_pub);
1628-
if(PubnubResponse != PNR_OK)
1629-
{
1630-
PubnubResponseError(PubnubResponse, "Failed to Parse Token.");
1631-
}
1632-
1633-
pubnub_chamebl_t grant_token_resp = pubnub_get_grant_token(ctx_pub);
1634-
if(!grant_token_resp.ptr)
1635-
{
1636-
PubnubError("Failed to get Parse Token - pointer to token is invalid.");
1637-
return;
1638-
}
1627+
char* TokenResponse = pubnub_parse_token(ctx_pub, TCHAR_TO_ANSI(*Token));
16391628

1640-
FString JsonResponse(grant_token_resp.ptr);
1629+
FString JsonResponse(TokenResponse);
16411630

16421631
//Delegate needs to be executed back on Game Thread
16431632
AsyncTask(ENamedThreads::GameThread, [this, OnParseTokenResponse, JsonResponse]()
16441633
{
16451634
//Broadcast bound delegate with JsonResponse
16461635
OnParseTokenResponse.ExecuteIfBound(JsonResponse);
16471636
});
1648-
}
1649-
1650-
void UPubnubSubsystem::SetAuthToken_priv(FString Token)
1651-
{
1652-
if(!CheckIsUserIDSet())
1653-
{return;}
1654-
1655-
if(CheckIsFieldEmpty(Token, "Token", "SetAuthToken"))
1656-
{return;}
16571637

1658-
pubnub_set_auth_token(ctx_pub, TCHAR_TO_ANSI(*Token));
1659-
1660-
pubnub_res PubnubResponse = pubnub_await(ctx_pub);
1661-
if(PubnubResponse != PNR_OK)
1662-
{
1663-
PubnubResponseError(PubnubResponse, "Failed to Set Auth Token.");
1664-
}
1638+
//Free this char, as it's allocated with malloc inside of pubnub_parse_token
1639+
free(TokenResponse);
16651640
}
16661641

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

25252501
JsonObject->SetNumberField(Channels[i], PermBitMask);
@@ -2550,6 +2526,12 @@ TSharedPtr<FJsonObject> UPubnubSubsystem::AddChannelGroupPermissionsToJson(TArra
25502526
struct pam_permission ChPerm;
25512527
ChPerm.read = CurrentPermissions.Read;
25522528
ChPerm.manage = CurrentPermissions.Manage;
2529+
ChPerm.write = false;
2530+
ChPerm.del = false;
2531+
ChPerm.get = false;
2532+
ChPerm.update = false;
2533+
ChPerm.join = false;
2534+
ChPerm.create = false;
25532535
int PermBitMask = pubnub_get_grant_bit_mask_value(ChPerm);
25542536

25552537
JsonObject->SetNumberField(ChannelGroups[i], PermBitMask);
@@ -2581,6 +2563,11 @@ TSharedPtr<FJsonObject> UPubnubSubsystem::AddUserPermissionsToJson(TArray<FStrin
25812563
ChPerm.del = CurrentPermissions.Delete;
25822564
ChPerm.get = CurrentPermissions.Get;
25832565
ChPerm.update = CurrentPermissions.Update;
2566+
ChPerm.read = false;
2567+
ChPerm.write = false;
2568+
ChPerm.manage = false;
2569+
ChPerm.join = false;
2570+
ChPerm.create = false;
25842571
int PermBitMask = pubnub_get_grant_bit_mask_value(ChPerm);
25852572

25862573
JsonObject->SetNumberField(Users[i], PermBitMask);

Source/PubnubLibrary/Public/PubnubSubsystem.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
318318
* Parses an access token and retrieves information about its permissions.
319319
*
320320
* @Note Requires the *Access Manager* add-on to be enabled for your key in the PubNub Admin Portal
321+
*
322+
* Permissions are written in bit mask int:
323+
* READ = 1
324+
* WRITE = 2
325+
* MANAGE = 4
326+
* DELETE = 8
327+
* CREATE = 16
328+
* GET = 32
329+
* UPDATE = 64
330+
* JOIN = 128
321331
*
322332
* @param Token The access token to parse.
323333
* @param OnParseTokenResponse The callback function used to handle the result in JSON format.
@@ -812,7 +822,6 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
812822
void InitPubnub_priv();
813823
void DeinitPubnub_priv();
814824
void SetUserID_priv(FString UserID);
815-
void SetSecretKey_priv();
816825
void PublishMessage_priv(FString Channel, FString Message, FPubnubPublishSettings PublishSettings = FPubnubPublishSettings());
817826
void Signal_priv(FString Channel, FString Message, FPubnubSignalSettings SignalSettings = FPubnubSignalSettings());
818827
void SubscribeToChannel_priv(FString Channel);
@@ -838,7 +847,6 @@ class PUBNUBLIBRARY_API UPubnubSubsystem : public UGameInstanceSubsystem
838847
void GrantToken_priv(FString PermissionObject, FOnPubnubResponse OnGrantTokenResponse);
839848
void RevokeToken_priv(FString Token);
840849
void ParseToken_priv(FString Token, FOnPubnubResponse OnParseTokenResponse);
841-
void SetAuthToken_priv(FString Token);
842850
FString FetchHistory_pn(FString Channel, FPubnubFetchHistorySettings FetchHistorySettings = FPubnubFetchHistorySettings());
843851
void FetchHistory_JSON_priv(FString Channel, FOnPubnubResponse OnFetchHistoryResponse, FPubnubFetchHistorySettings FetchHistorySettings = FPubnubFetchHistorySettings());
844852
void FetchHistory_DATA_priv(FString Channel, FOnFetchHistoryResponse OnFetchHistoryResponse, FPubnubFetchHistorySettings FetchHistorySettings = FPubnubFetchHistorySettings());
119 KB
Binary file not shown.
2.37 KB
Binary file not shown.
3.6 KB
Binary file not shown.
-1.26 MB
Binary file not shown.
2.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)