Skip to content

Commit

Permalink
alexa: fix device discovery / state callback
Browse files Browse the repository at this point in the history
alexa device discovery/state calls use URL of type
/api/hash_id_key_whatever/lights

This is blocked when API is disabled and fails
also when http API is enabled because no apikey is
sent by amazon calls

This commit grabs the "whatever Amazon Hash" during
device recognition and save it. All requets to URLS
starting with /api/hash_id_key_whatever are now
passing the auth check
  • Loading branch information
aL1aL7 committed Oct 26, 2020
1 parent 85969e4 commit 56a5dcc
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion code/espurna/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,37 @@ bool _apiRequestCallback(AsyncWebServerRequest* request) {
return true;
}

bool needAuth = true;
if ( getSetting("alexaEnabled", 1 == ALEXA_ENABLED) )
{
String alexaHash = getSetting("alexaHash", "none");
if ( (url.startsWith("/api/" + alexaHash)) && (alexaHash != "none") )
{

needAuth = false;
} else
{
if (url.equals("/description.xml")) {
if ( alexaHash != "in_progress" )
{
setSetting("alexaHash", "in_progress");
}
} else if ( (url.startsWith("/api/")) && (url.endsWith("/lights")) && (alexaHash == "in_progress") )
{
alexaHash = url;
alexaHash.remove(alexaHash.length()-7,7); // strip "/lights"
alexaHash.remove(0,5); // strip "/api/"
setSetting("alexaHash", alexaHash);
needAuth = false;
} else if (alexaHash == "in_progress")
{
setSetting("alexaHash", "none");
}
}
}

if (!url.startsWith("/api/")) return false;
if (!apiAuthenticate(request)) return false;
if (needAuth && !apiAuthenticate(request)) return false;

return _apiDispatchRequest(url, request);

Expand Down

0 comments on commit 56a5dcc

Please sign in to comment.