Import.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <template>
  2. <div>
  3. <div class="columns is-centered">
  4. <div class="form-column column is-two-thirds-tablet is-half-desktop is-one-third-widescreen is-one-third-fullhd">
  5. <h1 class="title has-text-grey-dark">
  6. {{ $t('twofaccounts.import.import') }}
  7. </h1>
  8. <div>
  9. <div v-if="exportedAccounts.length == 0">
  10. <div class="block is-size-7-mobile" v-html="$t('twofaccounts.import.import_legend')"></div>
  11. <!-- scan button that launch camera stream -->
  12. <div class="block">
  13. <button tabindex="0" class="button is-link is-rounded" @click="capture()">
  14. {{ $t('twofaccounts.forms.scan_qrcode') }}
  15. </button>
  16. </div>
  17. <!-- upload a qr code (with basic file field and backend decoding) -->
  18. <div class="block">
  19. <label role="button" tabindex="0" class="button is-link is-rounded is-outlined" ref="qrcodeInputLabel" @keyup.enter="$refs.qrcodeInputLabel.click()">
  20. <input aria-hidden="true" tabindex="-1" class="file-input" type="file" accept="image/*" v-on:change="submitQrCode" ref="qrcodeInput">
  21. {{ $t('twofaccounts.forms.upload_qrcode') }}
  22. </label>
  23. <field-error :form="form" field="qrcode" />
  24. <p class="help">{{ $t('twofaccounts.import.supported_formats_for_qrcode_upload') }}</p>
  25. </div>
  26. <!-- upload a file -->
  27. <div class="block">
  28. <label role="button" tabindex="0" class="button is-link is-rounded is-outlined" ref="fileInputLabel" @keyup.enter="$refs.fileInputLabel.click()">
  29. <input aria-hidden="true" tabindex="-1" class="file-input" type="file" accept="text/plain,application/json,text/csv" v-on:change="submitFile" ref="fileInput">
  30. {{ $t('twofaccounts.import.upload_a_file') }}
  31. </label>
  32. <field-error :form="uploadForm" field="file" />
  33. <p class="help">{{ $t('twofaccounts.import.supported_formats_for_file_upload') }}</p>
  34. </div>
  35. <!-- Supported migration resources -->
  36. <h5 class="title is-5 mb-3">{{ $t('twofaccounts.import.supported_migration_formats') }}</h5>
  37. <div class="field is-grouped is-grouped-multiline pt-0">
  38. <div class="control">
  39. <div class="tags has-addons">
  40. <span class="tag is-dark">Google Auth</span>
  41. <span class="tag is-black">{{ $t('twofaccounts.import.qr_code') }}</span>
  42. </div>
  43. </div>
  44. <div class="control">
  45. <div class="tags has-addons">
  46. <span class="tag is-dark">Aegis Auth</span>
  47. <span class="tag is-black">JSON</span>
  48. </div>
  49. </div>
  50. <div class="control">
  51. <div class="tags has-addons">
  52. <span class="tag is-dark">Aegis Auth</span>
  53. <span class="tag is-black">{{ $t('twofaccounts.import.plain_text') }}</span>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. <div v-else>
  59. <div v-for="(account, index) in exportedAccounts" :key="account.name" class="group-item has-text-light is-size-5 is-size-6-mobile">
  60. <div class="is-flex is-justify-content-space-between">
  61. <!-- Account name -->
  62. <div v-if="account.id > -2 && account.imported !== 0" class="is-flex-grow-1 has-ellipsis is-clickable" @click="previewAccount(index)" :title="$t('twofaccounts.import.generate_a_test_password')">
  63. <img v-if="account.icon && $root.appSettings.showAccountsIcons" class="import-icon" :src="'/storage/icons/' + account.icon" :alt="$t('twofaccounts.icon_for_account_x_at_service_y', {account: account.account, service: account.service})">
  64. {{ account.account }}
  65. </div>
  66. <div v-else class="is-flex-grow-1 has-ellipsis">{{ account.account }}</div>
  67. <!-- buttons -->
  68. <div v-if="account.imported === -1" class="tags is-flex-wrap-nowrap">
  69. <!-- discard button -->
  70. <button class="button tag is-dark has-text-grey-light" @click="discardAccount(index)" :title="$t('twofaccounts.import.discard_this_account')">
  71. <font-awesome-icon :icon="['fas', 'trash']" />
  72. </button>
  73. <!-- import button -->
  74. <button v-if="account.id > -2" class="button tag is-link" @click="createAccount(index)" :title="$t('twofaccounts.import.import_this_account')">
  75. {{ $t('twofaccounts.import.to_import') }}
  76. </button>
  77. </div>
  78. <!-- result label -->
  79. <div v-else class="has-nowrap">
  80. <span v-if="account.imported === 1" class="has-text-success">
  81. {{ $t('twofaccounts.import.imported') }} <font-awesome-icon :icon="['fas', 'check']" />
  82. </span>
  83. <span v-else class="has-text-danger">
  84. {{ $t('twofaccounts.import.failure') }} <font-awesome-icon :icon="['fas', 'times']" />
  85. </span>
  86. </div>
  87. </div>
  88. <div class="is-size-6 is-size-7-mobile">
  89. <!-- service name -->
  90. <div class="is-family-primary has-text-grey">{{ $t('twofaccounts.import.issuer') }}: {{ account.service }}</div>
  91. <!-- reasons to invalid G-Auth data -->
  92. <div v-if="account.id === -2" class="has-text-danger">
  93. <font-awesome-icon class="mr-1" :icon="['fas', 'times-circle']" />{{ account.secret }}
  94. </div>
  95. <!-- possible duplicates -->
  96. <div v-if="account.id === -1 && account.imported !== 1 && !account.errors" class="has-text-warning">
  97. <font-awesome-icon class="mr-1" :icon="['fas', 'exclamation-circle']" />{{ $t('twofaccounts.import.possible_duplicate') }}
  98. </div>
  99. <!-- errors during account creation -->
  100. <ul v-if="account.errors">
  101. <li v-for="(error) in account.errors" :key="error" class="has-text-danger">{{ error }}</li>
  102. </ul>
  103. </div>
  104. </div>
  105. <!-- discard links -->
  106. <div v-if="importableCount > 0" class="mt-2 is-size-7 is-pulled-right">
  107. <button v-if="duplicateCount" @click="discardDuplicates()" class="has-text-grey button is-small is-ghost">{{ $t('twofaccounts.import.discard_duplicates') }} ({{duplicateCount}})</button>
  108. <button @click="discardAccounts()" class="has-text-grey button is-small is-ghost">{{ $t('twofaccounts.import.discard_all') }}</button>
  109. </div>
  110. </div>
  111. <div v-if="isFetching && exportedAccounts.length === 0" class="has-text-centered">
  112. <span class="is-size-4">
  113. <font-awesome-icon :icon="['fas', 'spinner']" spin />
  114. </span>
  115. </div>
  116. </div>
  117. <!-- footer -->
  118. <vue-footer :showButtons="true">
  119. <!-- Import all button -->
  120. <p class="control" v-if="importableCount > 0">
  121. <button class="button is-link is-rounded is-focus" @click="createAccounts">
  122. <span>{{ $t('twofaccounts.import.import_all') }} ({{importableCount}})</span>
  123. <!-- <span class="icon is-small">
  124. <font-awesome-icon :icon="['fas', 'qrcode']" />
  125. </span> -->
  126. </button>
  127. </p>
  128. <!-- close button -->
  129. <p class="control">
  130. <router-link :to="{ name: 'accounts', params: { toRefresh: true } }" class="button is-dark is-rounded" v-html="importableCount > 0 ? $t('commons.cancel') : $t('commons.close')"></router-link>
  131. </p>
  132. </vue-footer>
  133. </div>
  134. </div>
  135. <!-- modal -->
  136. <modal v-model="ShowTwofaccountInModal">
  137. <otp-displayer ref="AdvancedFormOtpDisplayer" v-bind="form.data()">
  138. </otp-displayer>
  139. </modal>
  140. </div>
  141. </template>
  142. <script>
  143. import Modal from '../../components/Modal'
  144. import Form from './../../components/Form'
  145. import OtpDisplayer from '../../components/OtpDisplayer'
  146. export default {
  147. data() {
  148. return {
  149. migrationPayload: '',
  150. exportedAccounts: [],
  151. isFetching: false,
  152. form: new Form({
  153. service: '',
  154. account: '',
  155. otp_type: '',
  156. icon: '',
  157. secret: '',
  158. secretIsBase32Encoded: 1,
  159. algorithm: '',
  160. digits: null,
  161. counter: null,
  162. period: null,
  163. }),
  164. uploadForm: new Form(),
  165. ShowTwofaccountInModal : false,
  166. }
  167. },
  168. computed: {
  169. importableCount() {
  170. return this.exportedAccounts.filter(account => account.imported == -1 && account.id > -2).length;
  171. },
  172. duplicateCount() {
  173. return this.exportedAccounts.filter(account => account.id === -1 && account.imported === -1).length;
  174. },
  175. },
  176. mounted: async function() {
  177. // A migration URI is provided as route parameter, we extract the accounts from the URI and
  178. // list them in the view
  179. if( this.$route.params.migrationUri ) {
  180. this.migrate(this.$route.params.migrationUri)
  181. }
  182. this.$on('modalClose', function() {
  183. this.$refs.AdvancedFormOtpDisplayer.clearOTP()
  184. });
  185. },
  186. components: {
  187. Modal,
  188. OtpDisplayer,
  189. },
  190. methods: {
  191. /**
  192. * Post the migration payload
  193. */
  194. async migrate(migrationPayload) {
  195. this.migrationPayload = migrationPayload
  196. this.isFetching = true
  197. await this.axios.post('/api/v1/twofaccounts/migration', {payload: this.migrationPayload, withSecret: true}, {returnError: true}).then(response => {
  198. response.data.forEach((data) => {
  199. data.imported = -1;
  200. this.exportedAccounts.push(data)
  201. })
  202. this.notifyValidAccountFound()
  203. })
  204. .catch(error => {
  205. this.$notify({type: 'is-danger', text: this.$t(error.response.data.message) })
  206. });
  207. this.isFetching = false
  208. },
  209. /**
  210. * Remove all duplicates from the accounts list
  211. */
  212. discardDuplicates() {
  213. if(confirm(this.$t('twofaccounts.confirm.discard_duplicates'))) {
  214. this.$notify({ clean: true })
  215. this.$refs.AdvancedFormOtpDisplayer.clearOTP()
  216. // console.log(this.exportedAccounts.filter(account => account.id >= 0 && account.imported > -1))
  217. this.exportedAccounts = this.exportedAccounts.filter(account => account.id !== -1)
  218. }
  219. },
  220. /**
  221. * Clear the accounts list
  222. */
  223. discardAccounts() {
  224. if(confirm(this.$t('twofaccounts.confirm.discard_all'))) {
  225. this.$notify({ clean: true })
  226. this.$refs.AdvancedFormOtpDisplayer.clearOTP()
  227. this.exportedAccounts = []
  228. }
  229. },
  230. /**
  231. * Remove one duplicate from the accounts list
  232. */
  233. discardAccount(accountIndex) {
  234. if(confirm(this.$t('twofaccounts.confirm.discard'))) {
  235. this.exportedAccounts.splice(accountIndex, 1)
  236. }
  237. },
  238. /**
  239. * Batch store valid accounts, even duplicates
  240. */
  241. async createAccounts() {
  242. for (let index = 0; index < this.exportedAccounts.length; index++) {
  243. if (this.exportedAccounts[index].imported == -1) {
  244. await this.createAccount(index)
  245. }
  246. }
  247. },
  248. /**
  249. * Store the provided account
  250. */
  251. async createAccount(accountIndex) {
  252. let twofaccount = this.exportedAccounts[accountIndex]
  253. this.mapAccountToForm(twofaccount)
  254. await this.form.post('/api/v1/twofaccounts', {returnError: true})
  255. .then(response => {
  256. this.exportedAccounts[accountIndex].imported = 1
  257. this.exportedAccounts[accountIndex].id = response.data.id
  258. })
  259. .catch(error => {
  260. this.exportedAccounts[accountIndex].imported = 0
  261. this.exportedAccounts[accountIndex].id = 0
  262. this.exportedAccounts[accountIndex].errors = this.form.errors.flatten()
  263. });
  264. },
  265. /**
  266. * Generate a fresh OTP password and display it
  267. */
  268. previewAccount(accountIndex) {
  269. this.mapAccountToForm(this.exportedAccounts[accountIndex])
  270. .then(() => {
  271. this.$refs.AdvancedFormOtpDisplayer.$forceUpdate()
  272. this.$refs.AdvancedFormOtpDisplayer.show()
  273. })
  274. },
  275. /**
  276. * Map account field with the Form object
  277. */
  278. async mapAccountToForm(twofaccount) {
  279. this.form.account = twofaccount.account
  280. this.form.service = twofaccount.service
  281. this.form.otp_type = twofaccount.otp_type
  282. this.form.secret = twofaccount.secret
  283. this.form.algorithm = twofaccount.algorithm
  284. this.form.digits = twofaccount.digits
  285. this.form.counter = twofaccount.otp_type === 'hotp' ? twofaccount.counter : null
  286. this.form.period = twofaccount.otp_type === 'totp' ? twofaccount.period : null
  287. },
  288. /**
  289. * Upload the submitted file to the backend for parsing
  290. */
  291. submitFile() {
  292. this.isFetching = true
  293. let filedata = new FormData();
  294. filedata.append('file', this.$refs.fileInput.files[0]);
  295. filedata.append('withSecret', true);
  296. this.uploadForm.upload('/api/v1/twofaccounts/migration', filedata, {returnError: true}).then(response => {
  297. response.data.forEach((data) => {
  298. data.imported = -1;
  299. this.exportedAccounts.push(data)
  300. })
  301. this.notifyValidAccountFound()
  302. })
  303. .catch(error => {
  304. if( error.response.status !== 422 ) {
  305. this.$notify({type: 'is-danger', text: this.$t(error.response.data.message) })
  306. }
  307. });
  308. this.isFetching = false
  309. },
  310. /**
  311. * Upload the submitted QR code file to the backend for decoding
  312. */
  313. submitQrCode() {
  314. let imgdata = new FormData();
  315. imgdata.append('qrcode', this.$refs.qrcodeInput.files[0]);
  316. imgdata.append('withSecret', true);
  317. this.uploadForm.upload('/api/v1/qrcode/decode', imgdata, {returnError: true}).then(response => {
  318. this.migrate(response.data.data)
  319. })
  320. .catch(error => {
  321. if( error.response.status !== 422 ) {
  322. this.$notify({type: 'is-danger', text: this.$t(error.response.data.message) })
  323. }
  324. });
  325. },
  326. /**
  327. * Push user to the dedicated capture view for live scan
  328. */
  329. capture() {
  330. this.$router.push({ name: 'capture' });
  331. },
  332. /**
  333. * Notify that valid account(s) have been found for import
  334. */
  335. notifyValidAccountFound() {
  336. this.$notify({type: 'is-success', text: this.$t('twofaccounts.import.x_valid_accounts_found', { count: this.importableCount }) })
  337. }
  338. }
  339. }
  340. </script>