Skip to content

Commit

Permalink
Make basic authentication required and add option to use it to log in…
Browse files Browse the repository at this point in the history
… to OctoPrint
  • Loading branch information
fieldOfView committed Jun 22, 2020
1 parent 7d1353b commit d26373e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 37 deletions.
24 changes: 15 additions & 9 deletions octoprint_ngrok/__init__.py
Expand Up @@ -34,7 +34,8 @@ def get_settings_defaults(self):
subdomain="",
auth_name="",
auth_pass="",
auto_connect=True
auto_connect=True,
trust_basic_authentication=settings().getBoolean(["accessControl", "trustBasicAuthentication"]),
)

def get_settings_restricted_paths(self):
Expand All @@ -47,6 +48,9 @@ def get_settings_restricted_paths(self):
def on_settings_save(self, data):
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)

if "trust_basic_authentication" in data:
settings().setBoolean(["accessControl", "trustBasicAuthentication"], data["trust_basic_authentication"])

if "token" in data or "region" in data:
self._restart_ngrok = True

Expand Down Expand Up @@ -164,13 +168,19 @@ def _ngrok_connect(self):
self._ngrok_disconnect()

if not self._settings.get(["token"]):
self._logger.warning("Ngrok is not fully configured")
self._logger.warning("Ngrok auth token is not configured")
self._plugin_manager.send_plugin_message(self._identifier, dict(error="The auth token is not configured. An auth token is required to create a secure tunnel."))

self._restart_ngrok = True

return

if not (self._settings.get(["auth_name"]) and self._settings.get(["auth_pass"])):
self._logger.warning("Basic Auth is not configured")
self._plugin_manager.send_plugin_message(self._identifier, dict(error="The username and password are not configured. Authentication is required to create a secure tunnel."))

return

pyngrok_config = PyngrokConfig()
pyngrok_config.log_event_callback = self.on_ngrok_log_event

Expand All @@ -188,16 +198,12 @@ def _ngrok_connect(self):
self._logger.info("Opening ngrok tunnel...")
options = dict(
bind_tls=True,
inspect=False
)

if self._settings.get(["auth_name"]) and self._settings.get(["auth_pass"]):
auth_string = "%s:%s" % (
inspect=False,
auth="%s:%s" % (
self._settings.get(["auth_name"]),
self._settings.get(["auth_pass"])
)
options["auth"] = auth_string

)

if self._settings.get(["subdomain"]):
options["subdomain"] = self._settings.get(["subdomain"])
Expand Down
23 changes: 22 additions & 1 deletion octoprint_ngrok/static/js/ngrok.js
Expand Up @@ -6,6 +6,21 @@ $(function() {
self.settings = parameters[0];
self.tunnelName = ko.observable('');

self.tokenIsEmpty = ko.observable(false);
self.nameIsEmpty = ko.observable(false);
self.passIsEmpty = ko.observable(false);

$("#settings_plugin_ngrok_token").change(function(){
self.tokenIsEmpty(this.value == "");
});
$("#settings_plugin_ngrok_auth_name").change(function(){
self.nameIsEmpty(this.value == "");
});
$("#settings_plugin_ngrok_auth_pass").change(function(){
self.passIsEmpty(this.value == "");
});


self.closeTunnel = function() {
OctoPrint.simpleApiCommand("ngrok", "close", {});
}
Expand All @@ -16,7 +31,7 @@ $(function() {
}

self.showTunnelSettings = function() {
self.settings.show('#settings_plugin_ngrok');
self.settings.show("#settings_plugin_ngrok");
}

self.requestData = function() {
Expand All @@ -26,7 +41,13 @@ $(function() {
});
};


self.onAfterBinding = self.onServerReconnect = function() {
console.log(self.settings)
self.tokenIsEmpty(self.settings.settings.plugins.ngrok.token() == "");
self.nameIsEmpty(self.settings.settings.plugins.ngrok.auth_name() == "");
self.passIsEmpty(self.settings.settings.plugins.ngrok.auth_pass() == "");

self.requestData();
};

Expand Down
60 changes: 33 additions & 27 deletions octoprint_ngrok/templates/ngrok_settings.jinja2
Expand Up @@ -12,7 +12,7 @@
<a class="btn" title="{{ _('Visit tunnel') }}" target="_blank" data-bind="
attr: { href: (tunnelName() != '') ? 'https://' + tunnelName() : '' }
">
<i class="fa fa-link"></i>
<i class="fa fa-link"></i>
</a>
<button class="btn btn-danger" type="button" data-bind="click: closeTunnel">{{ _('Close') }}</button>
</span>
Expand All @@ -27,7 +27,7 @@
</div>

<h3>{{ _('Ngrok Settings') }}</h3>
<div class="control-group">
<div class="control-group" data-bind="css:{error: tokenIsEmpty()}">
<label class="control-label" for="settings_plugin_ngrok_token">{{ _('Auth Token') }}</label>
<div class="controls">
<span class="input-append">
Expand All @@ -38,6 +38,34 @@
ngrok dashboard</a>.')|format(dashboard_url="https://dashboard.ngrok.com/auth/your-authtoken") }}</span>
</div>
</div>

<div class="control-group" data-bind="css:{error: nameIsEmpty()}">
<label class="control-label" for="settings_plugin_ngrok_auth_name">{{ _('Username') }}</label>
<div class="controls">
<span class="input-append">
<input type="text" class="input" id="settings_plugin_ngrok_auth_name" required="required"
data-bind="value: settings.settings.plugins.ngrok.auth_name" />
</span>
</div>
</div>
<div class="control-group" data-bind="css:{error: passIsEmpty()}">
<label class="control-label" for="settings_plugin_ngrok_auth_pass">{{ _('Password') }}</label>
<div class="controls">
<span class="input-append">
<input type="password" class="input" id="settings_plugin_ngrok_auth_pass" required="required"
data-bind="value: settings.settings.plugins.ngrok.auth_pass" />
</span>
</div>
</div>

<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.settings.plugins.ngrok.trust_basic_authentication"> {{ _('Use these credentials to log in to OctoPrint') }}
</label>
</div>
</div>

<div class="control-group">
<label class="control-label" for="settings_plugin_ngrok_region">{{ _('Region') }}</label>
<div class="controls">
Expand All @@ -63,34 +91,12 @@
</div>
</div>

<div class="help-block">{{ _('You can use Basic Authentication to secure the tunnel on top of the OctoPrint access control.
It is recommended that you choose a strong password different from your OctoPrint password.') }}</div>
<div class="control-group">
<label class="control-label" for="settings_plugin_ngrok_auth_name">{{ _('Username') }}</label>
<div class="controls">
<span class="input-append">
<input type="text" class="input" id="settings_plugin_ngrok_auth_name" required="required"
data-bind="value: settings.settings.plugins.ngrok.auth_name" />
</span>
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.settings.plugins.ngrok.auto_connect"> {{ _('Create a tunnel when OctoPrint starts') }}
</label>
</div>
</div>
<div class="control-group">
<label class="control-label" for="settings_plugin_ngrok_auth_pass">{{ _('Password') }}</label>
<div class="controls">
<span class="input-append">
<input type="password" class="input" id="settings_plugin_ngrok_auth_pass" required="required"
data-bind="value: settings.settings.plugins.ngrok.auth_pass" />
</span>
</div>
</div>

<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.settings.plugins.ngrok.auto_connect"> {{ _('Create a tunnel when OctoPrint starts') }}
</label>
</div>

</div>
</fieldset>
</form>

0 comments on commit d26373e

Please sign in to comment.