Skip to content

Commit

Permalink
Merge pull request #559 from SignalK/invalidate_wsc_token
Browse files Browse the repository at this point in the history
Fix SK access token validity checking
  • Loading branch information
mairas authored Feb 15, 2022
2 parents 87be799 + f828000 commit da6c487
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/sensesp/net/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void MDNSDiscovery::start() {
if (!MDNS.begin(hostname.c_str())) { // Start the mDNS responder for hostname.local
debugW("Error setting up mDNS responder");
} else {
debugI("mDNS responder started at %s", hostname);
debugI("mDNS responder started for hostname '%s'", hostname.c_str());
}
mdns_instance_name_set(hostname.c_str()); // mDNS hostname for ESP32
MDNS.addService("http", "tcp", 80);
Expand Down
2 changes: 2 additions & 0 deletions src/sensesp/net/networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ void Networking::wifi_station_connected() {
debugI("Connected to wifi, SSID: %s (signal: %d)", WiFi.SSID().c_str(),
WiFi.RSSI());
debugI("IP address of Device: %s", WiFi.localIP().toString().c_str());
debugI("Default route: %s", WiFi.gatewayIP().toString().c_str());
debugI("DNS server: %s", WiFi.dnsIP().toString().c_str());
this->emit(WifiState::kWifiConnectedToAP);
}

Expand Down
18 changes: 10 additions & 8 deletions src/sensesp/net/ws_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,6 @@ bool WSClient::get_mdns_service(String& server_address, uint16_t& server_port) {
}

void WSClient::connect() {
debugI("WSClient websocket connect attempt (state=%d)",
get_connection_state());

if (get_connection_state() != WSConnectionState::kWSDisconnected) {
return;
}
Expand Down Expand Up @@ -428,10 +425,11 @@ void WSClient::test_token(const String server_address,
HTTPClient http;

String url =
String("http://") + server_address + ":" + server_port + "/signalk/";
String("http://") + server_address + ":" + server_port + "/signalk/v1/stream";
debugD("Testing token with url %s", url.c_str());
http.begin(wifi_client_, url);
String full_token = String("JWT ") + auth_token_;
String full_token = String("Bearer ") + auth_token_;
debugD("Authorization: %s", full_token.c_str());
http.addHeader("Authorization", full_token.c_str());
int httpCode = http.GET();
if (httpCode > 0) {
Expand All @@ -444,8 +442,9 @@ void WSClient::test_token(const String server_address,
} else {
debugD("Returned payload is empty");
}
if (httpCode == 200) {
// our token is valid, go ahead and connect
if (httpCode == 426) {
// HTTP status 426 is "Upgrade Required", which is the expected
// response for a websocket connection.
debugD("Attempting to connect to Signal K Websocket...");
server_detected_ = true;
token_test_success_ = true;
Expand Down Expand Up @@ -480,10 +479,13 @@ void WSClient::send_access_request(const String server_address,
String json_req = "";
serializeJson(doc, json_req);

debugD("Access request: %s", json_req.c_str());

HTTPClient http;

String url = String("http://") + server_address + ":" + server_port +
"/signalk/v1/access/requests";
debugD("Access request url: %s", url.c_str());
http.begin(wifi_client_, url);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(json_req);
Expand Down Expand Up @@ -591,7 +593,7 @@ void WSClient::connect_ws(const String host, const uint16_t port) {
set_connection_state(WSConnectionState::kWSConnecting);
this->client_.begin(host, port, path);
this->client_.onEvent(webSocketClientEvent);
String full_token = String("JWT ") + auth_token_;
String full_token = String("Bearer ") + auth_token_;
this->client_.setAuthorization(full_token.c_str());
}

Expand Down

0 comments on commit da6c487

Please sign in to comment.