logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

mfa.js (4329B)


  1. import RecoveryCodes from './mfa_backup_codes.vue'
  2. import TOTP from './mfa_totp.vue'
  3. import Confirm from './confirm.vue'
  4. import VueQrcode from '@chenfengyuan/vue-qrcode'
  5. import { mapState } from 'vuex'
  6. const Mfa = {
  7. data: () => ({
  8. settings: { // current settings of MFA
  9. available: false,
  10. enabled: false,
  11. totp: false
  12. },
  13. setupState: { // setup mfa
  14. state: '', // state of setup. '' -> 'getBackupCodes' -> 'setupOTP' -> 'complete'
  15. setupOTPState: '' // state of setup otp. '' -> 'prepare' -> 'confirm' -> 'complete'
  16. },
  17. backupCodes: {
  18. getNewCodes: false,
  19. inProgress: false, // progress of fetch codes
  20. codes: []
  21. },
  22. otpSettings: { // pre-setup setting of OTP. secret key, qrcode url.
  23. provisioning_uri: '',
  24. key: ''
  25. },
  26. currentPassword: null,
  27. otpConfirmToken: null,
  28. error: null,
  29. readyInit: false
  30. }),
  31. components: {
  32. 'recovery-codes': RecoveryCodes,
  33. 'totp-item': TOTP,
  34. qrcode: VueQrcode,
  35. confirm: Confirm
  36. },
  37. computed: {
  38. canSetupOTP () {
  39. return (
  40. (this.setupInProgress && this.backupCodesPrepared) ||
  41. this.settings.enabled
  42. ) && !this.settings.totp && !this.setupOTPInProgress
  43. },
  44. setupInProgress () {
  45. return this.setupState.state !== '' && this.setupState.state !== 'complete'
  46. },
  47. setupOTPInProgress () {
  48. return this.setupState.state === 'setupOTP' && !this.completedOTP
  49. },
  50. prepareOTP () {
  51. return this.setupState.setupOTPState === 'prepare'
  52. },
  53. confirmOTP () {
  54. return this.setupState.setupOTPState === 'confirm'
  55. },
  56. completedOTP () {
  57. return this.setupState.setupOTPState === 'completed'
  58. },
  59. backupCodesPrepared () {
  60. return !this.backupCodes.inProgress && this.backupCodes.codes.length > 0
  61. },
  62. confirmNewBackupCodes () {
  63. return this.backupCodes.getNewCodes
  64. },
  65. ...mapState({
  66. backendInteractor: (state) => state.api.backendInteractor
  67. })
  68. },
  69. methods: {
  70. activateOTP () {
  71. if (!this.settings.enabled) {
  72. this.setupState.state = 'getBackupcodes'
  73. this.fetchBackupCodes()
  74. }
  75. },
  76. fetchBackupCodes () {
  77. this.backupCodes.inProgress = true
  78. this.backupCodes.codes = []
  79. return this.backendInteractor.generateMfaBackupCodes()
  80. .then((res) => {
  81. this.backupCodes.codes = res.codes
  82. this.backupCodes.inProgress = false
  83. })
  84. },
  85. getBackupCodes () { // get a new backup codes
  86. this.backupCodes.getNewCodes = true
  87. },
  88. confirmBackupCodes () { // confirm getting new backup codes
  89. this.fetchBackupCodes().then((res) => {
  90. this.backupCodes.getNewCodes = false
  91. })
  92. },
  93. cancelBackupCodes () { // cancel confirm form of new backup codes
  94. this.backupCodes.getNewCodes = false
  95. },
  96. // Setup OTP
  97. setupOTP () { // prepare setup OTP
  98. this.setupState.state = 'setupOTP'
  99. this.setupState.setupOTPState = 'prepare'
  100. this.backendInteractor.mfaSetupOTP()
  101. .then((res) => {
  102. this.otpSettings = res
  103. this.setupState.setupOTPState = 'confirm'
  104. })
  105. },
  106. doConfirmOTP () { // handler confirm enable OTP
  107. this.error = null
  108. this.backendInteractor.mfaConfirmOTP({
  109. token: this.otpConfirmToken,
  110. password: this.currentPassword
  111. })
  112. .then((res) => {
  113. if (res.error) {
  114. this.error = res.error
  115. return
  116. }
  117. this.completeSetup()
  118. })
  119. },
  120. completeSetup () {
  121. this.setupState.setupOTPState = 'complete'
  122. this.setupState.state = 'complete'
  123. this.currentPassword = null
  124. this.error = null
  125. this.fetchSettings()
  126. },
  127. cancelSetup () { // cancel setup
  128. this.setupState.setupOTPState = ''
  129. this.setupState.state = ''
  130. this.currentPassword = null
  131. this.error = null
  132. },
  133. // end Setup OTP
  134. // fetch settings from server
  135. async fetchSettings () {
  136. const result = await this.backendInteractor.settingsMFA()
  137. if (result.error) return
  138. this.settings = result.settings
  139. this.settings.available = true
  140. return result
  141. }
  142. },
  143. mounted () {
  144. this.fetchSettings().then(() => {
  145. this.readyInit = true
  146. })
  147. }
  148. }
  149. export default Mfa