Skip to content

Commit

Permalink
fix: require reauth for any enrollment changes
Browse files Browse the repository at this point in the history
Enrollment, activation and deactivation are now
protected by a reauth requirement.

Thanks @jacopotediosi for the heads-up!
  • Loading branch information
foosel committed Oct 29, 2024
1 parent 329185a commit 72c0c00
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
2 changes: 2 additions & 0 deletions octoprint_mfa_totp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from flask_login import current_user
from octoprint.plugin.types import WrongMfaCredentials
from octoprint.schema import BaseModel
from octoprint.server.util.flask import ensure_credentials_checked_recently

CLEANUP_CUTOFF = 60 * 30 # 30 minutes
VALID_WINDOW = 1 # delay of one tick is ok
Expand Down Expand Up @@ -149,6 +150,7 @@ def on_api_command(self, command, data):
user = current_user
if not user or not user.is_authenticated or not user.is_active:
return abort(403)
ensure_credentials_checked_recently()

userid = user.get_id()

Expand Down
78 changes: 44 additions & 34 deletions octoprint_mfa_totp/static/js/mfa_totp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ $(() => {
function MfaTotpViewModel(parameters) {
var self = this;

self.loginState = parameters[0];

self.active = ko.observable(false);

self.enrollmentKey = ko.observable();
Expand Down Expand Up @@ -37,55 +39,63 @@ $(() => {

self.enroll = () => {
self.verificationToken("");
OctoPrint.plugins.mfa_totp.enroll().done((response) => {
self.enrollmentKey(response.key);
self.enrollmentUri(response.uri);
self.enrollmentDialog.modal("show");
$("#mfa_totp_enrollment_token").focus();
self.loginState.reauthenticateIfNecessary(() => {
OctoPrint.plugins.mfa_totp.enroll().done((response) => {
self.enrollmentKey(response.key);
self.enrollmentUri(response.uri);
self.enrollmentDialog.modal("show");
$("#mfa_totp_enrollment_token").focus();
});
});
};

self.finishEnrollment = () => {
const token = self.verificationToken();
self.verificationToken("");
OctoPrint.plugins.mfa_totp
.activate(token)
.done(() => {
self.verificationError(false);
self.mfaError("");
self.enrollmentDialog.modal("hide");
self.requestData();
})
.fail(() => {
self.verificationError(true);
});
self.loginState.reauthenticateIfNecessary(() => {
OctoPrint.plugins.mfa_totp
.activate(token)
.done(() => {
self.verificationError(false);
self.mfaError("");
self.enrollmentDialog.modal("hide");
self.requestData();
})
.fail(() => {
self.verificationError(true);
});
});
};

self.deactivate = () => {
self.verificationToken("");
self.verificationDialog.modal("show");
$("#mfa_totp_verification_token").focus();
self.loginState.reauthenticateIfNecessary(() => {
self.verificationDialog.modal("show");
$("#mfa_totp_verification_token").focus();
});
};

self.finishDeactivation = () => {
const token = self.verificationToken();
self.verificationToken("");
self.mfaError("");
OctoPrint.plugins.mfa_totp
.deactivate(token)
.done(() => {
self.verificationError(false);
self.mfaError("");
self.verificationDialog.modal("hide");
self.requestData();
})
.fail((xhr) => {
const response = xhrErrorJson(xhr);
if (response && response.mfa_error) {
self.mfaError(response.mfa_error);
}
self.verificationError(true);
});
self.loginState.reauthenticateIfNecessary(() => {
OctoPrint.plugins.mfa_totp
.deactivate(token)
.done(() => {
self.verificationError(false);
self.mfaError("");
self.verificationDialog.modal("hide");
self.requestData();
})
.fail((xhr) => {
const response = xhrErrorJson(xhr);
if (response && response.mfa_error) {
self.mfaError(response.mfa_error);
}
self.verificationError(true);
});
});
};

self.onUserSettingsShown = self.onUserLoggedIn = () => {
Expand All @@ -104,7 +114,7 @@ $(() => {

OCTOPRINT_VIEWMODELS.push({
construct: MfaTotpViewModel,
dependencies: [],
dependencies: ["loginStateViewModel"],
elements: [
"#usersettings_mfa_plugin_mfa_totp",
"#plugin_mfa_totp_enroll",
Expand Down

0 comments on commit 72c0c00

Please sign in to comment.