Skip to content

Commit d8fdac3

Browse files
committed
Merge pull request #4 from pubnub/auth
Auth param added
2 parents 4dd6a91 + f441466 commit d8fdac3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

PubNub.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,24 @@ bool PubNub::begin(const char *publish_key_, const char *subscribe_key_, const c
3939
subscribe_key = subscribe_key_;
4040
origin = origin_;
4141
uuid = NULL;
42+
auth = NULL;
4243
}
4344

4445
void PubNub::set_uuid(const char *uuid_)
4546
{
4647
uuid = uuid_;
4748
}
4849

50+
void PubNub::set_auth(const char *auth_)
51+
{
52+
auth = auth_;
53+
}
54+
4955
PubNub_BASE_CLIENT *PubNub::publish(const char *channel, const char *message, int timeout)
5056
{
5157
PubNub_BASE_CLIENT &client = publish_client;
5258
unsigned long t_start;
59+
int have_param = 0;
5360

5461
retry:
5562
t_start = millis();
@@ -92,7 +99,14 @@ PubNub_BASE_CLIENT *PubNub::publish(const char *channel, const char *message, in
9299
}
93100
}
94101

95-
enum PubNub_BH ret = this->_request_bh(client, t_start, timeout, '?');
102+
if (auth) {
103+
client.print(have_param ? '&' : '?');
104+
client.print("auth=");
105+
client.print(auth);
106+
have_param = 1;
107+
}
108+
109+
enum PubNub_BH ret = this->_request_bh(client, t_start, timeout, have_param ? '&' : '?');
96110
switch (ret) {
97111
case PubNub_BH_OK:
98112
/* Success and reached body, return handle to the client
@@ -115,6 +129,7 @@ PubSubClient *PubNub::subscribe(const char *channel, int timeout)
115129
{
116130
PubSubClient &client = subscribe_client;
117131
unsigned long t_start;
132+
int have_param = 0;
118133

119134
retry:
120135
t_start = millis();
@@ -136,9 +151,16 @@ PubSubClient *PubNub::subscribe(const char *channel, int timeout)
136151
if (uuid) {
137152
client.print("?uuid=");
138153
client.print(uuid);
154+
have_param = 1;
155+
}
156+
if (auth) {
157+
client.print(have_param ? '&' : '?');
158+
client.print("auth=");
159+
client.print(auth);
160+
have_param = 1;
139161
}
140162

141-
enum PubNub_BH ret = this->_request_bh(client, t_start, timeout, uuid ? '&' : '?');
163+
enum PubNub_BH ret = this->_request_bh(client, t_start, timeout, have_param ? '&' : '?');
142164
switch (ret) {
143165
case PubNub_BH_OK:
144166
/* Success and reached body. We need to eat '[' first,

PubNub.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ class PubNub {
152152
* that generates a random UUID (although not 100% reliable). */
153153
void set_uuid(const char *uuid);
154154

155+
/* Set the authorization key/token of PubNub client. This is useful
156+
* e.g. for access rights validation (PAM).
157+
*
158+
* Pass NULL to unset. The string is not copied over (just like
159+
* in begin()). */
160+
void set_auth(const char *auth);
161+
155162
/* Publish
156163
*
157164
* Send a message (assumed to be well-formed JSON) to a given channel.
@@ -215,6 +222,7 @@ class PubNub {
215222
const char *publish_key, *subscribe_key;
216223
const char *origin;
217224
const char *uuid;
225+
const char *auth;
218226

219227
PubNub_BASE_CLIENT publish_client, history_client;
220228
PubSubClient subscribe_client;

0 commit comments

Comments
 (0)