Qrome - updated with Basic Auth for OctoPrint

pull/14/head
Chrome Legion 2018-05-31 20:14:22 -07:00
parent 753b4b1660
commit f9b568403a
4 changed files with 40 additions and 8 deletions

View File

@ -23,14 +23,20 @@ SOFTWARE.
#include "OctoPrintClient.h" #include "OctoPrintClient.h"
OctoPrintClient::OctoPrintClient(String ApiKey, String server, int port) { OctoPrintClient::OctoPrintClient(String ApiKey, String server, int port, String user, String pass) {
updateOctoPrintClient(ApiKey, server, port); updateOctoPrintClient(ApiKey, server, port, user, pass);
} }
void OctoPrintClient::updateOctoPrintClient(String ApiKey, String server, int port) { void OctoPrintClient::updateOctoPrintClient(String ApiKey, String server, int port, String user, String pass) {
server.toCharArray(myServer, 100); server.toCharArray(myServer, 100);
myApiKey = ApiKey; myApiKey = ApiKey;
myPort = port; myPort = port;
encodedAuth = "";
if (user != "") {
String userpass = user + ":" + pass;
base64 b64;
encodedAuth = b64.encode(userpass, true);
}
} }
boolean OctoPrintClient::validate() { boolean OctoPrintClient::validate() {
@ -59,6 +65,10 @@ WiFiClient OctoPrintClient::getSubmitRequest(String apiGetData) {
printClient.println(apiGetData); printClient.println(apiGetData);
printClient.println("Host: " + String(myServer) + ":" + String(myPort)); printClient.println("Host: " + String(myServer) + ":" + String(myPort));
printClient.println("X-Api-Key: " + myApiKey); printClient.println("X-Api-Key: " + myApiKey);
if (encodedAuth != "") {
printClient.print("Authorization: ");
printClient.println("Basic " + encodedAuth);
}
printClient.println("User-Agent: ArduinoWiFi/1.1"); printClient.println("User-Agent: ArduinoWiFi/1.1");
printClient.println("Connection: close"); printClient.println("Connection: close");
if (printClient.println() == 0) { if (printClient.println() == 0) {

View File

@ -24,6 +24,7 @@ SOFTWARE.
#pragma once #pragma once
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <base64.h>
class OctoPrintClient { class OctoPrintClient {
@ -31,6 +32,7 @@ private:
char myServer[100]; char myServer[100];
int myPort = 80; int myPort = 80;
String myApiKey = ""; String myApiKey = "";
String encodedAuth = "";
void resetPrintData(); void resetPrintData();
boolean validate(); boolean validate();
@ -62,9 +64,9 @@ private:
public: public:
OctoPrintClient(String ApiKey, String server, int port); OctoPrintClient(String ApiKey, String server, int port, String user, String pass);
void getPrinterJobResults(); void getPrinterJobResults();
void updateOctoPrintClient(String ApiKey, String server, int port); void updateOctoPrintClient(String ApiKey, String server, int port, String user, String pass);
String getAveragePrintTime(); String getAveragePrintTime();
String getEstimatedPrintTime(); String getEstimatedPrintTime();

View File

@ -56,6 +56,8 @@ String OctoPrintApiKey = ""; // ApiKey from your User Account on OctoPrint
String OctoPrintHostName = "octopi";// Default 'octopi' -- or hostname if different (optional if your IP changes) String OctoPrintHostName = "octopi";// Default 'octopi' -- or hostname if different (optional if your IP changes)
String OctoPrintServer = ""; // IP or Address of your OctoPrint Server (DO NOT include http://) String OctoPrintServer = ""; // IP or Address of your OctoPrint Server (DO NOT include http://)
int OctoPrintPort = 80; // the port you are running your OctoPrint server on (usually 80); int OctoPrintPort = 80; // the port you are running your OctoPrint server on (usually 80);
String OctoAuthUser = ""; // only used if you have haproxy or basic athentintication turned on (not default)
String OctoAuthPass = ""; // only used with haproxy or basic auth (only needed if you must authenticate)
const int WEBSERVER_PORT = 80; // The port you can access this device on over HTTP const int WEBSERVER_PORT = 80; // The port you can access this device on over HTTP
const boolean WEBSERVER_ENABLED = true; // Device will provide a web interface via http://[ip]:[port]/ const boolean WEBSERVER_ENABLED = true; // Device will provide a web interface via http://[ip]:[port]/

View File

@ -83,7 +83,7 @@ String lastReportStatus = "";
boolean displayOn = true; boolean displayOn = true;
// OctoPrint Client // OctoPrint Client
OctoPrintClient printerClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort); OctoPrintClient printerClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort, OctoAuthUser, OctoAuthPass);
int printerCount = 0; int printerCount = 0;
//declairing prototypes //declairing prototypes
@ -103,11 +103,13 @@ const String CHANGE_FORM = "<form class='w3-container' action='/updateconfig' m
"<label>OctoPrint Host Name (usually octopi)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoPrintHostName' value='%OCTOHOST%' maxlength='60'>" "<label>OctoPrint Host Name (usually octopi)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoPrintHostName' value='%OCTOHOST%' maxlength='60'>"
"<label>OctoPrint Address (do not include http://)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoPrintAddress' value='%OCTOADDRESS%' maxlength='60'>" "<label>OctoPrint Address (do not include http://)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoPrintAddress' value='%OCTOADDRESS%' maxlength='60'>"
"<label>OctoPrint Port</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoPrintPort' value='%OCTOPORT%' maxlength='5' onkeypress='return isNumberKey(event)'>" "<label>OctoPrint Port</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoPrintPort' value='%OCTOPORT%' maxlength='5' onkeypress='return isNumberKey(event)'>"
"<label>OctoPrint User (only needed if you have haproxy or basic auth turned on)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='octoUser' value='%OCTOUSER%' maxlength='30'>"
"<label>OctoPrint Password </label><input class='w3-input w3-border w3-margin-bottom' type='password' name='octoPass' value='%OCTOPASS%'><hr>"
"<input name='isClockEnabled' class='w3-check w3-margin-top' type='checkbox' %IS_CLOCK_CHECKED%> Display Clock when printer is off<p>" "<input name='isClockEnabled' class='w3-check w3-margin-top' type='checkbox' %IS_CLOCK_CHECKED%> Display Clock when printer is off<p>"
"<input name='is24hour' class='w3-check w3-margin-top' type='checkbox' %IS_24HOUR_CHECKED%> Use 24 Hour Clock (military time)<p>" "<input name='is24hour' class='w3-check w3-margin-top' type='checkbox' %IS_24HOUR_CHECKED%> Use 24 Hour Clock (military time)<p>"
"Time Refresh (minutes) <select class='w3-option w3-padding' name='refresh'>%OPTIONS%</select></p>" "Time Refresh (minutes) <select class='w3-option w3-padding' name='refresh'>%OPTIONS%</select></p>"
"Theme Color <select class='w3-option w3-padding' name='theme'>%THEME_OPTIONS%</select></p>" "Theme Color <select class='w3-option w3-padding' name='theme'>%THEME_OPTIONS%</select></p>"
"<label>UTC Time Offset</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='utcoffset' value='%UTCOFFSET%' maxlength='12'>" "<label>UTC Time Offset</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='utcoffset' value='%UTCOFFSET%' maxlength='12'><hr>"
"<label>User ID (for this interface)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='userid' value='%USERID%' maxlength='20'>" "<label>User ID (for this interface)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='userid' value='%USERID%' maxlength='20'>"
"<label>Password </label><input class='w3-input w3-border w3-margin-bottom' type='password' name='stationpassword' value='%STATIONPASSWORD%'>" "<label>Password </label><input class='w3-input w3-border w3-margin-bottom' type='password' name='stationpassword' value='%STATIONPASSWORD%'>"
"<button class='w3-button w3-block w3-grey w3-section w3-padding' type='submit'>Save</button></form>"; "<button class='w3-button w3-block w3-grey w3-section w3-padding' type='submit'>Save</button></form>";
@ -363,6 +365,8 @@ void handleUpdateConfig() {
OctoPrintHostName = server.arg("octoPrintHostName"); OctoPrintHostName = server.arg("octoPrintHostName");
OctoPrintServer = server.arg("octoPrintAddress"); OctoPrintServer = server.arg("octoPrintAddress");
OctoPrintPort = server.arg("octoPrintPort").toInt(); OctoPrintPort = server.arg("octoPrintPort").toInt();
OctoAuthUser = server.arg("octoUser");
OctoAuthPass = server.arg("octoPass");
DISPLAYCLOCK = server.hasArg("isClockEnabled"); DISPLAYCLOCK = server.hasArg("isClockEnabled");
IS_24HOUR = server.hasArg("is24hour"); IS_24HOUR = server.hasArg("is24hour");
minutesBetweenDataRefresh = server.arg("refresh").toInt(); minutesBetweenDataRefresh = server.arg("refresh").toInt();
@ -414,6 +418,8 @@ void handleConfigure() {
form.replace("%OCTOHOST%", OctoPrintHostName); form.replace("%OCTOHOST%", OctoPrintHostName);
form.replace("%OCTOADDRESS%", OctoPrintServer); form.replace("%OCTOADDRESS%", OctoPrintServer);
form.replace("%OCTOPORT%", String(OctoPrintPort)); form.replace("%OCTOPORT%", String(OctoPrintPort));
form.replace("%OCTOUSER%", OctoAuthUser);
form.replace("%OCTOPASS%", OctoAuthPass);
String isClockChecked = ""; String isClockChecked = "";
if (DISPLAYCLOCK) { if (DISPLAYCLOCK) {
isClockChecked = "checked='checked'"; isClockChecked = "checked='checked'";
@ -771,6 +777,8 @@ void writeSettings() {
f.println("octoHost=" + OctoPrintHostName); f.println("octoHost=" + OctoPrintHostName);
f.println("octoServer=" + OctoPrintServer); f.println("octoServer=" + OctoPrintServer);
f.println("octoPort=" + String(OctoPrintPort)); f.println("octoPort=" + String(OctoPrintPort));
f.println("octoUser=" + OctoAuthUser);
f.println("octoPass=" + OctoAuthPass);
f.println("refreshRate=" + String(minutesBetweenDataRefresh)); f.println("refreshRate=" + String(minutesBetweenDataRefresh));
f.println("themeColor=" + themeColor); f.println("themeColor=" + themeColor);
f.println("www_username=" + String(www_username)); f.println("www_username=" + String(www_username));
@ -817,6 +825,16 @@ void readSettings() {
OctoPrintPort = line.substring(line.lastIndexOf("octoPort=") + 9).toInt(); OctoPrintPort = line.substring(line.lastIndexOf("octoPort=") + 9).toInt();
Serial.println("OctoPrintPort=" + String(OctoPrintPort)); Serial.println("OctoPrintPort=" + String(OctoPrintPort));
} }
if (line.indexOf("octoUser=") >= 0) {
OctoAuthUser = line.substring(line.lastIndexOf("octoUser=") + 9);
OctoAuthUser.trim();
Serial.println("OctoAuthUser=" + OctoAuthUser);
}
if (line.indexOf("octoPass=") >= 0) {
OctoAuthPass = line.substring(line.lastIndexOf("octoPass=") + 9);
OctoAuthPass.trim();
Serial.println("OctoAuthPass=" + OctoAuthPass);
}
if (line.indexOf("refreshRate=") >= 0) { if (line.indexOf("refreshRate=") >= 0) {
minutesBetweenDataRefresh = line.substring(line.lastIndexOf("refreshRate=") + 12).toInt(); minutesBetweenDataRefresh = line.substring(line.lastIndexOf("refreshRate=") + 12).toInt();
Serial.println("minutesBetweenDataRefresh=" + String(minutesBetweenDataRefresh)); Serial.println("minutesBetweenDataRefresh=" + String(minutesBetweenDataRefresh));
@ -848,7 +866,7 @@ void readSettings() {
} }
} }
fr.close(); fr.close();
printerClient.updateOctoPrintClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort); printerClient.updateOctoPrintClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort, OctoAuthUser, OctoAuthPass);
timeClient.setUtcOffset(UtcOffset); timeClient.setUtcOffset(UtcOffset);
} }