Challenge modal

This commit is contained in:
Mo Bitar
2020-03-20 16:21:15 -05:00
parent 148e840757
commit a0a2a3fc30
7 changed files with 542 additions and 514 deletions

View File

@@ -1,4 +1,3 @@
import { Challenges, ChallengeResponse } from 'snjs';
import { getPlatformString } from '@/utils';
import template from '%/root.pug';
import { AppStateEvents } from '@/state';
@@ -25,6 +24,7 @@ class RootCtrl extends PureCtrl {
application,
appState,
desktopManager,
godService,
lockManager,
preferencesManager /** Unused below, required to load globally */,
themeManager,
@@ -35,6 +35,7 @@ class RootCtrl extends PureCtrl {
this.$rootScope = $rootScope;
this.$compile = $compile;
this.desktopManager = desktopManager;
this.godService = godService;
this.lockManager = lockManager;
this.statusManager = statusManager;
this.themeManager = themeManager;
@@ -64,36 +65,21 @@ class RootCtrl extends PureCtrl {
this.handleAutoSignInFromParams();
}
async watchLockscreenValue() {
return new Promise((resolve) => {
const onLockscreenValue = (value) => {
const response = new ChallengeResponse(Challenges.LocalPasscode, value);
resolve([response]);
};
this.setState({ onLockscreenValue });
});
}
// async watchLockscreenValue() {
// return new Promise((resolve) => {
// const onLockscreenValue = (value) => {
// const response = new ChallengeResponse(ChallengeType.LocalPasscode, value);
// resolve([response]);
// };
// this.setState({ onLockscreenValue });
// });
// }
async loadApplication() {
await this.application.prepareForLaunch({
callbacks: {
requiresChallengeResponses: async (challenges) => {
if (challenges.includes(Challenges.LocalPasscode)) {
this.setState({ needsUnlock: true });
}
return this.watchLockscreenValue();
},
handleFailedChallengeResponses: (responses) => {
for (const response of responses) {
if (response.challenge === Challenges.LocalPasscode) {
this.application.alertService.alert({
text: "Invalid passcode. Please try again.",
onClose: () => {
this.lockScreenPuppet.focusInput();
}
});
}
}
receiveChallenge: async (challenge, orchestrator) => {
this.godService.promptForChallenge(challenge, orchestrator);
}
}
});

View File

@@ -1,5 +1,5 @@
import template from '%/directives/challenge-modal.pug';
import { Challenges, ChallengeResponse } from 'snjs';
import { ChallengeType, ChallengeValue, removeFromArray } from 'snjs';
import { PureCtrl } from '@Controllers';
class ChallengeModalCtrl extends PureCtrl {
@@ -13,18 +13,49 @@ class ChallengeModalCtrl extends PureCtrl {
) {
super($scope, $timeout, application, appState);
this.$element = $element;
this.processingTypes = [];
}
$onInit() {
super.$onInit();
this.values = {};
const values = {};
const types = this.challenge.types;
for (const type of types) {
values[type] = {
value: '',
invalid: false
};
}
this.setState({
challenges: this.challenges
types: types,
values: values,
processing: false
});
this.orchestrator.setCallbacks({
onComplete: () => {
this.dismiss();
},
onValidValue: (value) => {
this.state.values[value.type].invalid = false;
removeFromArray(this.processingTypes, value.type);
this.reloadProcessingStatus();
},
onInvalidValue: (value) => {
this.state.values[value.type].invalid = true;
removeFromArray(this.processingTypes, value.type);
this.reloadProcessingStatus();
}
});
}
reloadProcessingStatus() {
this.setState({
processing: this.processingTypes.length > 0
});
}
promptForChallenge(challenge) {
if(challenge === Challenges.LocalPasscode) {
if (challenge === ChallengeType.LocalPasscode) {
return 'Enter your application passcode';
} else {
return 'Enter your account password';
@@ -32,28 +63,26 @@ class ChallengeModalCtrl extends PureCtrl {
}
cancel() {
if (!this.cancelable) {
return;
}
this.dismiss();
this.onCancel && this.onCancel();
}
isChallengeInFailureState(challenge) {
if (!this.failedChallenges) {
return false;
}
return this.failedChallenges.find((candidate) => {
return candidate === challenge;
}) != null;
onTextValueChange(challenge) {
const values = this.state.values;
values[challenge].invalid = false;
this.setState({ values });
}
validate() {
const failed = [];
for (const cred of this.state.challenges) {
const value = this.values[cred];
for (const type of this.state.types) {
const value = this.state.values[type];
if (!value || value.length === 0) {
failed.push(cred);
this.state.values[type].invalid = true;
}
}
this.failedChallenges = failed;
return failed.length === 0;
}
@@ -61,13 +90,19 @@ class ChallengeModalCtrl extends PureCtrl {
if (!this.validate()) {
return;
}
const responses = Object.keys(this.values).map((key) => {
const challenge = Number(key);
const value = this.values[key];
return new ChallengeResponse(challenge, value);
});
this.onSubmit(responses);
this.dismiss();
this.setState({ processing: true });
const values = [];
for (const key of Object.keys(this.state.values)) {
const type = Number(key);
if(this.state.values[key].valid) {
continue;
}
const rawValue = this.state.values[key].value;
const value = new ChallengeValue(type, rawValue);
values.push(value);
}
this.processingTypes = values.map((v) => v.type);
this.orchestrator.submitValues(values);
}
dismiss() {
@@ -84,8 +119,8 @@ export class ChallengeModal {
this.bindToController = true;
this.scope = {
onSubmit: '=',
onCancel: '=',
challenges: '='
challenge: '=',
orchestrator: '='
};
}
}

View File

@@ -1,6 +1,4 @@
import angular from 'angular';
import { challengeToString } from 'snjs';
import { humanReadableList } from '@/utils';
export class GodService {
/* @ngInject */
@@ -25,42 +23,24 @@ export class GodService {
angular.element(document.body).append(el);
}
async promptForChallenges(challenges) {
return new Promise((resolve) => {
const scope = this.$rootScope.$new(true);
scope.challenges = challenges.slice();
scope.onSubmit = (responses) => {
resolve(responses);
};
const el = this.$compile(
"<challenge-modal class='sk-modal' challenges='challenges' on-submit='onSubmit'></challenge-modal>"
)(scope);
angular.element(document.body).append(el);
});
promptForChallenge(challenge, orchestrator) {
const scope = this.$rootScope.$new(true);
scope.challenge = challenge;
scope.orchestrator = orchestrator;
const el = this.$compile(
"<challenge-modal " +
"class='sk-modal' challenge='challenge' orchestrator='orchestrator'>" +
"</challenge-modal>"
)(scope);
angular.element(document.body).append(el);
}
async performProtocolUpgrade() {
const errors = await this.application.upgradeProtocolVersion({
requiresChallengeResponses: async (challenges) => {
return this.promptForChallenges(challenges);
},
handleFailedChallengeResponses: async (responses) => {
const names = responses.map((r) => challengeToString(r.challenge));
const formatted = humanReadableList(names);
return new Promise((resolve) => {
this.application.alertService.alert({
text: `Invalid authentication value for ${formatted}. Please try again.`,
onClose: () => {
resolve();
}
});
});
}
});
const errors = await this.application.upgradeProtocolVersion();
if (errors.length === 0) {
this.application.alertService.alert({
text: "Success! Your encryption version has been upgraded." +
" You'll be asked to enter your credentials again on other devices you're signed into."
" You'll be asked to enter your credentials again on other devices you're signed into."
});
} else {
this.application.alertService.alert({