Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: request refresh token when impersonating #1503

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
24 changes: 5 additions & 19 deletions client-app/core/composables/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import { useLocalStorage, createGlobalState } from "@vueuse/core";
import type { AfterFetchContext } from "@vueuse/core";
import { createGlobalState, useLocalStorage } from "@vueuse/core";
import { computed, ref } from "vue";
import { useFetch } from "@/core/api/common";
import { errorHandler, toServerError } from "@/core/api/common/utils";
import { globals } from "@/core/globals";
import { TabsType, unauthorizedErrorEvent, useBroadcast, userBeforeUnauthorizeEvent } from "@/shared/broadcast";
import type { AfterFetchContext } from "@vueuse/core";

type IdentityErrorType = {
code?: string;
description?: string;
};

type ConnectTokenResponseType = {
expires_in?: number;
access_token?: string;
refresh_token?: string;
errors?: Array<IdentityErrorType>;
error: string;
token_type?: string;
};
import type { ConnectTokenResponseType } from "../types";

function _useAuth() {
const broadcast = useBroadcast();
Expand Down Expand Up @@ -107,13 +94,11 @@ function _useAuth() {
}

async function externalSignInCallback(): Promise<void> {
const params = new URLSearchParams({
getTokenParams.value = new URLSearchParams({
grant_type: "external_sign_in",
scope: "offline_access",
});

getTokenParams.value = params;

await (getTokenRequest = getToken(true));
}

Expand Down Expand Up @@ -184,6 +169,7 @@ function _useAuth() {
setTokenType,
setAccessToken,
setExpiresAt,
setRefreshToken,
};
}

Expand Down
32 changes: 20 additions & 12 deletions client-app/core/composables/useImpersonate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useNotifications } from "@/shared/notification";
import type { ConnectTokenResponseType } from "../types";

export function _useImpersonate() {
const { setTokenType, setAccessToken, setExpiresAt } = useAuth();
const { setTokenType, setAccessToken, setExpiresAt, setRefreshToken } = useAuth();
const broadcast = useBroadcast();
const status = ref();
const notifications = useNotifications();
Expand All @@ -23,6 +23,7 @@ export function _useImpersonate() {
.post(
new URLSearchParams({
grant_type: "impersonate",
scope: "offline_access",
user_id: userId,
}),
"application/x-www-form-urlencoded",
Expand All @@ -32,21 +33,28 @@ export function _useImpersonate() {
if (!data.value || error.value) {
status.value = "error";
} else {
const { access_token, token_type, expires_in } = data.value;
const { access_token, token_type, expires_in, refresh_token, error: tokenError, errors } = data.value;

setAccessToken(access_token);
setExpiresAt(expires_in);
setTokenType(token_type);
if (access_token && token_type && expires_in && refresh_token) {
setAccessToken(access_token);
setExpiresAt(expires_in);
setTokenType(token_type);
setRefreshToken(refresh_token);
status.value = "success";
notifications.success({ text: t("pages.account.impersonate.success") });

status.value = "success";
notifications.success({ text: t("pages.account.impersonate.success") });

// reload all tabs to renew state
setTimeout(() => {
void broadcast.emit(reloadAndOpenMainPage, null, TabsType.ALL);
}, 1000);
// reload all tabs to renew state
setTimeout(() => {
void broadcast.emit(reloadAndOpenMainPage, null, TabsType.ALL);
}, 1000);
} else {
notifications.error({ text: t("pages.account.impersonate.error") });
Logger.error(impersonate.name, tokenError, errors);
status.value = "error";
}
}
} catch (e) {
notifications.error({ text: t("pages.account.impersonate.error") });
Logger.error(impersonate.name, e);
status.value = "error";
}
Expand Down
13 changes: 10 additions & 3 deletions client-app/core/types/connect-token-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
type IdentityErrorType = {
code?: string;
description?: string;
};

export type ConnectTokenResponseType = {
expires_in: number;
access_token: string;
token_type: string;
expires_in?: number;
access_token?: string;
refresh_token?: string;
errors?: Array<IdentityErrorType>;
error?: string;
token_type?: string;
};
2 changes: 1 addition & 1 deletion locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "Nachahmung",
"success": "Sie werden nachgeahmt",
"success": "Impersonation erfolgreich",
"error": "Einige Probleme während der Nachahmung. Überprüfen Sie Ihre Berechtigungen"
}
},
Expand Down
2 changes: 1 addition & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "Impersonation",
"success": "You are impersonated",
"success": "Impersonation succeeded",
"error": "Some problems occurred during impersonation. Check your permissions"
}
},
Expand Down
2 changes: 1 addition & 1 deletion locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "Suplantación",
"success": "Estás suplantado",
"success": "Suplantación exitosa",
"error": "Algunos problemas durante la suplantación. Verifica tus permisos"
}
},
Expand Down
2 changes: 1 addition & 1 deletion locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "Impersonation",
"success": "Vous êtes en train d'être imité",
"success": "Impersonation réussie",
"error": "Des problèmes sont survenus lors de l'imitation. Vérifiez vos autorisations"
}
},
Expand Down
2 changes: 1 addition & 1 deletion locales/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "Impersonificazione",
"success": "Sei impersonato",
"success": "Impersonificazione riuscita",
"error": "Alcuni problemi durante l'impersonificazione. Controlla i tuoi permessi"
}
},
Expand Down
2 changes: 1 addition & 1 deletion locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "代理ログイン",
"success": "代理ログインしました",
"success": "代理ログイン成功",
"error": "代理ログイン中に問題が発生しました。権限を確認してください"
}
},
Expand Down
2 changes: 1 addition & 1 deletion locales/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "Podrabianie",
"success": "Jesteś podrobiony",
"success": "Podrobienie zakończone",
"error": "Wystąpiły problemy podczas podrabiania. Sprawdź swoje uprawnienia"
}
},
Expand Down
2 changes: 1 addition & 1 deletion locales/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "Impersonação",
"success": "Você está sendo personificado",
"success": "Impersonificação bem-sucedida",
"error": "Alguns problemas durante a personificação. Verifique suas permissões"
}
},
Expand Down
6 changes: 3 additions & 3 deletions locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -1104,9 +1104,9 @@
"update_error_alert": "Произошла ошибка при обновлении профиля"
},
"impersonate": {
"title": "Авторизация",
"success": "Вы авторизовались",
"error": "Возникли проблемы при авторизации. Проверьте ваши права доступа"
"title": "Имперсонализация",
"success": "Имперсонализация прошла успешно",
"error": "Возникли проблемы при имперсонализации. Проверьте ваши права доступа"
}
},
"company": {
Expand Down
2 changes: 1 addition & 1 deletion locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@
},
"impersonate": {
"title": "冒充",
"success": "您已被冒充",
"success": "冒充成功",
"error": "冒充时出现一些问题。检查您的权限"
}
},
Expand Down
Loading