Skip to content

Commit a04f4ba

Browse files
committed
add setConfig function
1 parent 823dad9 commit a04f4ba

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

AuthClient.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ AuthClient::~AuthClient() {
1515

1616
}
1717

18-
void AuthClient::init(char* appid, char* scope, unsigned long bts) {
18+
void AuthClient::init(char *authendpoint, char* appid, char* scope, unsigned long bts) {
19+
this->authendpoint = authendpoint;
1920
this->appid = appid;
2021
this->scope = scope;
2122
this->bootts = bts;
@@ -24,7 +25,7 @@ void AuthClient::init(char* appid, char* scope, unsigned long bts) {
2425
bool AuthClient::connect(bool issecuremode) {
2526
int port = issecuremode?GEARAUTHSECUREPORT:GEARAUTHPORT;
2627
this->securemode = issecuremode;
27-
if (client->connect(GEARAUTHHOST,port)) {
28+
if (client->connect(authendpoint,port)) {
2829
return true;
2930
}
3031
else {
@@ -173,7 +174,7 @@ int AuthClient::getGearToken(char mode, char* token, char* tokensecret, char* en
173174
*flag = '\0';
174175

175176
strcpy(signbase,this->securemode?"POST&https%3A%2F%2F":"POST&http%3A%2F%2F");
176-
sprintf(strtail(signbase),"%s%%3A%d",GEARAUTHHOST,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT);
177+
sprintf(strtail(signbase),"%s%%3A%d",authendpoint,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT);
177178

178179
if (mode == _REQUESTTOKEN) {
179180
writeln("POST /api/rtoken HTTP/1.1");
@@ -183,7 +184,7 @@ int AuthClient::getGearToken(char mode, char* token, char* tokensecret, char* en
183184
writeln("POST /api/atoken HTTP/1.1");
184185
strcat(signbase,"%2Fapi%2Fatoken&");
185186
}
186-
sprintf(buff,"Host: %s:%d",GEARAUTHHOST,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT);
187+
sprintf(buff,"Host: %s:%d",authendpoint,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT);
187188
writeln(buff);
188189

189190
write("Authorization: OAuth ");

AuthClient.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#define MGREV "E8A1b"
2020
#define MAXVERIFIERSIZE 32
21+
#define MAXGEARAUTHSIZE 32
2122
#define TOKENSIZE 16
2223
#define TOKENSECRETSIZE 32
2324
#define MAXHEADERLINESIZE 350
@@ -33,16 +34,16 @@ class AuthClient {
3334
virtual ~AuthClient();
3435

3536
static void randomString(char* ,int);
36-
void init(char*, char*,unsigned long);
37+
void init(char*, char*, char*,unsigned long);
3738
bool connect(bool);
39+
char *authendpoint;
3840
void stop();
3941
void write_P(const char[]);
4042
void writeln_P(const char[]);
4143
void write(char*);
4244
void writeln(char*);
4345
bool readln(char*, size_t);
4446
int getGearToken(char, char*, char*, char*, char*, char*, char*, char *, char*, char*, char*);
45-
protected:
4647
private:
4748
Client* client;
4849
char* appid;
@@ -57,6 +58,6 @@ class AuthClient {
5758
char* strtail(char*);
5859
void strcat(char*, char*);
5960
void addParam(char*, char*, char*, bool);
60-
unsigned long bootts;
61+
unsigned long bootts;
6162
};
6263
#endif

MicroGear.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void MicroGear::initEndpoint(Client *client, char* endpoint) {
114114
char pstr[100];
115115
int port = this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT;
116116

117-
if(client->connect(GEARAUTHHOST,port)){
117+
if(client->connect(gearauth,port)){
118118
sprintf(pstr,"GET /api/endpoint/%s HTTP/1.1\r\n\r\n",this->gearkey);
119119
client->write((const uint8_t *)pstr,strlen(pstr));
120120

@@ -137,7 +137,7 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
137137
int port = (this->securemode)?GEARAUTHSECUREPORT:GEARAUTHPORT;
138138

139139
*bts = 0;
140-
if(client->connect(GEARAUTHHOST,port)){
140+
if(client->connect(gearauth,port)){
141141

142142
if (this->securemode) {
143143
WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
@@ -148,7 +148,7 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
148148
Serial.print("fingerprint loaded from eeprom : ");
149149
Serial.println(tstr);
150150
#endif
151-
if (clientsecure->verify(tstr, GEARAUTHHOST)) {
151+
if (clientsecure->verify(tstr, gearauth)) {
152152
#ifdef DEBUG_H
153153
Serial.println("fingerprint matched");
154154
#endif
@@ -209,6 +209,9 @@ MicroGear::MicroGear(Client& netclient ) {
209209
this->backoff = 10;
210210
this->retry = RETRY;
211211

212+
strcpy(this->gearauth,GEARAUTHHOST);
213+
this->gearauth[MAXGEARAUTHSIZE] = '\0';
214+
212215
this->eepromoffset = 0;
213216
cb_message = NULL;
214217
cb_connected = NULL;
@@ -283,7 +286,7 @@ void MicroGear::resetToken() {
283286
char revokecode[REVOKECODESIZE+1];
284287
int port = this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT;
285288

286-
if(sockclient->connect(GEARAUTHHOST,port)){
289+
if(sockclient->connect(gearauth,port)){
287290
readEEPROM(token,EEPROM_TOKENOFFSET,TOKENSIZE);
288291
readEEPROM(revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE);
289292
sprintf(pstr,"GET /api/revoke/%s/%s HTTP/1.1\r\n\r\n",token,revokecode);
@@ -514,7 +517,7 @@ int MicroGear::connectBroker(char* appid) {
514517

515518
if (authclient) delete(authclient);
516519
authclient = new AuthClient(*sockclient);
517-
authclient->init(appid,scope,bootts);
520+
authclient->init(gearauth,appid,scope,bootts);
518521

519522
tokenOK = getToken(this->gearkey,this->gearalias,token,tokensecret,endpoint);
520523
delete(authclient);
@@ -821,3 +824,13 @@ int MicroGear::state() {
821824
if (!mqttclient) return -9;
822825
else return this->mqttclient->state();
823826
}
827+
828+
int MicroGear::setConfig(char* key, char* value) {
829+
if (strcmp(key,"GEARAUTH")==0) {
830+
strncpy(gearauth,value,MAXGEARAUTHSIZE);
831+
return 1;
832+
}
833+
else {
834+
return 0;
835+
}
836+
}

MicroGear.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class MicroGear {
8686
int eepromoffset;
8787
bool eepromready;
8888
int backoff, retry;
89+
char gearauth[MAXGEARAUTHSIZE+1];
8990

9091
void* self;
9192
AuthClient* authclient;
@@ -98,7 +99,6 @@ class MicroGear {
9899
void syncTime(Client*, unsigned long*);
99100
void initEndpoint(Client*, char*);
100101
bool getToken(char*, char*, char*, char*, char*);
101-
102102
public:
103103
int constate;
104104
char* endpoint;
@@ -150,6 +150,7 @@ class MicroGear {
150150
void setEEPROMOffset(int);
151151
void readEEPROM(char*,int, int);
152152
void writeEEPROM(char*,int, int);
153+
int setConfig(char*, char*);
153154
};
154155

155156
#endif

0 commit comments

Comments
 (0)