Skip to content

Commit b53a5bb

Browse files
committed
fix oauth signature problem when request a token in secure mode
1 parent 3a7aece commit b53a5bb

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

AuthClient.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ void AuthClient::init(char* appid, char* scope, unsigned long bts) {
2121
this->bootts = bts;
2222
}
2323

24-
bool AuthClient::connect(bool securemode) {
25-
int port = securemode?GEARAUTHSECUREPORT:GEARAUTHPORT;
24+
bool AuthClient::connect(bool isecuremode) {
25+
int port = isecuremode?GEARAUTHSECUREPORT:GEARAUTHPORT;
26+
this->securemode = isecuremode;
2627
if (client->connect(GEARAUTHHOST,port)) {
2728
return true;
2829
}
@@ -162,8 +163,8 @@ int AuthClient::getGearToken(char mode, char* token, char* tokensecret, char* en
162163
*endpoint = '\0';
163164
*flag = '\0';
164165

165-
strcpy(signbase,"POST&http%3A%2F%2F");
166-
sprintf(strtail(signbase),"%s%%3A%d",GEARAUTHHOST,GEARAUTHPORT);
166+
strcpy(signbase,this->securemode?"POST&https%3A%2F%2F":"POST&http%3A%2F%2F");
167+
sprintf(strtail(signbase),"%s%%3A%d",GEARAUTHHOST,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT);
167168

168169
if (mode == _REQUESTTOKEN) {
169170
writeln("POST /api/rtoken HTTP/1.1");
@@ -173,7 +174,7 @@ int AuthClient::getGearToken(char mode, char* token, char* tokensecret, char* en
173174
writeln("POST /api/atoken HTTP/1.1");
174175
strcat(signbase,"%2Fapi%2Fatoken&");
175176
}
176-
sprintf(buff,"Host: %s:%d",GEARAUTHHOST,GEARAUTHPORT);
177+
sprintf(buff,"Host: %s:%d",GEARAUTHHOST,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT);
177178
writeln(buff);
178179

179180
write("Authorization: OAuth ");

AuthClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define GEARAUTHPORT 8080
1010
#define GEARAUTHSECUREPORT 8081
1111

12-
#define MGREV "E8A1a"
12+
#define MGREV "E8A1b"
1313
#define MAXVERIFIERSIZE 32
1414
#define TOKENSIZE 16
1515
#define TOKENSECRETSIZE 32
@@ -40,6 +40,7 @@ class AuthClient {
4040
char* appid;
4141
char* gearid;
4242
char* scope;
43+
bool securemode;
4344
void writeout(char*, bool, bool);
4445
char* append(char*, char*, char);
4546
char* append_P(char*, char*, char);

MicroGear.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,25 @@ bool MicroGear::clientReadln(Client* client, char *buffer, size_t buflen) {
6767
return false;
6868
}
6969

70-
bool MicroGear::getHTTPReply(Client *client, char *buff, size_t buffsize) {
70+
int MicroGear::getHTTPReply(Client *client, char *buff, size_t buffsize) {
71+
int httpstatus = 0;
7172
char pline = 0;
7273
buff[0] = '\0';
74+
7375
while (true) {
7476
clientReadln(client, buff, buffsize);
75-
if (pline > 0) {
76-
return true;
77+
if (httpstatus==0) {
78+
if (strncmp(buff,"HTTP",4)==0) {
79+
buff[12] = '\0';
80+
httpstatus = atoi(buff+9);
81+
}
82+
}
83+
else {
84+
if (pline > 0) {
85+
return httpstatus;
86+
}
87+
if (strlen(buff)<1) pline++;
7788
}
78-
if (strlen(buff)<1) pline++;
7989
}
8090
}
8191

@@ -110,7 +120,7 @@ void MicroGear::initEndpoint(Client *client, char* endpoint) {
110120

111121
void MicroGear::syncTime(Client *client, unsigned long *bts) {
112122
char timestr[200];
113-
int port = this->securemode?GEARTIMESECUREPORT:GEARTIMEPORT;
123+
int port = (this->securemode)?GEARTIMESECUREPORT:GEARTIMEPORT;
114124

115125
strcpy(timestr,"GET /api/time HTTP/1.1\r\n\r\n");
116126

@@ -119,7 +129,6 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
119129

120130
delay(1000);
121131
getHTTPReply(client,timestr,200);
122-
123132
*bts = atol(timestr) - millis()/1000;
124133

125134
client->stop();

MicroGear.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "AuthClient.h"
1818
//#include "debug.h"
1919

20-
2120
#define GEARTIMEADDRESS "ga.netpie.io"
2221
#define GEARTIMEPORT 8080
2322
#define GEARTIMESECUREPORT 8081
@@ -86,7 +85,7 @@ class MicroGear {
8685
Client *sockclient;
8786

8887
bool connectBroker(char*);
89-
bool getHTTPReply(Client*, char*, size_t);
88+
int getHTTPReply(Client*, char*, size_t);
9089
bool clientReadln(Client*, char*, size_t);
9190
void syncTime(Client*, unsigned long*);
9291
void initEndpoint(Client*, char*);

0 commit comments

Comments
 (0)