From 57efcaf35c4e053722b75bdf620ad4f280ab6bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Fri, 26 Jul 2024 15:56:13 +0200 Subject: [PATCH] GoToSocial: Support account security settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- src/actions/security.ts | 65 +++++++++++++++++++++++++++++------------ src/locales/pl.json | 2 +- src/utils/features.ts | 5 +++- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/actions/security.ts b/src/actions/security.ts index b3cb0ebcc1..dcc26c22dd 100644 --- a/src/actions/security.ts +++ b/src/actions/security.ts @@ -4,12 +4,12 @@ * @see module:soapbox/actions/auth */ +import api from 'soapbox/api'; import toast from 'soapbox/toast'; import { getLoggedInAccount } from 'soapbox/utils/auth'; +import { GOTOSOCIAL, parseVersion } from 'soapbox/utils/features'; import { normalizeUsername } from 'soapbox/utils/input'; -import api from '../api'; - import { AUTH_LOGGED_OUT, messages } from './auth'; import type { AppDispatch, RootState } from 'soapbox/store'; @@ -69,20 +69,39 @@ const revokeOAuthTokenById = (id: number) => const changePassword = (oldPassword: string, newPassword: string, confirmation: string) => (dispatch: AppDispatch, getState: () => RootState) => { dispatch({ type: CHANGE_PASSWORD_REQUEST }); - return api(getState)('/api/pleroma/change_password', { - method: 'POST', - body: JSON.stringify({ - password: oldPassword, - new_password: newPassword, - new_password_confirmation: confirmation, - }), - }).then(response => { - if (response.json.error) throw response.json.error; // This endpoint returns HTTP 200 even on failure - dispatch({ type: CHANGE_PASSWORD_SUCCESS, response }); - }).catch(error => { - dispatch({ type: CHANGE_PASSWORD_FAIL, error, skipAlert: true }); - throw error; - }); + const state = getState(); + const instance = state.instance; + const v = parseVersion(instance.version); + + if (v.software === GOTOSOCIAL) { + return api(getState)('/api/v1/user/password_change', { + method: 'POST', + body: JSON.stringify({ + old_password: oldPassword, + new_password: newPassword, + }), + }).then(response => { + dispatch({ type: CHANGE_PASSWORD_SUCCESS, response }); + }).catch(error => { + dispatch({ type: CHANGE_PASSWORD_FAIL, error, skipAlert: true }); + throw error; + }); + } else { + return api(getState)('/api/pleroma/change_password', { + method: 'POST', + body: JSON.stringify({ + password: oldPassword, + new_password: newPassword, + new_password_confirmation: confirmation, + }), + }).then(response => { + if (response.json.error) throw response.json.error; // This endpoint returns HTTP 200 even on failure + dispatch({ type: CHANGE_PASSWORD_SUCCESS, response }); + }).catch(error => { + dispatch({ type: CHANGE_PASSWORD_FAIL, error, skipAlert: true }); + throw error; + }); + } }; const resetPassword = (usernameOrEmail: string) => @@ -110,10 +129,14 @@ const resetPassword = (usernameOrEmail: string) => const changeEmail = (email: string, password: string) => (dispatch: AppDispatch, getState: () => RootState) => { dispatch({ type: CHANGE_EMAIL_REQUEST, email }); - return api(getState)('/api/pleroma/change_email', { + const state = getState(); + const instance = state.instance; + const v = parseVersion(instance.version); + + return api(getState)(v.software === GOTOSOCIAL ? '/api/v1/user/email_change' : '/api/pleroma/change_email', { method: 'POST', body: JSON.stringify({ - email, + [v.software === GOTOSOCIAL ? 'new_email' : 'email']: email, password, }), }).then(response => { @@ -127,10 +150,14 @@ const changeEmail = (email: string, password: string) => const deleteAccount = (password: string) => (dispatch: AppDispatch, getState: () => RootState) => { + dispatch({ type: CHANGE_PASSWORD_REQUEST }); const account = getLoggedInAccount(getState()); + const state = getState(); + const instance = state.instance; + const v = parseVersion(instance.version); dispatch({ type: DELETE_ACCOUNT_REQUEST }); - return api(getState)('/api/pleroma/delete_account', { + return api(getState)(v.software === GOTOSOCIAL ? '/api/v1/accounts/delete' : '/api/pleroma/delete_account', { method: 'POST', body: JSON.stringify({ password }), }).then(response => { diff --git a/src/locales/pl.json b/src/locales/pl.json index 1f016e857b..b4665a7171 100644 --- a/src/locales/pl.json +++ b/src/locales/pl.json @@ -1301,7 +1301,7 @@ "security.fields.new_password.label": "Nowe hasło", "security.fields.old_password.label": "Obecne hasło", "security.fields.password.label": "Hasło", - "security.fields.password_confirmation.label": "Obecne hasło (ponownie)", + "security.fields.password_confirmation.label": "Nowe hasło (ponownie)", "security.headers.delete": "Usuń konto", "security.headers.tokens": "Sesje", "security.qr.fail": "Nie udało się uzyskać klucza konfiguracyjnego", diff --git a/src/utils/features.ts b/src/utils/features.ts index b50d679791..4646859902 100644 --- a/src/utils/features.ts +++ b/src/utils/features.ts @@ -877,8 +877,11 @@ const getInstanceFeatures = (instance: Instance) => { * @see POST /api/pleroma/change_password * @see POST /api/pleroma/change_email * @see POST /api/pleroma/delete_account + * @see POST /api/v1/user/email_change + * @see POST /api/v1/user/password_change + * @see POST /api/v1/accounts/delete_account */ - security: v.software === PLEROMA, + security: any([v.software === PLEROMA, v.software === GOTOSOCIAL]), /** * Ability to manage account sessions.