WebAuthn.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <div>
  3. <setting-tabs :activeTab="'settings.webauthn.devices'"></setting-tabs>
  4. <div class="options-tabs">
  5. <form-wrapper>
  6. <div v-if="isRemoteUser" class="notification is-warning has-text-centered" v-html="$t('auth.auth_handled_by_proxy')" />
  7. <h4 class="title is-4 has-text-grey-light">{{ $t('auth.webauthn.security_devices') }}</h4>
  8. <div class="is-size-7-mobile">
  9. {{ $t('auth.webauthn.security_devices_legend')}}
  10. </div>
  11. <div class="mt-3">
  12. <a tabindex="0" @click="register" @keyup.enter="register">
  13. <font-awesome-icon :icon="['fas', 'plus-circle']" />&nbsp;{{ $t('auth.webauthn.register_a_new_device')}}
  14. </a>
  15. </div>
  16. <!-- credentials list -->
  17. <div v-if="credentials.length > 0" class="field">
  18. <div v-for="credential in credentials" :key="credential.id" class="group-item is-size-5 is-size-6-mobile">
  19. {{ displayName(credential) }}
  20. <!-- revoke link -->
  21. <button class="button tag is-pulled-right" :class="$root.showDarkMode ? 'is-dark':'is-white'" @click="revokeCredential(credential.id)" :title="$t('settings.revoke')">
  22. {{ $t('settings.revoke') }}
  23. </button>
  24. <!-- edit link -->
  25. <!-- <router-link :to="{ name: '' }" class="has-text-grey pl-1" :title="$t('commons.rename')">
  26. <font-awesome-icon :icon="['fas', 'pen-square']" />
  27. </router-link> -->
  28. </div>
  29. <div class="mt-2 is-size-7 is-pulled-right">
  30. {{ $t('auth.webauthn.revoking_a_device_is_permanent')}}
  31. </div>
  32. </div>
  33. <div v-if="isFetching && credentials.length === 0" class="has-text-centered mt-6">
  34. <span class="is-size-4">
  35. <font-awesome-icon :icon="['fas', 'spinner']" spin />
  36. </span>
  37. </div>
  38. <h4 class="title is-4 pt-6 has-text-grey-light">{{ $t('settings.options') }}</h4>
  39. <div class="field">
  40. {{ $t('auth.webauthn.need_a_security_device_to_enable_options')}}
  41. </div>
  42. <form>
  43. <!-- use webauthn only -->
  44. <form-checkbox v-on:useWebauthnOnly="saveSetting('useWebauthnOnly', $event)" :form="form" fieldName="useWebauthnOnly" :label="$t('auth.webauthn.use_webauthn_only.label')" :help="$t('auth.webauthn.use_webauthn_only.help')" :disabled="isRemoteUser || credentials.length === 0" />
  45. <!-- default sign in method -->
  46. <form-checkbox v-on:useWebauthnAsDefault="saveSetting('useWebauthnAsDefault', $event)" :form="form" fieldName="useWebauthnAsDefault" :label="$t('auth.webauthn.use_webauthn_as_default.label')" :help="$t('auth.webauthn.use_webauthn_as_default.help')" :disabled="isRemoteUser || credentials.length === 0" />
  47. </form>
  48. <!-- footer -->
  49. <vue-footer :showButtons="true">
  50. <!-- close button -->
  51. <p class="control">
  52. <router-link :to="{ name: 'accounts', params: { toRefresh: false } }" class="button is-rounded" :class="{'is-dark' : $root.showDarkMode}">{{ $t('commons.close') }}</router-link>
  53. </p>
  54. </vue-footer>
  55. </form-wrapper>
  56. </div>
  57. </div>
  58. </template>
  59. <script>
  60. import Form from './../../components/Form'
  61. import WebAuthn from './../../components/WebAuthn'
  62. export default {
  63. data(){
  64. return {
  65. form: new Form({
  66. useWebauthnOnly: null,
  67. useWebauthnAsDefault: null,
  68. }),
  69. credentials: [],
  70. isFetching: false,
  71. isRemoteUser: false,
  72. webauthn: new WebAuthn()
  73. }
  74. },
  75. async mounted() {
  76. const { data } = await this.form.get('/api/v1/settings')
  77. this.form.fillWithKeyValueObject(data)
  78. this.form.setOriginal()
  79. this.fetchCredentials()
  80. },
  81. methods : {
  82. /**
  83. * Save a setting
  84. */
  85. saveSetting(settingName, event) {
  86. this.axios.put('/api/v1/settings/' + settingName, { value: event }).then(response => {
  87. this.$notify({ type: 'is-success', text: this.$t('settings.forms.setting_saved') })
  88. this.$root.appSettings[response.data.key] = response.data.value
  89. })
  90. },
  91. /**
  92. * Get all credentials from backend
  93. */
  94. async fetchCredentials() {
  95. this.isFetching = true
  96. await this.axios.get('/webauthn/credentials', {returnError: true})
  97. .then(response => {
  98. this.credentials = response.data
  99. })
  100. .catch(error => {
  101. if( error.response.status === 400 ) {
  102. this.isRemoteUser = true
  103. }
  104. else {
  105. this.$router.push({ name: 'genericError', params: { err: error.response } });
  106. }
  107. })
  108. this.isFetching = false
  109. },
  110. /**
  111. * Register a new security device
  112. */
  113. async register() {
  114. if (this.isRemoteUser) {
  115. this.$notify({ type: 'is-warning', text: this.$t('errors.unsupported_with_reverseproxy') })
  116. return false
  117. }
  118. // Check https context
  119. if (!window.isSecureContext) {
  120. this.$notify({ type: 'is-danger', text: this.$t('errors.https_required') })
  121. return false
  122. }
  123. // Check browser support
  124. if (this.webauthn.doesntSupportWebAuthn) {
  125. this.$notify({ type: 'is-danger', text: this.$t('errors.browser_does_not_support_webauthn') })
  126. return false
  127. }
  128. const registerOptions = await this.axios.post('/webauthn/register/options').then(res => res.data)
  129. const publicKey = this.webauthn.parseIncomingServerOptions(registerOptions)
  130. let bufferedCredentials
  131. try {
  132. bufferedCredentials = await navigator.credentials.create({ publicKey })
  133. }
  134. catch (error) {
  135. if (error.name == 'AbortError') {
  136. this.$notify({ type: 'is-warning', text: this.$t('errors.aborted_by_user') })
  137. }
  138. else if (error.name == 'SecurityError') {
  139. this.$notify({ type: 'is-danger', text: this.$t('errors.security_error_check_rpid') })
  140. }
  141. else if (error.name == 'InvalidStateError') {
  142. this.$notify({ type: 'is-danger', text: this.$t('errors.security_device_unsupported') })
  143. }
  144. else if (error.name == 'NotAllowedError') {
  145. this.$notify({ type: 'is-danger', text: this.$t('errors.not_allowed_operation') })
  146. }
  147. return false
  148. }
  149. const publicKeyCredential = this.webauthn.parseOutgoingCredentials(bufferedCredentials);
  150. this.axios.post('/webauthn/register', publicKeyCredential).then(response => {
  151. this.$router.push({ name: 'settings.webauthn.editCredential', params: { id: publicKeyCredential.id, name: this.$t('auth.webauthn.my_device') } })
  152. })
  153. },
  154. /**
  155. * revoke a credential
  156. */
  157. async revokeCredential(credentialId) {
  158. if(confirm(this.$t('auth.confirm.revoke_device'))) {
  159. await this.axios.delete('/webauthn/credentials/' + credentialId).then(response => {
  160. // Remove the revoked credential from the collection
  161. this.credentials = this.credentials.filter(a => a.id !== credentialId)
  162. if (this.credentials.length == 0) {
  163. this.form.useWebauthnOnly = false
  164. this.form.useWebauthnAsDefault = false
  165. this.$root.appSettings['useWebauthnOnly'] = false
  166. this.$root.appSettings['useWebauthnAsDefault'] = false
  167. }
  168. this.$notify({ type: 'is-success', text: this.$t('auth.webauthn.device_revoked') })
  169. });
  170. }
  171. },
  172. /**
  173. * Always display a printable name
  174. */
  175. displayName(credential) {
  176. return credential.alias ? credential.alias : this.$t('auth.webauthn.my_device') + ' (#' + credential.id.substring(0, 10) + ')'
  177. },
  178. },
  179. }
  180. </script>