SecurityTab.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <script setup>
  2. import { onMounted, reactive, ref } from 'vue';
  3. import { useI18n } from 'vue-i18n';
  4. import { Modal, message } from 'ant-design-vue';
  5. import { HttpUtil, RandomUtil } from '@/utils';
  6. import SettingListItem from '@/components/SettingListItem.vue';
  7. import TwoFactorModal from './TwoFactorModal.vue';
  8. const { t } = useI18n();
  9. const props = defineProps({
  10. allSetting: { type: Object, required: true },
  11. });
  12. // 2FA modal state — both the "set" (enabling) and "confirm" (changing
  13. // password / disabling) flows route through the same component.
  14. const tfa = reactive({
  15. open: false,
  16. title: '',
  17. description: '',
  18. token: '',
  19. type: 'set',
  20. // resolveConfirm is called by the modal's @confirm with the success bool;
  21. // it then routes the value back to whichever flow opened the modal.
  22. resolveConfirm: (_success) => { },
  23. });
  24. function openTfa({ title, description = '', token = '', type, onConfirm }) {
  25. tfa.title = title;
  26. tfa.description = description;
  27. tfa.token = token;
  28. tfa.type = type;
  29. tfa.resolveConfirm = onConfirm;
  30. tfa.open = true;
  31. }
  32. function onTfaConfirm(success) {
  33. tfa.resolveConfirm(success);
  34. }
  35. const user = reactive({
  36. oldUsername: '',
  37. oldPassword: '',
  38. newUsername: '',
  39. newPassword: '',
  40. });
  41. const updating = ref(false);
  42. async function sendUpdateUser() {
  43. updating.value = true;
  44. try {
  45. const msg = await HttpUtil.post('/panel/setting/updateUser', user);
  46. if (msg?.success) {
  47. // Force re-login at the standard logout path; basePath is handled
  48. // by the Go router so a relative redirect is correct here.
  49. const basePath = window.__X_UI_BASE_PATH__ || '';
  50. window.location.replace(`${basePath}logout`);
  51. }
  52. } finally {
  53. updating.value = false;
  54. }
  55. }
  56. function updateUser() {
  57. if (props.allSetting.twoFactorEnable) {
  58. openTfa({
  59. title: t('pages.settings.security.twoFactorModalChangeCredentialsTitle'),
  60. description: t('pages.settings.security.twoFactorModalChangeCredentialsStep'),
  61. token: props.allSetting.twoFactorToken,
  62. type: 'confirm',
  63. onConfirm: (ok) => { if (ok) sendUpdateUser(); },
  64. });
  65. } else {
  66. sendUpdateUser();
  67. }
  68. }
  69. // === API Token =========================================================
  70. // Surfaces the panel's API token so a remote central panel can register
  71. // this instance as a node. Lazy-loaded on tab mount; rotation requires
  72. // confirmation since it invalidates any cached value upstream.
  73. const apiToken = ref('');
  74. const apiTokenLoading = ref(false);
  75. const apiTokenRotating = ref(false);
  76. async function loadApiToken() {
  77. apiTokenLoading.value = true;
  78. try {
  79. const msg = await HttpUtil.get('/panel/setting/getApiToken');
  80. if (msg?.success) apiToken.value = msg.obj || '';
  81. } finally {
  82. apiTokenLoading.value = false;
  83. }
  84. }
  85. async function copyApiToken() {
  86. if (!apiToken.value) return;
  87. try {
  88. await navigator.clipboard.writeText(apiToken.value);
  89. message.success(t('copySuccess'));
  90. } catch (_e) {
  91. // navigator.clipboard can be undefined on http:// — fall back to
  92. // a transient input + execCommand path.
  93. const ta = document.createElement('textarea');
  94. ta.value = apiToken.value;
  95. document.body.appendChild(ta);
  96. ta.select();
  97. document.execCommand('copy');
  98. document.body.removeChild(ta);
  99. message.success(t('copySuccess'));
  100. }
  101. }
  102. function regenerateApiToken() {
  103. Modal.confirm({
  104. title: t('pages.nodes.regenerateConfirm'),
  105. okText: t('confirm'),
  106. cancelText: t('cancel'),
  107. okType: 'danger',
  108. onOk: async () => {
  109. apiTokenRotating.value = true;
  110. try {
  111. const msg = await HttpUtil.post('/panel/setting/regenerateApiToken');
  112. if (msg?.success) {
  113. apiToken.value = msg.obj || '';
  114. message.success(t('success'));
  115. }
  116. } finally {
  117. apiTokenRotating.value = false;
  118. }
  119. },
  120. });
  121. }
  122. onMounted(loadApiToken);
  123. function toggleTwoFactor() {
  124. // Switch read-only — the actual flip happens after the modal succeeds.
  125. if (!props.allSetting.twoFactorEnable) {
  126. const newToken = RandomUtil.randomBase32String();
  127. openTfa({
  128. title: t('pages.settings.security.twoFactorModalSetTitle'),
  129. token: newToken,
  130. type: 'set',
  131. onConfirm: (ok) => {
  132. if (ok) {
  133. message.success(t('pages.settings.security.twoFactorModalSetSuccess'));
  134. props.allSetting.twoFactorToken = newToken;
  135. }
  136. props.allSetting.twoFactorEnable = ok;
  137. },
  138. });
  139. } else {
  140. openTfa({
  141. title: t('pages.settings.security.twoFactorModalDeleteTitle'),
  142. description: t('pages.settings.security.twoFactorModalRemoveStep'),
  143. token: props.allSetting.twoFactorToken,
  144. type: 'confirm',
  145. onConfirm: (ok) => {
  146. if (!ok) return;
  147. message.success(t('pages.settings.security.twoFactorModalDeleteSuccess'));
  148. props.allSetting.twoFactorEnable = false;
  149. props.allSetting.twoFactorToken = '';
  150. },
  151. });
  152. }
  153. }
  154. </script>
  155. <template>
  156. <a-collapse default-active-key="1">
  157. <a-collapse-panel key="1" :header="t('pages.settings.security.admin')">
  158. <SettingListItem paddings="small">
  159. <template #title>{{ t('pages.settings.oldUsername') }}</template>
  160. <template #control>
  161. <a-input v-model:value="user.oldUsername" autocomplete="username" />
  162. </template>
  163. </SettingListItem>
  164. <SettingListItem paddings="small">
  165. <template #title>{{ t('pages.settings.currentPassword') }}</template>
  166. <template #control>
  167. <a-input-password v-model:value="user.oldPassword" autocomplete="current-password" />
  168. </template>
  169. </SettingListItem>
  170. <SettingListItem paddings="small">
  171. <template #title>{{ t('pages.settings.newUsername') }}</template>
  172. <template #control>
  173. <a-input v-model:value="user.newUsername" />
  174. </template>
  175. </SettingListItem>
  176. <SettingListItem paddings="small">
  177. <template #title>{{ t('pages.settings.newPassword') }}</template>
  178. <template #control>
  179. <a-input-password v-model:value="user.newPassword" autocomplete="new-password" />
  180. </template>
  181. </SettingListItem>
  182. <a-list-item>
  183. <a-space direction="horizontal" :style="{ padding: '0 20px' }">
  184. <a-button type="primary" :loading="updating" @click="updateUser">{{ t('confirm') }}</a-button>
  185. </a-space>
  186. </a-list-item>
  187. </a-collapse-panel>
  188. <a-collapse-panel key="2" :header="t('pages.settings.security.twoFactor')">
  189. <SettingListItem paddings="small">
  190. <template #title>{{ t('pages.settings.security.twoFactorEnable') }}</template>
  191. <template #description>{{ t('pages.settings.security.twoFactorEnableDesc') }}</template>
  192. <template #control>
  193. <a-switch :checked="allSetting.twoFactorEnable" @click="toggleTwoFactor" />
  194. </template>
  195. </SettingListItem>
  196. </a-collapse-panel>
  197. <a-collapse-panel key="3" :header="t('pages.nodes.apiToken')">
  198. <SettingListItem paddings="small">
  199. <template #title>{{ t('pages.nodes.apiToken') }}</template>
  200. <template #description>{{ t('pages.nodes.apiTokenHint') }}</template>
  201. <template #control>
  202. <a-input-password :value="apiToken" readonly :loading="apiTokenLoading" style="min-width: 240px" />
  203. </template>
  204. </SettingListItem>
  205. <a-list-item>
  206. <a-space direction="horizontal" :style="{ padding: '0 20px' }">
  207. <a-button :disabled="!apiToken" @click="copyApiToken">{{ t('copy') }}</a-button>
  208. <a-button danger :loading="apiTokenRotating" @click="regenerateApiToken">
  209. {{ t('pages.nodes.regenerate') }}
  210. </a-button>
  211. </a-space>
  212. </a-list-item>
  213. </a-collapse-panel>
  214. </a-collapse>
  215. <TwoFactorModal v-model:open="tfa.open" :title="tfa.title" :description="tfa.description" :token="tfa.token"
  216. :type="tfa.type" @confirm="onTfaConfirm" />
  217. </template>