Create.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <template>
  2. <div>
  3. <!-- Quick form -->
  4. <form @submit.prevent="createAccount" @keydown="form.onKeydown($event)" v-if="showQuickForm">
  5. <div class="container preview has-text-centered">
  6. <div class="columns is-mobile">
  7. <div class="column">
  8. <label class="add-icon-button" v-if="!tempIcon">
  9. <input class="file-input" type="file" accept="image/*" v-on:change="uploadIcon" ref="iconInput">
  10. <font-awesome-icon :icon="['fas', 'image']" size="2x" />
  11. </label>
  12. <button class="delete delete-icon-button is-medium" v-if="tempIcon" @click.prevent="deleteIcon"></button>
  13. <otp-displayer ref="QuickFormOtpDisplayer" v-bind="form.data()" @increment-hotp="incrementHotp">
  14. </otp-displayer>
  15. </div>
  16. </div>
  17. <div class="columns is-mobile" v-if="form.errors.any()">
  18. <div class="column">
  19. <p v-for="field in form.errors.errors" class="help is-danger">
  20. <ul>
  21. <li v-for="(error, index) in field">{{ error }}</li>
  22. </ul>
  23. </p>
  24. </div>
  25. </div>
  26. <div class="columns is-mobile">
  27. <div class="column quickform-footer">
  28. <div class="field is-grouped is-grouped-centered">
  29. <div class="control">
  30. <v-button :isLoading="form.isBusy" >{{ $t('commons.save') }}</v-button>
  31. </div>
  32. <div class="control">
  33. <button type="button" class="button is-text" @click="cancelCreation">{{ $t('commons.cancel') }}</button>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </form>
  40. <!-- Full form -->
  41. <form-wrapper :title="$t('twofaccounts.forms.new_account')" v-if="showAdvancedForm">
  42. <form @submit.prevent="createAccount" @keydown="form.onKeydown($event)">
  43. <!-- qcode fileupload -->
  44. <div class="field">
  45. <div class="file is-black is-small">
  46. <label class="file-label" :title="$t('twofaccounts.forms.use_qrcode.title')">
  47. <input class="file-input" type="file" accept="image/*" v-on:change="uploadQrcode" ref="qrcodeInput">
  48. <span class="file-cta">
  49. <span class="file-icon">
  50. <font-awesome-icon :icon="['fas', 'qrcode']" size="lg" />
  51. </span>
  52. <span class="file-label">{{ $t('twofaccounts.forms.prefill_using_qrcode') }}</span>
  53. </span>
  54. </label>
  55. </div>
  56. </div>
  57. <field-error :form="form" field="qrcode" class="help-for-file" />
  58. <!-- service -->
  59. <form-field :form="form" :isDisabled="form.otp_type === 'steamtotp'" fieldName="service" inputType="text" :label="$t('twofaccounts.service')" :placeholder="$t('twofaccounts.forms.service.placeholder')" autofocus />
  60. <!-- account -->
  61. <form-field :form="form" fieldName="account" inputType="text" :label="$t('twofaccounts.account')" :placeholder="$t('twofaccounts.forms.account.placeholder')" />
  62. <!-- icon upload -->
  63. <label class="label">{{ $t('twofaccounts.icon') }}</label>
  64. <div class="field is-grouped">
  65. <!-- i'm lucky button -->
  66. <div class="control">
  67. <v-button @click="fetchLogo" :color="'is-dark'" :nativeType="'button'" :isDisabled="form.service.length < 3">
  68. <span class="icon is-small">
  69. <font-awesome-icon :icon="['fas', 'globe']" />
  70. </span>
  71. <span>{{ $t('twofaccounts.forms.i_m_lucky') }}</span>
  72. </v-button>
  73. </div>
  74. <!-- upload button -->
  75. <div class="control">
  76. <div class="file is-dark">
  77. <label class="file-label">
  78. <input class="file-input" type="file" accept="image/*" v-on:change="uploadIcon" ref="iconInput">
  79. <span class="file-cta">
  80. <span class="file-icon">
  81. <font-awesome-icon :icon="['fas', 'upload']" />
  82. </span>
  83. <span class="file-label">{{ $t('twofaccounts.forms.choose_image') }}</span>
  84. </span>
  85. </label>
  86. <span class="tag is-black is-large" v-if="tempIcon">
  87. <img class="icon-preview" :src="'/storage/icons/' + tempIcon" >
  88. <button class="delete is-small" @click.prevent="deleteIcon"></button>
  89. </span>
  90. </div>
  91. </div>
  92. </div>
  93. <div class="field">
  94. <field-error :form="form" field="icon" class="help-for-file" />
  95. <p class="help" v-html="$t('twofaccounts.forms.i_m_lucky_legend')"></p>
  96. </div>
  97. <!-- otp type -->
  98. <form-toggle @otp_type="setFormState" class="has-uppercased-button" :form="form" :choices="otp_types" fieldName="otp_type" :label="$t('twofaccounts.forms.otp_type.label')" :help="$t('twofaccounts.forms.otp_type.help')" :hasOffset="true" />
  99. <div v-if="form.otp_type">
  100. <!-- secret -->
  101. <label class="label" v-html="$t('twofaccounts.forms.secret.label')"></label>
  102. <div class="field has-addons">
  103. <p class="control">
  104. <span class="select">
  105. <select @change="form.secret=''" v-model="secretIsBase32Encoded">
  106. <option v-for="format in secretFormats" :value="format.value">{{ format.text }}</option>
  107. </select>
  108. </span>
  109. </p>
  110. <p class="control is-expanded">
  111. <input class="input" type="text" v-model="form.secret">
  112. </p>
  113. </div>
  114. <div class="field">
  115. <field-error :form="form" field="secret" class="help-for-file" />
  116. <p class="help" v-html="$t('twofaccounts.forms.secret.help')"></p>
  117. </div>
  118. <div v-if="form.otp_type !== 'steamtotp'">
  119. <h2 class="title is-4 mt-5 mb-2">{{ $t('commons.options') }}</h2>
  120. <p class="help mb-4">
  121. {{ $t('twofaccounts.forms.options_help') }}
  122. </p>
  123. <!-- digits -->
  124. <form-toggle :form="form" :choices="digitsChoices" fieldName="digits" :label="$t('twofaccounts.forms.digits.label')" :help="$t('twofaccounts.forms.digits.help')" />
  125. <!-- algorithm -->
  126. <form-toggle :form="form" :choices="algorithms" fieldName="algorithm" :label="$t('twofaccounts.forms.algorithm.label')" :help="$t('twofaccounts.forms.algorithm.help')" />
  127. <!-- TOTP period -->
  128. <form-field v-if="form.otp_type === 'totp'" :form="form" fieldName="period" inputType="text" :label="$t('twofaccounts.forms.period.label')" :placeholder="$t('twofaccounts.forms.period.placeholder')" :help="$t('twofaccounts.forms.period.help')" />
  129. <!-- HOTP counter -->
  130. <form-field v-if="form.otp_type === 'hotp'" :form="form" fieldName="counter" inputType="text" :label="$t('twofaccounts.forms.counter.label')" :placeholder="$t('twofaccounts.forms.counter.placeholder')" :help="$t('twofaccounts.forms.counter.help')" />
  131. </div>
  132. </div>
  133. <vue-footer :showButtons="true">
  134. <p class="control">
  135. <v-button :isLoading="form.isBusy" class="is-rounded" >{{ $t('commons.create') }}</v-button>
  136. </p>
  137. <p class="control" v-if="form.otp_type && form.secret">
  138. <button type="button" class="button is-success is-rounded" @click="previewAccount">{{ $t('twofaccounts.forms.test') }}</button>
  139. </p>
  140. <p class="control">
  141. <button type="button" class="button is-text is-rounded" @click="cancelCreation">{{ $t('commons.cancel') }}</button>
  142. </p>
  143. </vue-footer>
  144. </form>
  145. <!-- modal -->
  146. <modal v-model="ShowTwofaccountInModal">
  147. <otp-displayer ref="AdvancedFormOtpDisplayer" v-bind="form.data()" @increment-hotp="incrementHotp">
  148. </otp-displayer>
  149. </modal>
  150. </form-wrapper>
  151. <!-- alternatives -->
  152. <modal v-model="showAlternatives">
  153. <div class="too-bad"></div>
  154. <div class="block">
  155. {{ $t('errors.data_of_qrcode_is_not_valid_URI') }}
  156. </div>
  157. <div class="block has-text-light mb-6" v-html="uri"></div>
  158. <!-- Copy to clipboard -->
  159. <div class="block has-text-link">
  160. <label class="button is-link is-outlined is-rounded" v-clipboard="() => uri" v-clipboard:success="clipboardSuccessHandler">
  161. {{ $t('commons.copy_to_clipboard') }}
  162. </label>
  163. </div>
  164. <!-- Open in browser -->
  165. <div class="block has-text-link" v-if="isUrl(uri)" @click="openInBrowser(uri)">
  166. <label class="button is-link is-outlined is-rounded">
  167. {{ $t('commons.open_in_browser') }}
  168. </label>
  169. </div>
  170. </modal>
  171. </div>
  172. </template>
  173. <script>
  174. /**
  175. * Create form view
  176. *
  177. * route: '/account/create'
  178. *
  179. * Offer the user to define, preview and store an account. The form has 2 designs :
  180. * - The 'Quick Form', a read only design fed with $route.params.decodedUri passed by the Start view.
  181. * - The 'Advanced Form', a fully editable form, that let user define all field and OTP parameters.
  182. * ~ A qrcode can be used to automatically fill the form
  183. * ~ If an 'image' parameter is embeded in the qrcode, the remote image is downloaded and preset in the icon field
  184. *
  185. * Both design use the otpDisplayer component to preview the account with an otp rotation.
  186. *
  187. * input : [optional, for the Quick Form] an URI previously decoded by the Start view
  188. * submit : post account data to php backend to create the account
  189. */
  190. import Modal from '../../components/Modal'
  191. import Form from './../../components/Form'
  192. import OtpDisplayer from '../../components/OtpDisplayer'
  193. import Base32 from "hi-base32"
  194. export default {
  195. data() {
  196. return {
  197. showQuickForm: false,
  198. showAdvancedForm: false,
  199. ShowTwofaccountInModal : false,
  200. showAlternatives : false,
  201. tempIcon: '',
  202. uri: '',
  203. secretIsBase32Encoded: 0,
  204. form: new Form({
  205. service: '',
  206. account: '',
  207. otp_type: '',
  208. icon: '',
  209. secret: '',
  210. algorithm: '',
  211. digits: null,
  212. counter: null,
  213. period: null,
  214. image: '',
  215. qrcode: null,
  216. }),
  217. otp_types: [
  218. { text: 'TOTP', value: 'totp' },
  219. { text: 'HOTP', value: 'hotp' },
  220. { text: 'STEAM', value: 'steamtotp' },
  221. ],
  222. digitsChoices: [
  223. { text: 6, value: 6 },
  224. { text: 7, value: 7 },
  225. { text: 8, value: 8 },
  226. { text: 9, value: 9 },
  227. { text: 10, value: 10 },
  228. ],
  229. secretFormats: [
  230. { text: this.$t('twofaccounts.forms.plain_text'), value: 0 },
  231. { text: 'Base32', value: 1 }
  232. ],
  233. algorithms: [
  234. { text: 'sha1', value: 'sha1' },
  235. { text: 'sha256', value: 'sha256' },
  236. { text: 'sha512', value: 'sha512' },
  237. { text: 'md5', value: 'md5' },
  238. ],
  239. }
  240. },
  241. watch: {
  242. tempIcon: function(val) {
  243. if( this.showQuickForm ) {
  244. this.$refs.QuickFormOtpDisplayer.internal_icon = val
  245. }
  246. },
  247. },
  248. mounted: function () {
  249. if( this.$route.params.decodedUri ) {
  250. this.uri = this.$route.params.decodedUri
  251. // the Start view provided an uri so we parse it and prefill the quick form
  252. this.axios.post('/api/v1/twofaccounts/preview', { uri: this.uri }).then(response => {
  253. this.form.fill(response.data)
  254. this.secretIsBase32Encoded = 1
  255. this.tempIcon = response.data.icon ? response.data.icon : null
  256. this.showQuickForm = true
  257. })
  258. .catch(error => {
  259. if( error.response.status === 422 ) {
  260. if( error.response.data.errors.uri ) {
  261. this.showAlternatives = true
  262. this.showAdvancedForm = true
  263. }
  264. }
  265. });
  266. } else {
  267. this.showAdvancedForm = true
  268. }
  269. this.$on('modalClose', function() {
  270. this.showAlternatives = false;
  271. if( this.showAdvancedForm ) {
  272. this.$refs.AdvancedFormOtpDisplayer.stopLoop()
  273. }
  274. });
  275. },
  276. components: {
  277. Modal,
  278. OtpDisplayer,
  279. },
  280. methods: {
  281. async createAccount() {
  282. // set current temp icon as account icon
  283. this.form.icon = this.tempIcon
  284. // Secret to base32 if necessary
  285. this.form.secret = this.secretIsBase32Encoded ? this.form.secret : Base32.encode(this.form.secret).toString();
  286. await this.form.post('/api/v1/twofaccounts')
  287. if( this.form.errors.any() === false ) {
  288. this.$router.push({name: 'accounts', params: { toRefresh: true }});
  289. }
  290. },
  291. previewAccount() {
  292. this.$refs.AdvancedFormOtpDisplayer.show()
  293. },
  294. cancelCreation: function() {
  295. if( this.form.service ) {
  296. if( confirm(this.$t('twofaccounts.confirm.cancel')) === false ) {
  297. return
  298. }
  299. }
  300. // clean possible uploaded temp icon
  301. this.deleteIcon()
  302. this.$router.push({name: 'accounts'});
  303. },
  304. async uploadQrcode(event) {
  305. let imgdata = new FormData();
  306. imgdata.append('qrcode', this.$refs.qrcodeInput.files[0]);
  307. imgdata.append('inputFormat', 'fileUpload');
  308. // First we get the uri encoded in the qrcode
  309. const { data } = await this.form.upload('/api/v1/qrcode/decode', imgdata)
  310. this.uri = data.data
  311. // Then the otp described by the uri
  312. this.axios.post('/api/v1/twofaccounts/preview', { uri: data.data }).then(response => {
  313. this.form.fill(response.data)
  314. this.secretIsBase32Encoded = 1
  315. this.tempIcon = response.data.icon ? response.data.icon : null
  316. })
  317. .catch(error => {
  318. if( error.response.status === 422 ) {
  319. if( error.response.data.errors.uri ) {
  320. this.showAlternatives = true
  321. }
  322. }
  323. });
  324. },
  325. async uploadIcon(event) {
  326. // clean possible already uploaded temp icon
  327. this.deleteIcon()
  328. let imgdata = new FormData();
  329. imgdata.append('icon', this.$refs.iconInput.files[0]);
  330. const { data } = await this.form.upload('/api/v1/icons', imgdata)
  331. this.tempIcon = data.filename;
  332. },
  333. fetchLogo() {
  334. this.axios.post('/api/v1/icons/default', {service: this.form.service}, {returnError: true}).then(response => {
  335. if (response.status === 201) {
  336. // clean possible already uploaded temp icon
  337. this.deleteIcon()
  338. this.tempIcon = response.data.filename;
  339. }
  340. else this.$notify({type: 'is-warning', text: this.$t('errors.no_logo_found_for_x', {service: this.form.service}) })
  341. })
  342. .catch(error => {
  343. this.$notify({type: 'is-warning', text: this.$t('errors.no_logo_found_for_x', {service: this.form.service}) })
  344. });
  345. },
  346. deleteIcon(event) {
  347. if(this.tempIcon) {
  348. this.axios.delete('/api/v1/icons/' + this.tempIcon)
  349. this.tempIcon = ''
  350. }
  351. },
  352. incrementHotp(payload) {
  353. // The quick form or the preview feature has incremented the HOTP counter so we get the new value from
  354. // the component.
  355. // This could desynchronized the HOTP verification server and our local counter if the user never verified the HOTP but this
  356. // is acceptable (and HOTP counter can be edited by the way)
  357. this.form.counter = payload.nextHotpCounter
  358. },
  359. clipboardSuccessHandler ({ value, event }) {
  360. if(this.$root.appSettings.kickUserAfter == -1) {
  361. this.appLogout()
  362. }
  363. this.$notify({ type: 'is-success', text: this.$t('commons.copied_to_clipboard') })
  364. },
  365. clipboardErrorHandler ({ value, event }) {
  366. console.log('error', value)
  367. },
  368. setFormState (event) {
  369. this.form.otp_type = event
  370. this.form.service = event === 'steamtotp' ? 'Steam' : ''
  371. this.secretIsBase32Encoded = event === 'steamtotp' ? 1 : this.secretIsBase32Encoded
  372. },
  373. },
  374. }
  375. </script>