2018-04-03 14:16:57 +00:00
|
|
|
import Ember from 'ember';
|
2018-07-18 14:13:39 +00:00
|
|
|
import base64js from 'base64-js';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
const { Component, inject, computed, get } = Ember;
|
|
|
|
const { camelize } = Ember.String;
|
|
|
|
|
|
|
|
const DEFAULTS = {
|
|
|
|
key: null,
|
|
|
|
loading: false,
|
|
|
|
errors: [],
|
|
|
|
threshold: null,
|
|
|
|
progress: null,
|
2018-06-26 21:35:47 +00:00
|
|
|
pgp_key: null,
|
|
|
|
haveSavedPGPKey: false,
|
2018-04-03 14:16:57 +00:00
|
|
|
started: false,
|
|
|
|
generateWithPGP: false,
|
|
|
|
pgpKeyFile: { value: '' },
|
|
|
|
nonce: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Component.extend(DEFAULTS, {
|
|
|
|
tagName: '',
|
|
|
|
store: inject.service(),
|
|
|
|
formText: null,
|
|
|
|
fetchOnInit: false,
|
|
|
|
buttonText: 'Submit',
|
|
|
|
thresholdPath: 'required',
|
|
|
|
generateAction: false,
|
|
|
|
encoded_token: null,
|
|
|
|
|
|
|
|
init() {
|
|
|
|
if (this.get('fetchOnInit')) {
|
|
|
|
this.attemptProgress();
|
|
|
|
}
|
|
|
|
return this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
2018-06-26 21:35:47 +00:00
|
|
|
onShamirSuccess() {},
|
2018-04-03 14:16:57 +00:00
|
|
|
// can be overridden w/an attr
|
|
|
|
isComplete(data) {
|
|
|
|
return data.complete === true;
|
|
|
|
},
|
|
|
|
|
|
|
|
stopLoading() {
|
|
|
|
this.setProperties({
|
|
|
|
loading: false,
|
|
|
|
errors: [],
|
|
|
|
key: null,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.setProperties(DEFAULTS);
|
|
|
|
},
|
|
|
|
|
|
|
|
hasProgress: computed.gt('progress', 0),
|
|
|
|
|
|
|
|
actionSuccess(resp) {
|
|
|
|
const { isComplete, onShamirSuccess, thresholdPath } = this.getProperties(
|
|
|
|
'isComplete',
|
|
|
|
'onShamirSuccess',
|
|
|
|
'thresholdPath'
|
|
|
|
);
|
|
|
|
this.stopLoading();
|
|
|
|
this.set('threshold', get(resp, thresholdPath));
|
|
|
|
this.setProperties(resp);
|
|
|
|
if (isComplete(resp)) {
|
|
|
|
this.reset();
|
|
|
|
onShamirSuccess(resp);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actionError(e) {
|
|
|
|
this.stopLoading();
|
|
|
|
if (e.httpStatus === 400) {
|
|
|
|
this.set('errors', e.errors);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-26 21:35:47 +00:00
|
|
|
generateStep: computed('generateWithPGP', 'haveSavedPGPKey', 'otp', 'pgp_key', function() {
|
|
|
|
let { generateWithPGP, otp, pgp_key, haveSavedPGPKey } = this.getProperties(
|
|
|
|
'generateWithPGP',
|
|
|
|
'otp',
|
|
|
|
'pgp_key',
|
|
|
|
'haveSavedPGPKey'
|
|
|
|
);
|
|
|
|
if (!generateWithPGP && !pgp_key && !otp) {
|
|
|
|
return 'chooseMethod';
|
|
|
|
}
|
|
|
|
if (otp) {
|
|
|
|
return 'beginGenerationWithOTP';
|
|
|
|
}
|
|
|
|
if (generateWithPGP) {
|
|
|
|
if (pgp_key && haveSavedPGPKey) {
|
|
|
|
return 'beginGenerationWithPGP';
|
|
|
|
} else {
|
|
|
|
return 'providePGPKey';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
extractData(data) {
|
|
|
|
const isGenerate = this.get('generateAction');
|
|
|
|
const hasStarted = this.get('started');
|
|
|
|
const usePGP = this.get('generateWithPGP');
|
|
|
|
const nonce = this.get('nonce');
|
|
|
|
|
|
|
|
if (!isGenerate || hasStarted) {
|
|
|
|
if (nonce) {
|
|
|
|
data.nonce = nonce;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usePGP) {
|
|
|
|
return {
|
|
|
|
pgp_key: data.pgp_key,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
otp: data.otp,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
attemptProgress(data) {
|
|
|
|
const checkStatus = data ? false : true;
|
|
|
|
let action = this.get('action');
|
|
|
|
action = action && camelize(action);
|
|
|
|
this.set('loading', true);
|
|
|
|
const adapter = this.get('store').adapterFor('cluster');
|
|
|
|
const method = adapter[action];
|
|
|
|
method
|
|
|
|
.call(adapter, data, { checkStatus })
|
|
|
|
.then(resp => this.actionSuccess(resp), (...args) => this.actionError(...args));
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2018-06-26 21:35:47 +00:00
|
|
|
reset() {
|
|
|
|
this.reset();
|
|
|
|
this.set('encoded_token', null);
|
|
|
|
this.set('otp', null);
|
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
onSubmit(data) {
|
|
|
|
if (!data.key) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.attemptProgress(this.extractData(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
startGenerate(data) {
|
|
|
|
this.attemptProgress(this.extractData(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
generateOTP() {
|
|
|
|
const bytes = new window.Uint8Array(16);
|
|
|
|
window.crypto.getRandomValues(bytes);
|
|
|
|
this.set('otp', base64js.fromByteArray(bytes));
|
|
|
|
},
|
|
|
|
|
|
|
|
setKey(_, keyFile) {
|
|
|
|
this.set('pgp_key', keyFile.value);
|
|
|
|
this.set('pgpKeyFile', keyFile);
|
|
|
|
},
|
|
|
|
|
2018-06-26 21:35:47 +00:00
|
|
|
savePGPKey() {
|
|
|
|
if (this.get('pgp_key')) {
|
|
|
|
this.set('haveSavedPGPKey', true);
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|