WorkflowProtect.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <div>
  3. <form
  4. class="mb-4 flex w-full items-center"
  5. @submit.prevent="protectWorkflow"
  6. >
  7. <ui-input
  8. v-model="state.password"
  9. :placeholder="t('common.password')"
  10. :type="state.showPassword ? 'text' : 'password'"
  11. input-class="pr-10"
  12. autofocus
  13. class="mr-6 flex-1"
  14. >
  15. <template #append>
  16. <v-remixicon
  17. :name="state.showPassword ? 'riEyeOffLine' : 'riEyeLine'"
  18. class="absolute right-2"
  19. @click="state.showPassword = !state.showPassword"
  20. />
  21. </template>
  22. </ui-input>
  23. <ui-button variant="accent">
  24. {{ t('workflow.protect.button') }}
  25. </ui-button>
  26. </form>
  27. <p>
  28. {{ t('workflow.protect.note') }}
  29. </p>
  30. </div>
  31. </template>
  32. <script setup>
  33. import { shallowReactive } from 'vue';
  34. import { useI18n } from 'vue-i18n';
  35. import { nanoid } from 'nanoid';
  36. import AES from 'crypto-js/aes';
  37. import hmacSHA256 from 'crypto-js/hmac-sha256';
  38. import getPassKey from '@/utils/getPassKey';
  39. const props = defineProps({
  40. workflow: {
  41. type: Object,
  42. default: () => ({}),
  43. },
  44. });
  45. const emit = defineEmits(['update', 'close']);
  46. const { t } = useI18n();
  47. const state = shallowReactive({
  48. password: '',
  49. showPassword: false,
  50. });
  51. async function protectWorkflow() {
  52. const key = getPassKey(nanoid());
  53. const encryptedPass = AES.encrypt(state.password, key).toString();
  54. const hmac = hmacSHA256(encryptedPass, state.password).toString();
  55. const { drawflow } = props.workflow;
  56. const flow =
  57. typeof drawflow === 'string' ? drawflow : JSON.stringify(drawflow);
  58. emit('update', {
  59. isProtected: true,
  60. pass: hmac + encryptedPass,
  61. drawflow: AES.encrypt(flow, state.password).toString(),
  62. });
  63. emit('close');
  64. }
  65. </script>