mixins.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. await this.axios.get('/user/logout')
  11. this.$storage.clear()
  12. delete this.axios.defaults.headers.common['Authorization']
  13. this.$router.push({ name: 'login' })
  14. },
  15. exitSettings: function(event) {
  16. if (event) {
  17. this.$notify({ clean: true })
  18. this.$router.push({ name: 'accounts' })
  19. }
  20. },
  21. isUrl: function (url) {
  22. var strRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/
  23. var re = new RegExp(strRegex)
  24. return re.test(url)
  25. },
  26. openInBrowser(uri) {
  27. const a = document.createElement('a')
  28. a.setAttribute('href', uri)
  29. a.dispatchEvent(new MouseEvent("click", {'view': window, 'bubbles': true, 'cancelable': true}))
  30. },
  31. /**
  32. * Parses the Public Key Options received from the Server for the browser.
  33. *
  34. * @param publicKey {Object}
  35. * @returns {Object}
  36. */
  37. parseIncomingServerOptions(publicKey) {
  38. publicKey.challenge = this.uint8Array(publicKey.challenge);
  39. if (publicKey.user !== undefined) {
  40. publicKey.user = {
  41. ...publicKey.user,
  42. id: this.uint8Array(publicKey.user.id, true)
  43. };
  44. }
  45. ["excludeCredentials", "allowCredentials"]
  46. .filter((key) => publicKey[key] !== undefined)
  47. .forEach((key) => {
  48. publicKey[key] = publicKey[key].map((data) => {
  49. return { ...data, id: this.uint8Array(data.id) };
  50. });
  51. });
  52. return publicKey;
  53. },
  54. /**
  55. * Parses the outgoing credentials from the browser to the server.
  56. *
  57. * @param credentials {Credential|PublicKeyCredential}
  58. * @return {{response: {string}, rawId: string, id: string, type: string}}
  59. */
  60. parseOutgoingCredentials(credentials) {
  61. let parseCredentials = {
  62. id: credentials.id,
  63. type: credentials.type,
  64. rawId: this.arrayToBase64String(credentials.rawId),
  65. response: {}
  66. };
  67. [
  68. "clientDataJSON",
  69. "attestationObject",
  70. "authenticatorData",
  71. "signature",
  72. "userHandle"
  73. ]
  74. .filter((key) => credentials.response[key] !== undefined)
  75. .forEach((key) => {
  76. if( credentials.response[key] === null )
  77. {
  78. parseCredentials.response[key] = null
  79. }
  80. else {
  81. parseCredentials.response[key] = this.arrayToBase64String(
  82. credentials.response[key]
  83. );
  84. }
  85. });
  86. return parseCredentials;
  87. },
  88. /**
  89. * Transform an string into Uint8Array instance.
  90. *
  91. * @param input {string}
  92. * @param atob {boolean}
  93. * @returns {Uint8Array}
  94. */
  95. uint8Array(input, atob = false) {
  96. return Uint8Array.from(
  97. atob ? window.atob(input) : this.base64UrlDecode(input),
  98. (c) => c.charCodeAt(0)
  99. );
  100. },
  101. /**
  102. * Encodes an array of bytes to a BASE64 URL string
  103. *
  104. * @param arrayBuffer {ArrayBuffer|Uint8Array}
  105. * @returns {string}
  106. */
  107. arrayToBase64String(arrayBuffer) {
  108. return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
  109. },
  110. /**
  111. *
  112. * Decodes a BASE64 URL string into a normal string.
  113. *
  114. * @param input {string}
  115. * @returns {string|Iterable}
  116. */
  117. base64UrlDecode(input) {
  118. input = input.replace(/-/g, "+").replace(/_/g, "/");
  119. const pad = input.length % 4;
  120. if (pad) {
  121. if (pad === 1) {
  122. throw new Error(
  123. "InvalidLengthError: Input base64url string is the wrong length to determine padding"
  124. );
  125. }
  126. input += new Array(5 - pad).join("=");
  127. }
  128. return window.atob(input);
  129. },
  130. }
  131. })