mixins.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import Vue from 'vue'
  2. Vue.mixin({
  3. data: function () {
  4. return {
  5. appVersion: window.appVersion
  6. }
  7. },
  8. methods: {
  9. async appLogout(evt) {
  10. if (this.$root.appConfig.proxyAuth) {
  11. if (this.$root.appConfig.proxyLogoutUrl) {
  12. location.assign(this.$root.appConfig.proxyLogoutUrl)
  13. }
  14. else return false
  15. }
  16. else {
  17. await this.axios.get('/user/logout')
  18. this.$storage.clear()
  19. this.$router.push({ name: 'login', params: { forceRefresh: true } })
  20. }
  21. },
  22. exitSettings: function (event) {
  23. if (event) {
  24. this.$notify({ clean: true })
  25. this.$router.push({ name: 'accounts' })
  26. }
  27. },
  28. isUrl: function (url) {
  29. var strRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/
  30. var re = new RegExp(strRegex)
  31. return re.test(url)
  32. },
  33. openInBrowser(uri) {
  34. const a = document.createElement('a')
  35. a.setAttribute('href', uri)
  36. a.dispatchEvent(new MouseEvent("click", { 'view': window, 'bubbles': true, 'cancelable': true }))
  37. },
  38. /**
  39. * Parses the Public Key Options received from the Server for the browser.
  40. *
  41. * @param publicKey {Object}
  42. * @returns {Object}
  43. */
  44. parseIncomingServerOptions(publicKey) {
  45. publicKey.challenge = this.uint8Array(publicKey.challenge);
  46. if (publicKey.user !== undefined) {
  47. publicKey.user = {
  48. ...publicKey.user,
  49. id: this.uint8Array(publicKey.user.id, true)
  50. };
  51. }
  52. ["excludeCredentials", "allowCredentials"]
  53. .filter((key) => publicKey[key] !== undefined)
  54. .forEach((key) => {
  55. publicKey[key] = publicKey[key].map((data) => {
  56. return { ...data, id: this.uint8Array(data.id) };
  57. });
  58. });
  59. return publicKey;
  60. },
  61. /**
  62. * Parses the outgoing credentials from the browser to the server.
  63. *
  64. * @param credentials {Credential|PublicKeyCredential}
  65. * @return {{response: {string}, rawId: string, id: string, type: string}}
  66. */
  67. parseOutgoingCredentials(credentials) {
  68. let parseCredentials = {
  69. id: credentials.id,
  70. type: credentials.type,
  71. rawId: this.arrayToBase64String(credentials.rawId),
  72. response: {}
  73. };
  74. [
  75. "clientDataJSON",
  76. "attestationObject",
  77. "authenticatorData",
  78. "signature",
  79. "userHandle"
  80. ]
  81. .filter((key) => credentials.response[key] !== undefined)
  82. .forEach((key) => {
  83. if (credentials.response[key] === null) {
  84. parseCredentials.response[key] = null
  85. }
  86. else {
  87. parseCredentials.response[key] = this.arrayToBase64String(
  88. credentials.response[key]
  89. );
  90. }
  91. });
  92. return parseCredentials;
  93. },
  94. /**
  95. * Transform an string into Uint8Array instance.
  96. *
  97. * @param input {string}
  98. * @param atob {boolean}
  99. * @returns {Uint8Array}
  100. */
  101. uint8Array(input, atob = false) {
  102. return Uint8Array.from(
  103. atob ? window.atob(input) : this.base64UrlDecode(input),
  104. (c) => c.charCodeAt(0)
  105. );
  106. },
  107. /**
  108. * Encodes an array of bytes to a BASE64 URL string
  109. *
  110. * @param arrayBuffer {ArrayBuffer|Uint8Array}
  111. * @returns {string}
  112. */
  113. arrayToBase64String(arrayBuffer) {
  114. return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
  115. },
  116. /**
  117. *
  118. * Decodes a BASE64 URL string into a normal string.
  119. *
  120. * @param input {string}
  121. * @returns {string|Iterable}
  122. */
  123. base64UrlDecode(input) {
  124. input = input.replace(/-/g, "+").replace(/_/g, "/");
  125. const pad = input.length % 4;
  126. if (pad) {
  127. if (pad === 1) {
  128. throw new Error(
  129. "InvalidLengthError: Input base64url string is the wrong length to determine padding"
  130. );
  131. }
  132. input += new Array(5 - pad).join("=");
  133. }
  134. return window.atob(input);
  135. },
  136. /**
  137. * Encodes an array of bytes to a BASE64 URL string
  138. *
  139. * @param arrayBuffer {ArrayBuffer|Uint8Array}
  140. * @returns {string}
  141. */
  142. inputId(fieldType, fieldName) {
  143. let prefix
  144. fieldName = fieldName.toString()
  145. switch (fieldType) {
  146. case 'button':
  147. prefix = 'txt'
  148. break
  149. case 'button':
  150. prefix = 'btn'
  151. break
  152. case 'email':
  153. prefix = 'eml'
  154. break
  155. case 'password':
  156. prefix = 'pwd'
  157. break
  158. case 'radio':
  159. prefix = 'rdo'
  160. break
  161. case 'label':
  162. prefix = 'lbl'
  163. break
  164. default:
  165. prefix = 'txt'
  166. break
  167. }
  168. return prefix + fieldName[0].toUpperCase() + fieldName.toLowerCase().slice(1);
  169. // button
  170. // checkbox
  171. // color
  172. // date
  173. // datetime-local
  174. // file
  175. // hidden
  176. // image
  177. // month
  178. // number
  179. // radio
  180. // range
  181. // reset
  182. // search
  183. // submit
  184. // tel
  185. // text
  186. // time
  187. // url
  188. // week
  189. },
  190. }
  191. })